Archive for September, 2007

Java & XML, 2nd Edition private void loadFromElements(List (Hp web site)

Tuesday, September 25th, 2007

Java & XML, 2nd Edition private void loadFromElements(List elements, StringBuffer baseName) { // Iterate through each elementfor (Iterator i = elements.iterator(); i.hasNext( ); ) { Element current = (Element)i.next( ); String name = current.getName( ); String text = current.getTextTrim( ); // Don’t add “.” if no baseName if (baseName.length( ) > 0) { baseName.append(”.”); } baseName.append(name); // See if we have an element value if ((text == null) || (text.equals(”"))) { // If no text, recurse on childrenloadFromElements(current.getChildren( ), baseName); } else { // If text, this is a property setProperty(baseName.toString( ), text); } // On unwind from recursion, remove last name if (baseName.length() == name.length( )) { baseName.setLength(0); } else { baseName.setLength(baseName.length( ) - (name.length( ) + 1)); } } } The implementation of the load( ) method (which all overloaded versions delegate to) uses SAXBuilder to read in the supplied XML document. I discussed this earlier in the chapter, and I’ll look at it in even more detail in the next; for now, it’s enough to realize that it simply reads XML into a JDOM Document object. The name for a property consists of the names of all the elements leading to the property value, with a period separating each name. Here’s a sample property in XML:
“.” The property name can be obtained by taking the element names leading to the value (excluding the properties element, which was used as a root-level container): org, enhydra, and classpath. Throw a period between each, and you get org.enhydra.classpath, which is the property name in question. To accomplish this, I coded up the loadFromElements( ) method. This takes in a list of elements, iterates through them, and deals with each element individually. If the element has a textual value, that value is added to the superclass object’s properties. If it has child elements instead, then the children are obtained, and recursion
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Java & XML, 2nd Edition To accomplish this (Graphic web design)

Wednesday, September 19th, 2007

Java & XML, 2nd Edition To accomplish this task, you can start by creating an XMLProperties class that extends the java.util.Properties class. With this approach, making things work becomes simply a matter of overriding the load( ), save( ), and store( ) methods. The first of these, load( ) , reads in an XML document and loads the properties within that document into the superclass object. Don’t mistake this class for an all-purpose XML-to-properties converter; it only will read in XML that is in the format detailed earlier in this chapter. In other words, properties are elements with either textual or attribute values but not both; I’ll cover both approaches, but you’ll have to choose one or the other. Don’t try to take all your XML documents, read them in, and expect things to work as planned! The second method, save( ) , is actually deprecated in Java 2, as it doesn’t expose any error information; still, it needs to be overridden for Java 1.1 users. To facilitate this, the implementation in XMLProperties simply calls store( ) . And store( ) handles the task of writing the properties information out to an XML document. Example 7-6 is a good start at this, and provides a skeleton within which to work. Example 7-6. The skeleton of the XMLProperties class package javaxml2; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Properties; import org.jdom.Attribute; import org.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class XMLProperties extends Properties { public void load(Reader reader) throws IOException { // Read XML document into a Properties object }
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Florida web design - Java & XML, 2nd Edition in no line

Tuesday, September 18th, 2007

Java & XML, 2nd Edition in no line breaks and no indentation. The resultant document would have the entire document, except for the XML declaration, on a single line. I’d show you this, but it wouldn’t fit on the page, and tends to cause confusion. The outputter has several constructors though: public XMLOutputter( ); public XMLOutputter(String indent); public XMLOutputter(String indent, boolean newlines); public XMLOutputter(String indent, boolean newlines, String encoding); public XMLOutputter(XMLOutputter that); Most of these are self-explanatory. The indent parameter allows specification of how many spaces to use for indentation; I used two spaces (” “) in the sample code. The boolean value for newlines determines if line breaks are used (this was on in the sample). If needed, an encoding parameter can be specified, which becomes the value for encoding in the XML declaration: Additionally, there are mutator methods for all of these properties (setIndent( ), setEncoding( ), etc.) in the class. There are also versions of the output( ) method (the one used in the example code) that take either an OutputStream or a Writer. And there are versions that take the various JDOM constructs as input, so you could output an entire Document, or just an Element, Comment, ProcessingInstruction, or anything else: // Create an outputter with 4 space indentation and new linesXMLOutputter outputter = new XMLOutputter(” “, true); // Output different JDOM constructsoutputter.output(myDocument, myOutputStream); outputter.output(myElement, myWriter); outputter.output(myComment, myOutputStream); // etc… In other words, XMLOutputter serves all of your XML output needs. Of course, you can also use DOMOutputter and SAXOutputter, which I’ll cover in detail in the next chapter. 7.3 XMLProperties Let’s take things to the next logical step, and look at reading XML. Continuing with the example of converting a properties file to XML, you are now probably wondering how to access the information in your XML file. Luckily, there’s a solution for that, too! In this section, for the sake of explaining how JDOM reads XML, I want to introduce a new utility class, XMLProperties. This class is essentially an XML-aware version of the Java Properties class; in fact, it extends that class. This class allows access to an XML document through the typical property-access methods like getProperty( ) and properties( ); in other words, it allows Java-style access (using the Properties class) to XML-style storage. In my opinion, this is the best combination you can get.
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.

Photography web hosting - Java & XML, 2nd Edition Example 7-5. Output

Tuesday, September 18th, 2007

Java & XML, 2nd Edition Example 7-5. Output of PropsToXML using attributes

Each property value is now an attribute of the innermost element. Notice that JDOM converts the quotation marks within the attribute values, which are disallowed, to entity references so the document as output is well-formed. However, this makes the output a little less clean, so you may want to switch your code back to using textual data within elements, rather than attributes. 7.2.4 Outputting XML with JDOM Before we continue, I want to spend a little time talking about the output portion of the code that I skimmed over earlier in the chapter. It’s highlighted again here: private void convertToXML(Properties props, String xmlFilename) throws IOException { // Create a new JDOM Document with a root element “properties” Element root = new Element(”properties”); Document doc = new Document(root); // Get the property names Enumeration propertyNames = props.propertyNames( ); while (propertyNames.hasMoreElements( )) { String propertyName = (String)propertyNames.nextElement( ); String propertyValue = props.getProperty(propertyName); createXMLRepresentation(root, propertyName, propertyValue); } // Output document to supplied filename XMLOutputter outputter = new XMLOutputter(” “, true); FileOutputStream output = new FileOutputStream(xmlFilename); outputter.output(doc, output); } You already know that XMLOutputter is the class to use for handling output to a file, stream, or other static representation. However, I supplied some arguments to the constructor in the code sample; without any arguments, the outputter would perform direct output. There would be no change to the XML used as input. When reading in XML, this most often results
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Java & XML, 2nd Edition (Cool web site) “org.apache.xerces.parsers.SAXParser” And, just

Monday, September 17th, 2007

Java & XML, 2nd Edition
“org.apache.xerces.parsers.SAXParser”
And, just as quickly as you’ve started in on JDOM, you’ve got the hang of it. However, you might notice that the XML document violates one of the rules of thumb for document design introduced in Chapter 2 (in the section detailing usage of elements versus usage of attributes). You see, each property value has a single textual value. That arguably makes the property values suitable as attributes of the last element on the stack, rather than content. Proving that rules are meant to be broken, I prefer them as content in this case, but that’s neither here nor there. For no other reason than demonstration purposes, let’s look at converting the property values to attributes rather than textual content. This turns out to be quite easy, and can be done in one of two ways. The first is to create an instance of the JDOM Attribute class. The constructor for that class takes the name of the attribute and its value. Then, the resulting instance can be added to the leaf element with that element’s setAttribute( ) method. That approach is shown here: // When out of loop, what’s left is the final element’s nameElement last = new Element(name); /* last.setText(propertyValue); */ Attribute attribute = new Attribute(”value”, propertyValue); current.setAttribute(attribute); current.addContent(last); If you want to compile the file with these changes, be sure you add an import statement for the Attribute class: import org.jdom.Attribute; A slightly easier way is to use one of the convenience methods that JDOM offers. Since adding attributes is such a common task, the Element class provides an overloaded version of setAttribute( ) that takes a name and value, and internally creates an Attribute object. In this case, that approach is a little clearer: // When out of loop, what’s left is the final element’s nameElement last = new Element(name); /* last.setText(propertyValue); */ last.setAttribute(”value”, propertyValue); current.addContent(last); This works just as well, but also avoids having to use an extra import statement. You can compile this change in and run the sample program. The new output should match Example 7-5.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Java & XML, 2nd Edition property can be

Monday, September 17th, 2007

Java & XML, 2nd Edition property can be added to the leaf element, which turns out to be (nicely) the element that the current pointer references. Add this code to your source file: private void createXMLRepresentation(Element root, String propertyName, String propertyValue) { /* Element element = new Element(propertyName); element.setText(propertyValue); root.addContent(element); */ int split; String name = propertyName; Element current = root; Element test = null; while ((split = name.indexOf(”.”)) != -1) { String subName = name.substring(0, split); name = name.substring(split+1); // Check for existing element if ((test = current.getChild(subName)) == null) { Element subElement = new Element(subName); current.addContent(subElement); current = subElement; } else { current = test; } } // When out of loop, what’s left is the final element’s name Element last = new Element(name); last.setText(propertyValue); current.addContent(last); } With this addition in place, recompile the program and run it again. This time, your output should be a lot nicer, as shown in Example 7-4. Example 7-4. Updated output from PropsToXML
“:” “./bootstrap.conf” org.enhydra.multiServer.bootstrap.Bootstrap “.”
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Web design course - Java & XML, 2nd Edition This whirs along

Sunday, September 16th, 2007

Java & XML, 2nd Edition This whirs along for a fraction of a second, and then generates an enhydraProps.xml file. Open this up; it should look like Example 7-3.4 Example 7-3. First version of the enhydraProps.xml document
“:” “./bootstrap.conf” org.enhydra.multiServer.bootstrap.Bootstrap “.” “org.apache.xerces.parsers.SAXParser” In about 50 lines of code, you’ve gone from Java properties to XML. However, this XML document isn’t much better than the properties file; there is still no way to relate the org.enhydra.initialArgs property to the org.enhydra.classpath property. Our job isn’t done yet. Instead of using the property name as the element name, the code needs to take the property name and split it on the period delimiters. For each of these “sub-names,” an element needs to be created and added to the element stack. Then the process can repeat. For the property name org.xml.sax, the following XML structure should result: [property Value] At each step, using the Element constructor and the addContent( ) method does the trick; and once the name is completely deconstructed, the setText( ) method can be used to set the last element’s textual value. The best way is to create a new Element, called current, and use it is a “pointer” (there aren’t any pointers in Java it’s just a term); it will always point at the element that content should be added to. At each step, the code also needs to see if the element to be added already exists. For example, the first property, org.xml.sax, creates an org element. When the next property is added (org.enhydra.classpath), the org element does not need to be created again. To facilitate this, the getChild( ) method is used. This method takes the name of the child element to retrieve, and is available to all instances of the Element class. If the child specified exists, that element is returned. However, if no child exists, a null value is returned, and it is on this null value that our code can key. In other words, if the return value is an element, that becomes the current element, and no new element needs to be created (it already exists). However, if the return from the getChild( ) call is null, a new element must be created with the current sub-name, added as content to the current element, and then the current pointer is moved down the tree. Finally, once the iteration is over, the textual value of the Note that the line wraps in the example are for publishing purposes only; in your document, each property with opening tag, text, and closing tag should be on its own line.
We recommend high quality webhost to host and run your jsp application: christian web host services.

Free web host - Java & XML, 2nd Edition // Get the

Sunday, September 16th, 2007

Java & XML, 2nd Edition // Get the property names Enumeration propertyNames = props.propertyNames( ); while (propertyNames.hasMoreElements( )) { String propertyName = (String)propertyNames.nextElement( ); String propertyValue = props.getProperty(propertyName); createXMLRepresentation(root, propertyName, propertyValue); } // Output document to supplied filename XMLOutputter outputter = new XMLOutputter(” “, true); FileOutputStream output = new FileOutputStream(xmlFilename); outputter.output(doc, output); } Don’t worry about the last few lines that output the JDOM Document yet. I’ll deal with this in the next section, but first I want to cover the createXML-Representation( ) method, which contains the logic for dealing with a single property, and creating XML from it. The easiest (and logically, the first) step in moving from a property to XML is to take the name of the property and create an Element with that name. You’ve already seen how to do this; simply pass the name of the element to its constructor. Once the element is created, assign the value of the property as the textual content of the element. This can be done easily enough through the setText( ) method, which of course takes a String. Once the element is ready for use, it can be added as a child of the root element through the addContent( ) method. In fact, any legal JDOM construct can be passed to an element’s addContent( ) method, as it is overloaded to accept these various types. These include instances of a JDOM Entity, Comment, ProcessingInstruction, and more. But I’ll get to those later; for now, add the following method into your source file: /** *

This will convert a single property and its value to * an XML element and textual value.

* * @param root JDOM root Element to add children to. * @param propertyName name to base element creation on. * @param propertyValue value to use for property. */ private void createXMLRepresentation(Element root, String propertyName, String propertyValue) { Element element = new Element(propertyName); element.setText(propertyValue); root.addContent(element); } At this point, you can actually compile the source file, and then use the resulting PropsToXMLclass. Supply a properties file (you can type in or download the enhydra.properties file shown earlier in this chapter), as well as an output filename, as shown here:3 /javaxml2/build $ java javaxml2.PropsToXML /javaxml2/ch07/properties/enhydra.properties enhydraProps.xml If you’re not familiar with *NIX, the backslash at the end of each line ( ) simply allows for continuation of the command on the next line; Windows users should enter the entire command on one line.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Java & XML, 2nd Edition /** * Provide (Virtual web hosting)

Sunday, September 16th, 2007

Java & XML, 2nd Edition /** *

Provide a static entry point for running.

*/ public static void main(String[] args) { if (args.length != 2) { System.out.println(”Usage: java javaxml2.PropsToXML ” + “[properties file] [XML file for output]”); System.exit(0); } try { PropsToXML propsToXML = new PropsToXML( ); propsToXML.convert(args[0], args[1]); } catch (Exception e) { e.printStackTrace( ); } } } The only new part of this code is the Java Properties object, which I’ve mentioned briefly. The supplied properties filename is used in the load( ) method, and that object is delegated on to a method that will use JDOM, which I’ll focus on next. 7.2.3 Creating XML with JDOM Once the code has the properties in a (more) usable form, it’s time to start using JDOM. The first task is to create a JDOM Document. For that to occur, you need to create a root element for the document, using JDOM’s Element class. Since an XML document can’t exist without a root element, an instance of the Element class is required as input for the Document class constructor. Creating an Element requires only the passing of the element’s name. There are alternate versions that take in namespace information, and I’ll discuss those a little later. For now, it’s easiest to use the root element’s name, and since this needs to be a top-level, arbitrary name (to contain all the property nestings), I use “properties” in the code. Once this element is created, it’s used to create a new JDOM Document. Then, it’s on to dealing with the properties in the supplied file. The list of property names is obtained as a Java Enumeration through the Properties object’s propertyNames( ) method. Once that name is available, it can be used to obtain the property value by using the getProperty( ) method. At this point, you’ve got the root element of the new XML document, the property name to add, and the value for that property. And then, like any other good program, you iterate through all of the other properties until finished. At each step, this information is supplied to a new method, createXMLRepresentation( ) . This performs the logic for handling conversion of a single property into a set of XML elements. Add this code, as shown here, to your source file: private void convertToXML(Properties props, String xmlFilename) throws IOException { // Create a new JDOM Document with a root element “properties” Element root = new Element(”properties”); Document doc = new Document(root);
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Web proxy server - Java & XML, 2nd Edition document using JDOM,

Saturday, September 15th, 2007

Java & XML, 2nd Edition document using JDOM, and outputs it to the specified filename. Example 7-2 starts the ball rolling. Example 7-2. The skeleton of the PropsToXML class package javaxml2; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; public class PropsToXML { /** *

This will take the supplied properties file, and * convert that file to an XML representation, which is * then output to the supplied XML document filename.

* * @param propertiesFilename file to read in as Java properties. * @param xmlFilename file to output XML representation to. * @throws IOException - when errors occur. */ public void convert(String propertiesFilename, String xmlFilename) throws IOException { // Get Java Properties object FileInputStream input = new FileInputStream(propertiesFilename); Properties props = new Properties( ); props.load(input); // Convert to XML convertToXML(props, xmlFilename); } /** *

This will handle the detail of conversion from a Java * Properties object to an XML document.

* * @param props Properties object to use as input. * @param xmlFilename file to output XML to. * @throws IOException - when errors occur. */ private void convertToXML(Properties props, String xmlFilename) throws IOException { // JDOM conversion code goes here }
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.