001: // XMLSerializer.java
002: // $Id: XMLSerializer.java,v 1.9 2007/02/28 12:24:03 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.resources.serialization.xml;
006:
007: import java.io.Writer;
008: import java.io.Reader;
009: import java.io.IOException;
010:
011: import org.w3c.util.LookupTable;
012:
013: import org.xml.sax.Parser;
014:
015: import org.w3c.tools.resources.Resource;
016: import org.w3c.tools.resources.AttributeHolder;
017: import org.w3c.tools.resources.serialization.Serializer;
018: import org.w3c.tools.resources.serialization.ResourceDescription;
019: import org.w3c.tools.resources.serialization.SerializationException;
020:
021: /**
022: * @version $Revision: 1.9 $
023: * @author Benoît Mahé (bmahe@w3.org)
024: */
025: public class XMLSerializer implements Serializer, JigXML {
026:
027: public static final String PARSER_P1 = "com.bluecast.xml.Piccolo";
028: public static final String PARSER_P2 = "com.jclark.xml.sax.Driver";
029: public static final String PARSER_P3 = "org.apache.xerces.parsers.SAXParser";
030:
031: protected Class parser_class = null;
032: protected String parser_name = null;
033:
034: /**
035: * Write the resource descriptions using the given writer.
036: * @param descr the resource descriptions array
037: * @param writer the writer
038: */
039: public void writeResourceDescriptions(ResourceDescription descr[],
040: Writer writer) throws IOException, SerializationException {
041: XMLDescrWriter xmlwriter = null;
042: try {
043: xmlwriter = new XMLDescrWriter(writer);
044: //XML headers
045: xmlwriter.startDocument();
046: //dump the resources in XML...
047: for (int i = 0; i < descr.length; i++) {
048: xmlwriter.writeResourceDescription(descr[i]);
049: }
050: } finally {
051: if (xmlwriter != null) {
052: xmlwriter.closeDocument();
053: }
054: }
055: }
056:
057: /**
058: * Write the resource descriptions using the given writer.
059: * @param descr the resource array
060: * @param writer the writer
061: */
062: public void writeResourceDescriptions(Resource descr[],
063: Writer writer) throws IOException, SerializationException {
064: XMLResourceWriter xmlwriter = null;
065: try {
066: xmlwriter = new XMLResourceWriter(writer);
067: //XML headers
068: xmlwriter.startDocument();
069: //dump the resources in XML...
070: for (int i = 0; i < descr.length; i++) {
071: xmlwriter.writeResourceDescription(descr[i]);
072: }
073: } finally {
074: if (xmlwriter != null) {
075: xmlwriter.closeDocument();
076: }
077: }
078: }
079:
080: /**
081: * Write the resources using the given writer.
082: * @param descr the resource array
083: * @param writer the writer
084: */
085: public void writeResources(AttributeHolder holders[], Writer writer)
086: throws IOException, SerializationException {
087: XMLResourceWriter xmlwriter = null;
088: try {
089: xmlwriter = new XMLResourceWriter(writer);
090: //XML headers
091: xmlwriter.startDocument();
092: //dump the holders in XML...
093: for (int i = 0; i < holders.length; i++) {
094: xmlwriter.writeResource(holders[i]);
095: }
096: //close the writer;
097: } finally {
098: if (xmlwriter != null) {
099: xmlwriter.closeDocument();
100: }
101: }
102: }
103:
104: protected Parser getParser() throws SerializationException {
105: try {
106: return (Parser) parser_class.newInstance();
107: } catch (Exception ex) {
108: throw new SerializationException("Unable to intantiate : "
109: + parser_name);
110: }
111: }
112:
113: /**
114: * Read the resource descriptions using the given reader.
115: * @param writer the reader
116: * @return a ResourceDescription array
117: */
118: public ResourceDescription[] readResourceDescriptions(Reader reader)
119: throws IOException, SerializationException {
120: Parser parser = getParser();
121: XMLDescrReader xmlreader = new XMLDescrReader(reader, parser);
122: return xmlreader.readResourceDescriptions();
123: }
124:
125: /**
126: * Read the resources using the given reader.
127: * @param writer the reader
128: * @return a Resources array
129: */
130: public Resource[] readResources(Reader reader) throws IOException,
131: SerializationException {
132: Parser parser = getParser();
133: XMLReader xmlreader = new XMLReader(reader, parser);
134: return xmlreader.readResources();
135: }
136:
137: /**
138: * Read the attribute holders using the given reader.
139: * @param writer the reader
140: * @return a Resources array
141: */
142: public AttributeHolder[] readAttributeHolders(Reader reader)
143: throws IOException, SerializationException {
144: Parser parser = getParser();
145: XMLReader xmlreader = new XMLReader(reader, parser);
146: return xmlreader.readAttributeHolders();
147: }
148:
149: /**
150: * Load only some attributes
151: * @param attributes the attribute names array.
152: */
153: public LookupTable[] readAttributes(Reader reader,
154: String attributes[]) throws IOException,
155: SerializationException {
156: Parser parser = getParser();
157: XMLSubsetReader xmlreader = new XMLSubsetReader(reader, parser,
158: attributes);
159: return xmlreader.readAttributeTables();
160: }
161:
162: public XMLSerializer() {
163: try {
164: parser_class = Class.forName(PARSER_P1);
165: parser_name = PARSER_P1;
166: } catch (ClassNotFoundException pex) {
167: try {
168: parser_class = Class.forName(PARSER_P2);
169: parser_name = PARSER_P2;
170: } catch (ClassNotFoundException ppex) {
171: try {
172: parser_class = Class.forName(PARSER_P3);
173: parser_name = PARSER_P3;
174: } catch (ClassNotFoundException pppex) {
175: parser_class = null;
176: }
177: }
178: }
179: }
180: }
|