Archive for December, 2007

Java & XML, 2nd (Most popular web site) Edition // Castor generated

Monday, December 31st, 2007

Java & XML, 2nd Edition // Castor generated classesimport javaxml2.castor.Catalog; import javaxml2.castor.Item; import javaxml2.castor.types.LevelType; public class AddItemServlet extends HttpServlet { private static final String CATALOG_FILE = “c:\java\tomcat\webapps\javaxml2\catalog.xml”; public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter( ); res.setContentType(”text/html”); // Get input parameters String id = req.getParameterValues(”id”)[0]; String levelString = req.getParameterValues(”level”)[0]; String title = req.getParameterValues(”title”)[0]; String teacher = req.getParameterValues(”teacher”)[0]; String[] guests = req.getParameterValues(”guest”); String description = req.getParameterValues(”description”)[0]; // Create new item Item item = new Item( ); item.setId(id); item.setLevel(LevelType.valueOf(levelString)); item.setTitle(title); item.setTeacher(teacher); if (guests != null) { for (int i=0; iIf you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Java & XML, 2nd Edition Teacher: Guests: Description: (Zeus web server)

Sunday, December 30th, 2007

Java & XML, 2nd Edition Teacher: Guests: Description:

You should have Tomcat or another servlet engine running from some of the previous chapters, so I won’t get into those details. Drop this form into one of your web applications, and then enter in and compile the servlet shown in Example 15-4. Example 15-4. The AddItemServlet for Castor package javaxml2; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // Castor classesimport org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.Unmarshaller;
We recommend high quality webhost to host and run your jsp application: christian web host services.

Web host 4 life - Java & XML, 2nd Edition about it just

Sunday, December 30th, 2007

Java & XML, 2nd Edition about it just yet. You can compile the type files, as well as the other Castor-generated sources, with this simple command: javac -d . javaxml2/castor/*.java javaxml2/castor/types/*.java At this point, you have classes that are ready to use. I won’t show you the source for these files here, because it’s quite long (and you can look at it yourself). I’ve listed the key methods for the Catalog class, though, so you’ll get an idea of what to expect: package javaxml2.castor; public class Catalog { // Add a new Item public void addItem( ); // Get the items as an Enumeration public Enumeration enumerateItem( ); // Get all items public Item[] getItem( ); // Get number of items public getItemCount( ); } Notice that you can add items, as well as move through the items available. The names of two of these methods, enumerateItem( ) and getItem( ) , are a bit odd, so watch out for those. I did not expect getItem( ) to return an array, and looked for getItems( ) or getItemList( ) first, myself. Once you’ve got these generated classes, though, you’re ready to use them in your application. 15.2.3 Marshalling and Unmarshalling After you’ve compiled the classes Castor generates, add them to your classpath. You can then use them in your own applications. Example 15-3 shows a basic HTML form that allows a user to enter information about a new item. Example 15-3. HTML form for adding items Add New Item to Catalog

Add New Item


If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Java & XML, 2nd Edition At least as (Web hosting service)

Saturday, December 29th, 2007

Java & XML, 2nd Edition At least as of this writing, Castor supported only the October 2000 XML Schema Candidate Recommendation, as opposed to the final version of that specification. This could require you to make some small modifications in your existing schemas to conform to that earlier specification if you’re using Castor. Hopefully that framework will have caught up by the time you are reading this; you can verify the current level of compliance for XML Schema at http://castor.exolab.org/xmlschema.html. Once you have your XML Schema defined, you are ready to generate classes for the constraints. I’ve named my schema from Example 15-1 catalog.xsd, as you’ll see reflected in the example instructions coming up. Once you’ve got your schema, generating classes with Castor is a piece of cake. You’ll need to use the org.exolab.castor.builder.SourceGenerator class, as shown here: java org.exolab.castor.builder.SourceGenerator -i castor/catalog.xsd-package javaxml2.castor In this example, I’m running the command with my schema in a subdirectory of the current directory, called castor/. I specified the schema with the “-i” flag, and the package to generate the files within through the “-package” flag. There’s a whole slew of other options you can check out by simply entering the class without any options. The class will spit out the various flags and options you can supply. Once the command executes (you’ll get errors if your schema has problems), you will get a directory path correlating to the package you entered. In my example, I ended up with a javaxml2 directory, and a castor directory within that. Within that directory, I ended up with a Catalog.java and CatalogDescriptor.java source file, and an Item.java and ItemDescriptor.java source file. For most situations, you’ll only need to worry about working with the first of each of these pairs. You should also get a subdirectory called types, with some additional files within it. These are generated because of the user-defined type in the XML Schema for the “level” attribute. The result is a class called LevelType. Since there are only five allowed values, Castor must create custom classes for this type to handle it. These type classes are a pain to work with, as there is no way, for example, to do this: // Create a new type with a value of “1″ LevelType levelType = new LevelType(1); Instead, you’ll need to get the value you want to use and convert it to a String. You can then use the valueOf( ) method, which is static, to get an instance of LevelType with the correct value: LevelType levelType = LevelType.valueOf(”1″); Of course, once you get used to this, it’s not such a big deal. If this seems a little fuzzy, you’ll see how to use this class in a practical situation in the next section, so don’t worry too much
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Java & XML, 2nd Edition (Photoshop web design) In this and

Saturday, December 29th, 2007

Java & XML, 2nd Edition In this and subsequent examples, I’ve assumed that you still have a SAX-compliant XML parser, like Xerces, on your classpath in addition to the libraries discussed in this chapter. If you don’t, add xerces.jar or your parser’s jar file(s) to the classpath in addition to the data binding framework you are using. 15.2.2 Source Generation Castor does provide for class generation, using an existing set of constraints to create Java classes. You will need to have an XML Schema that constrains your XML to use this. Example 15-2 provides just such a schema for the document I showed you earlier in Example 15-1. Example 15-2. XML Schema for Example 15-1 (using Castor) Obviously, you may have XML schemas of your own that you want to try out; as long as they conform to the XML Schema specification, they should work with any examples in this section.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Web design portfolio - Java & XML, 2nd Edition to use the

Friday, December 28th, 2007

Java & XML, 2nd Edition to use the class generation, marshalling, and unmarshalling capabilities of each framework. That should be more than enough to get you going. 15.1.4 Use Cases As a final explanation of data binding and why it’s worthwhile, I will give you a small sampling of use-cases. Some of these are best suited for the lower-level APIs like SAX or DOM, and some are perfect for data binding. In Table 15-1, I’ve listed a common use case, the type of API that would work well, and a short reasoning behind my decision. This should help you see how data binding fits into the XML landscape. Table 15-1. API use cases Use case Well-suited API Reasoning XML IDE DOM or JDOM Tree-based viewing of XML, as in an IDE, closely follows the tree-based models of DOM and JDOM. XML messaging server SAX Since speed is the most important factor, SAX allows the fastest stream-based reading of messages. Configuration data Data binding The content, rather than the model, is paramount. Data binding saves time and adds convenience to reading configuration data. XML transformations DOM or JDOM Changing both structure and content requires modification of content (rules out SAX) and structure (rules out data binding). XML messaging client Data binding When the message format is known ahead of time, the client can benefit from easy Java objects, as created by data binding. Obviously, these are just a few common XML applications, but they should give you an idea of when to use a low-level API and when to use a higher-level one. 15.2 Castor The first data binding framework I will discuss is Castor, hosted online at http://castor.exolab.org/. This framework has been around for quite a while, and the latest release as of this writing was Version 0.92. First, it should be made clear that Castor provides quite a bit more than just XML data binding. The package provides bindings in Java for more than just XML; you can also work with LDAP objects, OQL for mapping SQL queries to objects, as well as Java Data Objects (JDO), a fairly new specification from Sun dealing with Java-to-RDBMS (relational database management system) persistence. However, this is an XML book, so I’m only going to talk about the XML bindings. 15.2.1 Installation To get ready to use Castor, you’ll need to download a release from the download page: http://castor.exolab.org/download.html. That page has links to the Exolab FTP site (or you can FTP in manually, as I did), and lists the files available. I’d recommend getting the full release (in the event you want to play with OQL or JDO later), named castor-0.9.2.zip or castor-0.9.2.tgz. Extract the jar files from that archive, add them to your classpath, and you’re ready to go.3 Actually, there are two jar files within the distribution: castor-0.9.2.jar and castor-0.9.2-xml.jar. The first is a superset of the second, so you only need the first; or if you want a smaller archive, you can just use the second for XML binding.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Zeus web server - Java & XML, 2nd Edition 15.1.3 Marshalling Marshalling

Friday, December 28th, 2007

Java & XML, 2nd Edition 15.1.3 Marshalling Marshalling is just the opposite of unmarshalling. It is the process of converting a Java object and its dependent objects into an XML representation. In many cases, marshalling is part of a repeated cycle of transformations to and from XML, and is paired with unmarshalling. As an example, check out Figure 15-2, which is a typical application flow. Figure 15-2. XML data binding application flow There are two distinct ways to marshal a Java object. The first is to invoke a marshal( ) method on an object; this method is usually generated along with the accessors and mutators during class generation. The method recursively calls the marshal( ) method on each of its dependent objects, until you end up with an XML document. Note that the target XML document does not have to be the same as the original XML; you can easily end up with a vast number of archived XML documents by supplying different filenames to the marshalling process. A different approach to marshalling, and the one I favor, is having a standalone class that performs marshalling. Instead of invoking marshal( ) on a generated object, you invoke marshal( ) on the standalone class, and pass it the object to marshal. This is useful because it performs the same tasks as illustrated previously, but also allows classes that were not originally unmarshalled from XML to be converted to XML. Think about it this way: data binding, used like this, becomes a persistence framework. Any object with bean-like properties (setXXX( ) and getXXX( )) can easily be converted to XML! You get the power of data binding with the flexibility of persistence. This is a handy combination, and supported by some of the frameworks I talk about in this chapter. I realize that if you’re new to data binding, this may sound a bit confusing and vague; sort of like talking about chemistry. I’d much rather blow some things up (err. . . you know!), so in the rest of the chapter I show you how to use some data binding frameworks. Since I’m going to cover four of these, none of the examples are immensely complex; instead I focus on how
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Java & (Most popular web site) XML, 2nd Edition public interface Item

Friday, December 28th, 2007

Java & XML, 2nd Edition public interface Item { public String getID( ); public void setID(String id); public int getLevel( ); public void setLevel(int level); public String getTitle( ); public void setTitle(String title); public String getTeacher( ); public void setTeacher(String teacher); public List getGuests( ); public void addGuest(String guest); public void setGuestList(List guests); public String getDescription( ); public void setDescription( ); } This is quite a bit more useful than writing hundreds of lines of SAX callbacks. It makes working with the document a piece of cake, instead of an exercise in your Java and XML API skills. These are just examples, and not necessarily representative of what you’ll get using the APIs covered in this chapter. However, in the following sections, I’ll show you exactly how to use the API and let you know what to expect. 15.1.2 Unmarshalling Keep in mind that once you have a set of generated classes, you still don’t have a great many uses for them. Sure, you could use an existing lower-level XML API to read in an XML document, pull out the data, create new instances of the generated classes, and populate them with the data from the XML. But data binding provides all this out of the box, so why bother with that? In fact, data binding frameworks provide for exactly that process. And in that context, unmarshalling is the process of converting an XML document into an instance of a Java class. I’ve seen, and even been a part of, a lot of confusion about the marshalling/unmarshalling terminology. I’m using the terminology as defined in Sun’s latest version of the Java Architecture for Data Binding (JAXB) specification, which is certain to become the standardized vocabulary. In that specification, marshalling is moving from Java to XML, and unmarshalling is moving from XML to Java. I’d stick with those definitions if I were you, as well. This turns out to be a pretty simple process; you get an XML document, pass it to some tool or class instance in your data binding framework, and you get back a Java object. This is usually a class instance of the top-level Java object representing your document. So, again using Example 15-1, you would get back an instance of the Catalog class. You’ll typically need to cast from a java.lang.Object to the specific class that you’re expecting, since the framework won’t know anything about your classes (because they were generated). But after the class cast, you’re ready to work with the object as a Catalog, not as an XML document. You can then use the various accessor and mutator methods to work with the data, and when you are ready to send the document back to the XML from whence it came, you marshal the document.
We recommend high quality webhost to host and run your jsp application: christian web host services.

Java & XML, 2nd Edition (like those found (Web design)

Thursday, December 27th, 2007

Java & XML, 2nd Edition (like those found in a DTD or XML Schema) are equivalent to Java class definitions. They define the way that data is represented. On the other hand, an XML document is equivalent to an instance of these classes, in that it is simply a set of data that fulfills the contract defined by the document’s constraints. Now, read this paragraph again, slowly, and you’ll have it. The data binding frameworks I talk about in this chapter all have a way of representing a document’s constraints (usually through a DTD or an XML Schema, but there are also some other options, which I’ll get to in the appropriate section). These constraints can then be run through some form of a class generation tool, and you get Java source code ready to compile. This code, once compiled, can be used to generate instance data based on an XML document. You end up with a process akin to that shown in Figure 15-1. Figure 15-1. Class generation in XML data binding Note that the final product here can be concrete classes, interfaces, interfaces and implementations, or any other permutation of Java classes. In the case of Example 15-1 (assuming that the constraints are represented in some arbitrary form), you might end up with a Catalog interface like this: public interface Catalog { public List getItemList( ); public void addItem(Item item); public void setItemList(List items); } Further, you might have an Item interface like this:
We recommend high quality webhost to host and run your jsp application: christian web host services.

Web site development - Java & XML, 2nd Edition The Guitar of

Thursday, December 27th, 2007

Java & XML, 2nd Edition David Wilcox Create fresh new guitar sounds with rich, ringing voicings! David even shows you how to invent your own tunings. Chris Thile Here’s a lesson that will thrill and inspire mandolin players at all levels. Sam Bush Learn complete solos to eight traditional and orignal tunes, each one jam-packed with licks, runs, and musicalvariations. In previous chapters, you learned how to use SAX, DOM, JDOM, and JAXP to access this document. You could manipulate both its structure (the names and ordering of elements, attributes, and other lexical constructs) and its content (the actual data). However, many times you don’t need access to the document’s structure, and only want to work with its data. In this latter case, it’s overkill and a bit on the annoying side to have to write code that parses your document, extracts the data, and puts it into some format that you can use. It would be much nicer to run a program (or API. . . are you starting to get the picture here?) that did this for you, and produced usable Java classes. In fact, this is exactly what data binding does. With data binding, there are three distinct processes that can occur one after another, in differing order, or in completely unrelated processes. I’ll cover each in turn. 15.1.1 Class Generation The first process, class generation, provides a means to convert an XML document to Java. When data binding converts an XML document into a Java representation, it seeks to provide access to just the data in the document. Additionally, data binding provides some level of meaning to the information in the document. It does this by creating the Java representation with accessor and mutator2 methods like getItem( ) and setTeacher( ), instead of getElement( ) and setAttribute( ). This makes dealing with documents like the one in Example 15-1 less about Java and more about business logic, which is obviously a good thing. However, these wonderfully Java classes must exist before an XML document can be turned into an instance of one, so I’m back to class generation. Class generation is the process of taking a set of XML constraints and generating Java classes (and possibly interfaces) from those constraints. Think about it this way: XML constraints When I say “accessor,” I’m referring to what most people call a “getter” method; when I say “mutator,” I’m referring to what most people call a “setter” method. However, I also know that a “setter” is a dog, not a Java method, so I’m quick to tell my students not to use that term. Just an idiosyncrasy, I suppose!
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.


Item ID:
Item Level:
Title: