org.netbeans.modules.cnd.api.xml

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » cnd » org.netbeans.modules.cnd.api.xml 
org.netbeans.modules.cnd.api.xml
Overview of XML package.

Terms

codec Abbreviation for Coder/Decoder terminal node A XML node of the form <tag>content</tag>

Goals

R1 XML writing utility. SAX parses but there's no utiltity to help with indentation, attributes, escaping and XML document headers and footers. R2 codec to be separate from actual object. Alternative schemes involve serializable objects to implement a particular codec interface. Separate codecs are good for these reasons: - Allows us to write codecs for classes which we can't modify. - Decoding typically benefits from class extension. But extending a decoder class might conflict with the object classes existing inheritance. Sure, one can use interfaces and then create "Support" classes to be delagated to, but that becomes increasingly inconvenient. R3 codecs to not be associated with documents. We might have an XML document which contains <breakpoints> at the top level. Some other day we might want to embed the <breakpoints> as part of <profile> in another document. The <breakpoints> codec should not have to know about whether it's in a document or where it is embedded. R4 Single codec class Not have to write a separate class for encoding and another for decoding. This will allow the encapsulation of all XML-specific codecing, primarily tag and attribute names, info in one class. R* tags to driver object construction [ tentative ]

Motivational history

- While developing SunStudio we found that we often migrated where colelctions of data are stored (both in mmeory and under persistence) and adapting to distinct XML persistence infrastructures was a PIB. From this we get R3. - The cnd makeproject introduced the notion of dispatching the parsing and writing of sub-systems to ... the sub-systems via the so-called ConfigurationAuxObject interface. But this mechanism was hard-coded in ConfigurationDescriptorHelper and no-one else could benefit from it. That implementation was a first-approximation solution to R3 and in this package we generalize it. - A large amount of copy-pasted code, especially in the area of writing out XML, would drop various features: - Proper XML encoding attribute (sometimes it needs to be other than "UTF-8". - Inconsistent quoting. Attribute value quoting, via XMLUtil.toAttributeValue was used only in some places. Content coding, via XMLUtil.toElementContent wasn't used _anywhere_. - Lot of code relied on converting to XML strings before passing them on to other places. Having Codecs just write to a stream is more efficient.

Example

In the following example Methods with empty definitions are elided for brevity although in actual code they need to be implemented to satisfy interfaces. Consider data which would be stored as follows:
	<family>
		<father>
			<person firstName="X"
				lastName="L"
				gender="M"/>
		</father>
		<mother>
			<person firstName="Y"
				lastName="L"
				gender="F"/>
		</mother>
		<children>
			<person firstName="Z"
				lastName="L"
				gender="M"/>
		</children>
	</family>
based on
	class Family {
		Person mother;
		Person father;
		Vector<Person> children;
	}
Let's see how one would write this out.

	FamilyXMLDocWriter writer = new FamilyXMLDocWriter(family);
	writer.setIndentChars(4);	// as opposed to default of 2
	writer.write(new File("family.html");


	FamilyXMLDocWriter extends XMLDocWriter {

		FamilyXMLCodec codec;

		FamilyXMLDocWriter(Family family) {
			codec = new FamilyXMLCodec(family);
		}

		public void write(File file) {
			OutputStream os = file.();
			write(os);
		}


		// interface XMLEncoder
		public void encode(XMLEncoderStream xes) {
			codec.encode(xes);
		}
	}

	FamilyXMLCodec extends XMLDecoder implements XMLEncoder {

		private Family family		// being written out
		private PersonXMLCodec personCodec;
		private ChildrenXMLCodec childrenCodec;

		FamilyXMLCodec(Family family) {
			this.family = family;
			personCodec = new PersonXMLCodec();
			childrenCodec = new ChildrenXMLCodec(family.children);
		}

		// interface XMLDecoder
		protected String tag() {
			return "family"
		}

		// interface XMLEncoder
		public void encode(XMLEncoderStream xes) {
			xes.elementOpen(tag());
				xes.elementOpen("father");
					personCodec.setPerson(father);
					personCodec.encode(xes)
				xes.elementClose("father");

				xes.elementOpen("mother");
					personCodec.setPerson(mother);
					personCodec.encode(xes)
				xes.elementClose("mother");

				childrenCodec.encode(xes);
			xes.elementClose(tag());
		}
	}

	ChildrenXMLCodec extends XMLDecoder implements XMLEncoder {

		private Vector<PErson> children;
		private PersonXMLCodec personCodec;

		ChildrenXMLCodec(Vector<Person> children) {
			this.children = children;
			personCodec = new PersonXMLCodec();
		}

		// interface XMLDecoder
		protected String tag() {
			return "children"
		}

		// interface XMLEncoder
		public void encode(XMLEncoderStream xes) {
			xes.elementOpen(tag());
			for (Person p in children) {
				personCodec.setPerson(p);
				personCodec.encode(xes);
			}
			xes.elementClose(tag());
		}
	}

	PersonXMLCodec extends XMLDecoder implements XMLEncoder {

		private Person person;
		private Vector<Person> list;

		PersonXMLCodec(Person person {
			this.person = person;
		}

		PersonXMLCodec(Vector<Person> list) {
			this.list = list;
		}

		public void setPerson(Person person) {
			this.person = person;
		}

		// interface XMLDecoder
		protected String tag() {
			return "person"
		}

		// interface XMLEncoder
		public void encode(XMLEncoderStream xes) {
			AttrValuePair attrs[] = new AttrValuePair[] {
				new AttrValuePair("firstName",
						  person.firstName);
				new AttrValuePair("lastName",
						  person.lastName);
				new AttrValuePair("gender",
						  person.gender);
			}
			xes.element(tag(), attrs);
		}
	}
Now let's see how one would read it in.

These are the same classes as above but with the encoding related code taken out and only decoding code appearing.

	FamilyXMLDocReader writer = new FamilyXMLDocReader(family);
	writer.write(new File("family.html");


	FamilyXMLDocReader extends XMLDocReader {

		FamilyXMLCodec codec;

		FamilyXMLDocReader(Family family) {
			codec = new FamilyXMLCodec(family);
			registerXMLDecoder(codec);
		}

		public void write(File file) {
			InputStream is = file.();
			String what = "family"
			read(is, what);
		}
	}

	FamilyXMLCodec extends XMLDecoder implements XMLEncoder {

		private Family family
		private PersonXMLCodec personCodec;
		private ChildrenXMLCodec childrenCodec;

		FamilyXMLCodec(Family family) {
			this.family = family;

			personCodec = new PersonXMLCodec();
			registerXMLEncoder(personCodec);

			childrenCodec = new ChildrenXMLCodec(family.children);
			registerXMLEncoder(childrenCodec);
		}

		// interface XMLDecoder
		protected String tag() {
			return "family"
		}

		// interface XMLDecoder
		protected void startElement(String name, Attributes atts) {

			// personCode.start() will automatically be called when
			//  is seen due to the above registration.
			// Here we just ensure that we decode into the 
			// right Person instance

			if (name.equals("mother")) {
				family.mother = new Person();
				personCodec.setPerson(family.mother);
			}
			else if (name.equals("father")) {
				family.father = new Person();
				personCodec.setPerson(family.father);
			}

			// children handled by registration
		}
	}

	/**
	 * For decosing children we cheat a bit.
	 * We instantiate and register a version of PersonXMLCodec which
	 * takes a container (Vector) to stuff new Persons into.
	 */
	ChildrenXMLCodec extends XMLDecoder implements XMLEncoder {

		private Vector<PErson> children;
		private PersonXMLCodec personCodec;

		ChildrenXMLCodec(Vector<Person> children) {
			this.children = children;

			personCodec = new PersonXMLCodec(children);
			registerXMLEncoder(personCodec);
		}

		// interface XMLDecoder
		protected String tag() {
			return "children"
		}
	}

	PersonXMLCodec extends XMLDecoder implements XMLEncoder {

		private Person person;
		private Vector<Person> list;

		PersonXMLCodec(Person person {
			this.person = person;
		}

		/**
		 * Form used when we delay setting the person to be decoded
		 * into til setPerson().
		 */
		PersonXMLCodec(Person person {
			this.person = null;
		}

		/**
		 * Form used when we create Persons, decode them and add them
		 * to 'list'
		 */
		PersonXMLCodec(Vector<Person> list) {
			this.list = list;
		}

		public void setPerson(Person person) {
			this.person = person;
		}

		// interface XMLDecoder
		protected String tag() {
			return "person"
		}

		// interface XMLDecoder
		protected void start(Attributes atts) {
			Person newPerson = person;
			if (!newPerson)
				newPerson = new Person();
			person.firstName = atts.getValue("firstName");
			person.familyName = atts.getValue("familyName");
			person.gender = atts.getValue("gender");
			if (list != null)
				list.add(newPerson);
		}
	}
Java Source File NameTypeComment
AttrValuePair.javaClass Means of passing XML attribute/value pairs to XMLEncoderStream .
AttrValuePairs.javaClass Utility class for constructing AttrValuePair s to be passed to XMLEncoderStream .
Catalog.javaClass
VersionException.javaClass Thrown by XMLDecoder.checkVersion if a version mismatch is detected.
XMLDecoder.javaClass Receive notification of the content of an XML element named via XMLDecoder.tag .
XMLDocReader.javaClass Drive the reading of and receive notification of the content of an XML document.
XMLDocWriter.javaClass Drive the writing of an XML document.
XMLEncoder.javaInterface An XMLEncoder should be implemented by a subclass, which would typically also extend XMLDecoder to create a codec.
XMLEncoderStream.javaClass Provides convenience methods to write out XML elements.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.