Process XML with
JavaBeans, Part 3 - JavaWorld January
2000
Tutorial Details:
Process XML with JavaBeans, Part 3
Process XML with JavaBeans, Part 3
By: By Mark Johnson
Simplify XML processing with XMLConvenience Beans
his final article in the XML JavaBeans series builds on the previous two parts. Part 1 discussed nonvisual JavaBeans that parse XML documents into DOM trees, and Part 2 explained how Integrated Development Environments (IDEs) interconnect JavaBeans with event and property change relationships. This month, I tie together those articles by developing a somewhat more complex (though still functionally trivial) XML editor. You'll see that as the application becomes more complex, the standard nonvisual XML JavaBeans quickly become more difficult to use. So, I explain how the XMLConvenience bean set simplifies creating XML GUI editors, with an example. I also cover a general Java programming topic: how to use interfaces to simulate multiple inheritance.
Process XML with JavaBeans: Read the whole series!
Part 1. Interconnect JavaBeans to process XML
Part 2. How IDEs interconnect components
Part 3. Simplify XML processing with XMLConvenience Beans
Building a better editor
The result of last month's article was an XML editor that edited an XML document containing a single tag, . Though the little application provided a good example of how IDEs wire applications together with event relationships, I think the result was disappointing. I certainly wasn't hurrying home from work every night to play with the Recipe Name Editor. Figure 1 shows the extensions I've created to make the application more useful. I call this improved Recipe Editor the ComplicatedRecipeEditor .
Figure 1. ComplicatedRecipeEditor, a (slightly) improved Recipe Editor
I'm making three extensions to last month's editor. I start by adding a tag (indicated by the letter A in Figure 1). Second, I add a Save File button ( B ), which saves the editor's DOM document as an XML file. Third, I add a status bar for error messages ( C ), which also indicates the name of the file being edited.
I'll do all of this the hard way first, using nonvisual beans and connecting them to standard AWT (Abstract Windowing Toolkit) interface components. The result will be a tangled web of event and property relationships. Then I'll show you how to use XMLConvenience beans to create the same application with just a few objects and connections.
Adding the Description tag
Figure 2 shows last month's application. I'm going to add a TextArea so you can edit a recipe's description as well as its name.
Figure 2. Last month's Recipe Editor application
Figure 3 shows the additional components and connections that I made to generate a tag to the editor. Don't be intimidated by Figure 3's complexity. (My apologies for the event and property connection lines overlapping the object labels. That's how VisualAge for Java's Visual Composition Editor draws things.)
The only additions are that of another ElementContainer ( F in Figure 3), which holds the element, and a TextContainer ( H ), which holds the text inside the .
Figure 3. Adding a tag to the editor
Take a look at the objects underneath the DocumentContainer1 object ( C ) in Figure 3. DocumentContainer1 has an ElementContainer called ECFindRecipe , which itself has two ElementContainer s, ECFindName and ECFindDesc , each of which has a TextContainer .
This structure of XML JavaBeans objects exactly reflects the structure of the DOM tree it represents, as Figure 4 illustrates. XML JavaBeans container classes are designed to can contain any structure that can be described by an XML DTD (document type definition). XML JavaBeans programmers can then connect these containers to other objects, such as GUI components, to do useful work.
Figure 4. DOM tree and XML JavaBeans container structures are similar
A number of readers have been a bit confused about what the labels in the diagram refer to: classes or instances. Each object label in the diagram is the name of the instance, and it is the value of that instance's beanName property. VisualAge for Java makes up instance names by adding a number to the class name (as in the case of DocumentContainer1 ). In some cases (like DOMGenerator ), I changed the instance name to the class name. It would be like naming my dog "Dog." Just remember that all of the objects in the diagram are instances, not classes, and the whole discussion will make a lot more sense.
The class diagrammed in Figure 3 works by objects passing events and values to one another. A look at the diagram may not make it clear how these DOM elements are "passed" from one XML JavaBean to another as I've been describing. It's really quite simple. The connection between the output of one XML JavaBean and the input of another is a bound property relationship. A bound property is a property whose value is set to track some other property of an object. In this case, the result property of each XML JavaBean is bound to the input property of another bean. When the first bean produces a result, it sets its result property, and the property relationship (by way of a PropertyChange event) updates the other bean's input to that same value. It is as if the first bean passed its result to the second bean.
The class whose structure is displayed in Figure 3 works almost the same as RecipeEditor did in last month's article. When a user clicks the Open File button, the DOMGenerator ( B in Figure 3) acquires an input filename from ExtendedFileDialog and produces a DOM tree that represents the XML document in the file. DocumentContainer1 ( C ), which receives the DOM tree as input, passes any element it finds at the top level of the tree through to its output. The element in DocumentContainer1 is passed to an ElementContainer called ECFindRecipe ( D ), whose outputs are in turn connected to two other ElementContainer s, ECFindName ( E ) and ECFindDesc ( F ). These containers search for the and nodes (respectively) of the Recipe that contains them. Connected to the output of those last two containers are the TextContainer objects NameContainer ( G ) and DescContainer ( H ), which contain Text nodes that hold the text values of the recipe's name and description. The content of the two Text nodes is edited in the GUI.
Now that the editor can add descriptions, it's time to add the status bar.
Adding a status bar to RecipeEditor
Figure 5 shows the ComplicatedRecipeEditor , now with a status bar( A ). The status bar itself is simply a java.awt.Label with a navy blue background added to the application frame.
Figure 5. A status bar is added to ComplicatedRecipeEditor
The Label 's text property controls the displayed text. Each of the event connections in Figure 5 sets this text to some value. DOMGenerator ( B ) has three event connections that may set the status Label 's text:
Connection C : DOMGenerator 's generationStarted event, which DOMGenerator produces before it starts parsing, is connected to set the Label text to the string "Started parsing..." .
Connection D : DOMGenerator 's generationError event is connected to set the Label text to the string "Parse error!" when a parse error occurs.
Connection E : DOMGenerator 's generationOver event, produced when DOMGenerator completes a successful parse, sets the Label text to the name of the file being edited. The link F attached to ExtendedFileDialog ( G ) supplies the name of the file as an argument to connection E .
By using the connections described above, DOMGenerator reports its status to the user. It would be nice to extract the error message from DOMGenerator and display it in the status bar; unfortunately, there is no easy way to do so from this IDE, since the error itself is expressed as an event.
The final improvement to the application is the addition of a Save To feature.
Saving the edited document
The implementation of the Save File function is straightforward. As you may remember from Part 1 of this series, the XMLFileGenerator 's XMLCore bean turns a DOM tree into an XML file and writes it to the file system. In other words, it performs the inverse operation of the DOMGenerator class. Figure 6 shows an XMLFileGenerator bean ( B ) added to the application, and the event relationships connected to it that control its operation.
Figure 6. The Save File feature
When the user clicks the Save File button, the button's actionPerformed() method calls the XMLFileGenerator 's triggerAction method. triggerAction tells XMLFileGenerator to write the DOM document at its inputDocument property to the file whose name is the value of its xmlSaveLocation property.
Note that the XMLFileGenerator 's autoAction property is set to false (though this is not visible on the diagram in Figure 6). autoAction , when true , causes the XMLFileGenerator to write its output file immediately whenever its inputDocument property is set to any DOM structure. autoAction is false in this example because if it weren't, the XMLFileGenerator would try to write a file every time a successful parse occurred. The application should write a file only when the user indicates that it should (by clicking the Save File button.)
Before XMLFileGenerator processes its input, it emits a fetchArguments event, which is used to set up the generator's inputs. This event is used to set both the output filename and the input document for the XMLFileGenerator .
To set the output filename, connection D hooks the fetchArguments event to the show() method of the ExtendedFileDialog ( C ), causing the dialog to present a file selection box to the user. When the user selects a file, the ExtendedFileDialog sets its fullpath property to the pathname of the selected file. Connection E is a property change relationship that updates the XMLFileGenerator 's xmlSaveLocation property, setting it to the selected file pathname.
Connection F is also an event relationship inv
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Process XML with
JavaBeans, Part 3 - JavaWorld January
2000
View Tutorial: Process XML with
JavaBeans, Part 3 - JavaWorld January
2000
Related
Tutorials:
XML messaging, Part
3
XML messaging, Part
3 |
Boost Struts with
Boost Struts with XSLT and XML |
Will Big Blue
eclipse the Java
tools market?
Will Big Blue
eclipse the Java
tools market? |
Create your own type 3 JDBC driver, Part 2
Create your own type 3 JDBC driver, Part 2 |
Rumble in the
jungle: J2EE versus .Net, Part
1
Rumble in the
jungle: J2EE versus .Net, Part
1 |
Jabber away with instant
messaging
Jabber away with instant
messaging |
Business process
automation
made easy with
Java, Part 1
Business process
automation
made easy with
Java, Part 1 |
Yes, you can secure your Web services documents, Part 2
Yes, you can secure your Web services documents, Part 2 |
J2SE 1.4
breathes new life into the CORBA community, Part
3
J2SE 1.4
breathes new life into the CORBA community, Part
3 |
Sun boosts
Sun boosts enterprise Java |
Get the inside
track on J2EE architect certification
Get the inside
track on J2EE architect certification |
Servlet 2.4:
What's in store
Servlet 2.4:
What's in store |
Axis-orizing objects for SOAP
Axis-orizing objects for SOAP |
The J2EE 1.4 Tutorial
The J2EE 1.4 Tutorial is a guide to developing enterprise applications for the Java 2 Platform, Enterprise Edition (J2EE) version 1.4. Here we cover all the things you need to know to make the best use of this tutorial. |
Finally, getting hands in !
Finally, getting hands in ! |
Impressive
!
Impressive
! |
JSP 2.0: The New Deal, Part 3
JSP 2.0: The New Deal, Part 3
More Flexible JSP Document Format Rules
The JSP specification supports two types of JSP pages: regular JSP pages containing any type of text or markup, and JSP Documents, which are well-formed XML documents; i.e., docum |
Parsing and Processing Large XML Documents with Digester Rules
Parsing and Processing Large XML Documents with Digester Rules
XML is commonly used for integration with third-party applications or web services, especially those that are running on non-Java platforms. On the other hand, if the code is running in a man |
What is Persistence Framework?
What is Persistence Framework?
What is Persistence Framework?
A persistence framework moves the program data in its most natural form (in memory objects) to and from a permanent data store the database. The persistence framework manages the |
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Distributed Architecture
Two-tier application:
In the past two-tier applications were used. Two-tier applications are also know as |
|
|
|