Archive for July, 2007

Java & XML, 2nd (Web hosts) Edition Java and XML,

Tuesday, July 31st, 2007

Java & XML, 2nd Edition

Java and XML, 2nd edition, is now available at bookstores, as well as through O’Reilly at http://www.oreilly.com.

In this XHTML fragment, the whitespace between the opening p element and the opening i element is not ignorable, and therefore reported through the characters( ) callback. If you aren’t completely confused (and I don’t think you are), be prepared to closely monitor both of the character-related callbacks. That will make explaining the last SAX callback related to this issue a snap. 3.3.7 Ignorable Whitespace With all that whitespace discussion done, adding an implementation for the ignorableWhitespace( ) method is a piece of cake. Since the whitespace reported is ignorable, the code does just that ignore it: public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // This is ignorable, so don’t display it } Whitespace is reported in the same manner as character data; it can be reported with one callback, or a SAX parser may break up the whitespace and report it over several method invocations. In either case, adhere closely to the precautions about not making assumptions or counting on whitespace as textual data in order to avoid troublesome bugs in your applications. 3.3.8 Entities As you recall, there is only one entity reference in the contents.xml document, OReillyCopyright. When parsed and resolved, this results in another file being loaded, either from the local filesystem or some other URI. However, validation is not turned on in the reader implementation being used.3 An often overlooked facet of nonvalidating parsers is that they are not required to resolve entity references, and instead may skip them. This has caused some headaches before, as parser results may simply not include entity references that were expected to be included. SAX 2.0 nicely accounts for this with a callback that is issued when an entity is skipped by a nonvalidating parser. The callback gives the name of the entity, which can be included in the viewer’s output: public void skippedEntity(String name) throws SAXException { DefaultMutableTreeNode skipped = new DefaultMutableTreeNode(”Skipped Entity: ‘” + name + “‘”); current.add(skipped); } Before you go looking for the OReillyCopyright node, though, you should be aware that most established parsers will not skip entities, even if they are not validating. Apache Xerces, I’m assuming that even if you aren’t using Apache Xerces, your parser does not leave validation on by default. If you get different results than shown in this chapter, consult your documentation and see if validation is on. If it is, sneak a peek at Chapter 4 and see how to turn it off.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Java & XML, 2nd Edition Currently, neither Apache

Tuesday, July 31st, 2007

Java & XML, 2nd Edition Currently, neither Apache Xerces nor just about any other parser available performs validation by default. In the example program, since nothing has been done to turn it on, no validation occurs. However, that does not mean that a DTD or schema is not processed, again in almost all cases. Note that even without validation, an exception resulted when no system ID could be found, and the DTD reference could not be resolved (in the section on InputSources). So be sure to realize the difference between validation occurring, and DTD or schema processing occurring. Triggering of ignorableWhitespace( ) only requires that DTD or schema processing occurs, not that validation occurs. Finally, whitespace is often reported by the characters( ) method. This introduces additional confusion, as another SAX callback, ignorableWhitespace( ), also reports whitespace. Unfortunately, a lot of books (including, I’m embarrassed to admit, my first edition of Java and XML) got the details of whitespace either partially or completely wrong. So, let me take this opportunity to set the record straight. First, if no DTD or XML Schema is referenced, the ignorable-Whitespace( ) method should never be invoked. Period. The reason is that a DTD (or schema) details the content model for an element. In other words, in the JavaXML.dtd file, the contents element can only have chapter elements within it. Any whitespace between the start of the contents element and the start of a chapter element is (by logic) ignorable. It doesn’t mean anything, because the DTD says not to expect any character data (whitespace or otherwise). The same thing applies for whitespace between the end of a chapter element and the start of another chapter element, or between it and the end of the contents element. Because the constraints (in DTD or schema form) specify that no character data is allowed, this whitespace cannot be meaningful. However, without a constraint specifying that information to a parser, that whitespace cannot be interpreted as meaningless. So by removing the reference to a DTD, these various whitespaces would trigger the characters( ) callback, where previously they triggered the ignorableWhitespace( ) callback. Thus whitespace is never simply ignorable, or nonignorable; it all depends on what (if any) constraints are referenced. Change the constraints, and you might change the meaning of the whitespace. Let’s dive even deeper. In the case where an element can only have other elements within it, things are reasonably clear. Whitespace in between elements is ignorable. However, consider a mixed content model: If this looks like gibberish, think of HTML; it represents (in part) the constraints for the pelement, or paragraph tag. Of course, text within this tag can exist, and also bold (b), italics (i), and links (a) elements as well. In this model, there is no whitespace between the starting and ending p tags that will ever be reported as ignorable (with or without a DTD or schema reference). That’s because it’s impossible to distinguish between whitespace used for readability and whitespace that is supposed to be in the document. For example:
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Java & XML, 2nd Edition As you write

Monday, July 30th, 2007

Java & XML, 2nd Edition As you write SAX event handlers, be sure to keep your mind in a hierarchical mode. In other words, you should not get in the habit of thinking that an element owns its data and child elements, but only that it serves as a parent. Also keep in mind that the parser is moving along, handling elements, attributes, and data as it comes across them. This can make for some surprising results. Consider the following XML document fragment:
This element has embedded text within it. Forgetting that SAX parses sequentially, making callbacks as it sees elements and data, and forgetting that the XML is viewed as hierarchical, you might make the assumption that the output here would be something like Figure 3-2. Figure 3-2. Expected, and incorrect, graphical tree This seems logical, as the parent element completely “owns” the child element. But what actually occurs is that a callback is made at each SAX event-point, resulting in the tree shown in Figure 3-3. Figure 3-3. Actual generated tree SAX does not read ahead, so the result is exactly what you would expect if you viewed the XML document as sequential data, without all the human assumptions that we tend to make. This is an important point to remember.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Java & XML, 2nd Edition public (Web server) void endElement(String

Monday, July 30th, 2007

Java & XML, 2nd Edition public void endElement(String namespaceURI, String localName, String qName) throws SAXException { // Walk back up the tree current = (DefaultMutableTreeNode)current.getParent( ); } One final note before moving on to element data: you may have noticed that with a namespace URI and an element’s Q name, it would be possible to figure out the prefix as well as the URI from the information supplied to the startElement( ) callback, without having to use a map of namespace associations. That’s absolutely true, and would serve the example code well. However, most applications have hundreds and even thousands of lines of code in these callbacks (or, better yet, in methods invoked from code within these callbacks). In those cases, relying on parsing of the element’s Q name is not nearly as robust a solution as storing the data in a custom structure. In other words, splitting the Q name on a colon is great for simple applications, but isn’t so wonderful for complex (and therefore more realistic) ones. 3.3.6 Element Data Once the beginning and end of an element block are identified and the element’s attributes are enumerated for an application, the next piece of important information is the actual data contained within the element itself. This generally consists of additional elements, textual data, or a combination of the two. When other elements appear, the callbacks for those elements are initiated, and a type of pseudo-recursion happens: elements nested within elements result in callbacks “nested” within callbacks. At some point, though, textual data will be encountered. Typically the most important information to an XML client, this data is usually either what is shown to the client or what is processed to generate a client response. In XML, textual data within elements is sent to a wrapping application via the characters( ) callback. This method provides the wrapping application with an array of characters as well as a starting index and the length of the characters to read. Generating a String from this array and applying the data is a piece of cake: public void characters(char[] ch, int start, int length) throws SAXException { String s = new String(ch, start, length); DefaultMutableTreeNode data = new DefaultMutableTreeNode(”Character Data: ‘” + s + “‘”); current.add(data); } Seemingly a simple callback, this method often results in a significant amount of confusion because the SAX interface and standards do not strictly define how this callback must be used for lengthy pieces of character data. In other words, a parser may choose to return all contiguous character data in one invocation, or split this data up into multiple method invocations. For any given element, this method will be called not at all (if no character data is present within the element) or one or more times. Parsers implement this behavior differently, often using algorithms designed to increase parsing speed. Never count on having all the textual data for an element within one callback method; conversely, never assume that multiple callbacks would result from one element’s contiguous character data.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Web and email hosting - Java & XML, 2nd Edition In the example,

Sunday, July 29th, 2007

Java & XML, 2nd Edition In the example, several things occur that illustrate this capability. First, a new node is created and added to the tree with the local name of the element. Then, that node becomes the current node, so all nested elements and attributes are added as leaves. Next, the namespace is determined, using the supplied namespace URI and the namespaceMappings object (to get the prefix) that you just added to the code from the last section. This is added as a node, as well. Finally, the code iterates through the Attributes interface, adding each (with local name and namespace information) as a child node. The code to accomplish all this is shown here: public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { DefaultMutableTreeNode element = new DefaultMutableTreeNode(”Element: ” + localName); current.add(element); current = element; // Determine namespaceif (namespaceURI.length( ) > 0) { String prefix = (String)namespaceMappings.get(namespaceURI); if (prefix.equals(”")) { prefix = “[None]”; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(”Namespace: prefix = ‘” + prefix + “‘, URI = ‘” + namespaceURI + “‘”); current.add(namespace); } // Process attributes for (int i=0; i 0) { String attPrefix = (String)namespaceMappings.get(namespaceURI); if (attPrefix.equals(”")) { attPrefix = “[None]”; } DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(”Namespace: prefix = ‘” + attPrefix + “‘, URI = ‘” + attURI + “‘”); attribute.add(attNamespace); } current.add(attribute); } } The end of an element is much easier to code. Since there is no need to give any visual information, all that must be done is to walk back up the tree one node, leaving the element’s parent as the new current node:
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Java & (Web hosting unlimited bandwidth) XML, 2nd Edition The solution shown

Sunday, July 29th, 2007

Java & XML, 2nd Edition The solution shown here is far from a complete one in terms of dealing with more complex namespace issues. It’s perfectly legal to reassign prefixes to new URIs for an element’s scope, or to assign multiple prefixes to the same URI. In the example, this would result in widely scoped namespace mappings being overwritten by narrowly scoped ones in the case where identical URIs were mapped to different prefixes. In a more robust application, you would want to store prefixes and URIs separately, and have a method of relating the two without causing overwriting. However, you get the idea in the example of how to handle namespaces in the general sense. 3.3.5 Element Callbacks By now you are probably ready to get to the data in the XML document. It is true that over half of the SAX callbacks have nothing to do with XML elements, attributes, and data. This is because the process of parsing XML is intended to do more than simply provide your application with the XML data; it should give the application instructions from XML PIs so your application knows what actions to take, let the application know when parsing begins and when it ends, and even tell it when there is whitespace that can be ignored! If some of these callbacks don’t make much sense yet, keep reading. Of course, there certainly are SAX callbacks intended to give you access to the XML data within your documents. The three primary events involved in getting that data are the start and end of elements and the characters( ) callback. These tell you when an element is parsed, the data within that element, and when the closing tag for that element is reached. The first of these, startElement( ), gives an application information about an XML element and any attributes it may have. The parameters to this callback are the name of the element (in various forms) and an org.xml.sax.Attributes instance. This helper class holds references to all of the attributes within an element. It allows easy iteration through the element’s attributes in a form similar to a Vector. In addition to being able to reference an attribute by its index (used when iterating through all attributes), it is possible to reference an attribute by its name. Of course, by now you should be a bit cautious when you see the word “name” referring to an XML element or attribute, as it can mean various things. In this case, either the complete name of the attribute (with a namespace prefix, if any), called its Q name, can be used, or the combination of its local name and namespace URI if a namespace is used. There are also helper methods such as getURI(int index) and getLocal-Name(int index) that help give additional namespace information about an attribute. Used as a whole, the Attributes interface provides a comprehensive set of information about an element’s attributes. In addition to the element attributes, you get several forms of the element’s name. This again is in deference to XML namespaces. The namespace URI of the element is supplied first. This places the element in its correct context across the document’s complete set of namespaces. Then the local name of the element is supplied, which is the unprefixed element name. In addition (and for backwards compatibility), the Q name of the element is supplied. This is the unmodified, unchanged name of the element, which includes a namespace prefix if present; in other words, exactly what was in the XML document: ora:copyright for the copyright element. With these three types of names supplied, you should be able to describe an element with or without respect to its namespace.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Web design tools - Java & XML, 2nd Edition class JTreeContentHandler implements

Sunday, July 29th, 2007

Java & XML, 2nd Edition class JTreeContentHandler implements ContentHandler { /** Hold onto the locator for location information */ private Locator locator; /** Store URI to prefix mappings */ private Map namespaceMappings; /** Tree Model to add nodes to */ private DefaultTreeModel treeModel; /** Current node to add sub-nodes to */ private DefaultMutableTreeNode current; public JTreeContentHandler(DefaultTreeModel treeModel, DefaultMutableTreeNode base) { this.treeModel = treeModel; this.current = base; this.namespaceMappings = new HashMap( ); } // Existing methods public void startPrefixMapping(String prefix, String uri) { // No visual events occur here. namespaceMappings.put(uri, prefix); } public void endPrefixMapping(String prefix) { // No visual events occur here. for (Iterator i = namespaceMappings.keySet().iterator( ); i.hasNext( ); ) { String uri = (String)i.next( ); String thisPrefix = (String)namespaceMappings.get(uri); if (prefix.equals(thisPrefix)) { namespaceMappings.remove(uri); break; } } } } One thing of note: I used the URI as a key to the mappings, rather than the prefix. As I mentioned a moment ago, the startElement( ) callback reports the namespace URI for the element, not the prefix. So keying on URIs makes those lookups faster. However, as you see in endPrefixMapping( ), it does add a little bit of work to removing the mapping when it is no longer available. In any case, storing namespace mappings in this fashion is a fairly typical SAX trick, so store it away in your toolkit for XML programming.
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web server certificate - Java & XML, 2nd Edition Recommendation. With SAX

Saturday, July 28th, 2007

Java & XML, 2nd Edition Recommendation. With SAX 2.0, support for namespaces was introduced at the element level. This allows a distinction to be made between the namespace of an element, signified by an element prefix and an associated namespace URI, and the local name of an element. In this case, the term local name refers to the unprefixed name of an element. For example, the local name of the ora:copyright element is simply copyright. The namespace prefix is ora, and the namespace URI is declared as http://www.oreilly.com/. There are two SAX callbacks specifically dealing with namespaces. These callbacks are invoked when the parser reaches the beginning and end of a prefix mapping. Although this is a new term, it is not a new concept; a prefix mapping is simply an element that uses the xmlns attribute to declare a namespace. This is often the root element (which may have multiple mappings), but can be any element within an XML document that declares an explicit namespace. For example: In this case, an explicit namespace is declared several element nestings deep within the document. That prefix and URI mapping (in this case, xlink and http://www.w3.org/1999/xlink, respectively) are then available to elements and attributes within the declaring element. The startPrefixMapping( ) callback is given the namespace prefix as well as the URI associated with that prefix. The mapping is considered “closed” or “ended” when the element that declared the mapping is closed, which triggers the endPrefixMapping( ) callback. The only twist to these callbacks is that they don’t quite behave in the sequential manner in which SAX usually is structured; the prefix mapping callback occurs directly before the callback for the element that declares the namespace, and the ending of the mapping results in an event just after the close of the declaring element. However, it actually makes a lot of sense: for the declaring element to be able to use the declared namespace mapping, the mapping must be available before the element’s callback. It works in just the opposite way for ending a mapping: the element must close (as it may use the namespace), and then the namespace mapping can be removed from the list of available mappings. In the JTreeContentHandler, there aren’t any visual events that should occur within these two callbacks. However, a common practice is to store the prefix and URI mappings in a data structure. You will see in a moment that the element callbacks report the namespace URI, but not the namespace prefix. If you don’t store these prefixes (reported through startPrefixMapping( )), they won’t be available in your element callback code. The easiest way to do this is to use a Map, add the reported prefix and URI to this Map in startPrefixMapping( ), and then remove them in endPrefixMapping( ). This can be accomplished with the following code additions:
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Professional web hosting - Java & XML, 2nd Edition an application using

Saturday, July 28th, 2007

Java & XML, 2nd Edition an application using the SAX classes should not have to catch this exception, because it should not have to know where the XML resource is located (it might be a local file, as opposed to a network resource). Instead, the application can catch the single SAXException. Within the SAX reader, the original exception is caught and rethrown as a SAXException, with the originating exception stuffed inside the new one. This allows applications to have one standard exception to trap for, while allowing specific details of what errors occurred within the parsing process to be wrapped and made available to the calling program through this standard exception. The SAXException class provides a method, getException( ), which returns the underlying Exception (if one exists). 3.3.3 Processing Instructions I talked about processing instructions (PIs) within XML as a bit of a special case. They were not considered XML elements, and were handled differently by being made available to the calling application. Because of these special characteristics, SAX defines a specific callback for handling processing instructions. This method receives the target of the processing instruction and any data sent to the PI. For this chapter’s example, the PI can be converted to a new node and displayed in the tree viewer: public void processingInstruction(String target, String data) throws SAXException { DefaultMutableTreeNode pi = new DefaultMutableTreeNode(”PI (target = ‘” + target + “‘, data = ‘” + data + “‘)”); current.add(pi); } In a real application using XML data, this is where an application could receive instructions and set variable values or execute methods to perform application-specific processing. For example, the Apache Cocoon publishing framework might set flags to perform transformations on the data once it is parsed, or to display the XML as a specific content type. This method, like the other SAX callbacks, throws a SAXException when errors occur. It’s worth pointing out that this method will not receive notification of the XML declaration: In fact, SAX provides no means of getting at this information (and you’ll find out that it’s not currently part of DOM or JDOM, either!). The general underlying principle is that this information is for the XML parser or reader, not the consumer of the document’s data. For that reason, it’s not exposed to the developer. 3.3.4 Namespace Callbacks From the discussion of namespaces in Chapter 2, you should be starting to realize their importance and impact on parsing and handling XML. Alongside XML Schema, XML Namespaces is easily the most significant concept added to XML since the original XML 1.0
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Java & XML, 2nd Edition of the ContentHandler (Professional web hosting)

Friday, July 27th, 2007

Java & XML, 2nd Edition of the ContentHandler implementation. Since this might be handy to use later, the code shown here saves the provided Locator instance to a member variable: class JTreeContentHandler implements ContentHandler { /** Hold onto the locator for location information */ private Locator locator; // Constructor public void setDocumentLocator(Locator locator) { // Save this for later use this.locator = locator; } } 3.3.2 The Beginning and the End of a Document In any lifecycle process, there must always be a beginning and an end. These important events should each occur once, the former before all other events, and the latter after all other events. This rather obvious fact is critical to applications, as it allows them to know exactly when parsing begins and ends. SAX provides callback methods for each of these events, startDocument( ) and endDocument( ). The first method, startDocument( ), is called before any other callbacks, including the callback methods within other SAX handlers, such as DTDHandler. In other words, startDocument( ) is not only the first method called within ContentHandler, but also within the entire parsing process, aside from the setDocument-Locator( ) method just discussed. This ensures a finite beginning to parsing, and lets the application perform any tasks it needs to before parsing takes place. The second method, endDocument( ), is always the last method called, again across all handlers. This includes situations in which errors occur that cause parsing to halt. I will discuss errors later, but there are both recoverable errors and unrecoverable errors. If an unrecoverable error occurs, the ErrorHandler’s callback method is invoked, and then a final call to endDocument( ) completes the attempted parsing. In the example code, no visual event should occur with these methods; however, as with implementing any interface, the methods must still be present: public void startDocument( ) throws SAXException { // No visual events occur here } public void endDocument( ) throws SAXException { // No visual events occur here } Both of these callback methods can throw SAXExceptions. The only types of exceptions that SAX events ever throw, they provide another standard interface to the parsing behavior. However, these exceptions often wrap other exceptions that indicate what problems have occurred. For example, if an XML file was parsed over the network via a URL, and the connection suddenly became invalid, a java.net.SocketException might occur. However,
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.