Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / tools / resources / serialization / xml / XMLSerializer.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/serialization/xml/XMLSerializer.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/serialization/xml/XMLSerializer.java
new file mode 100644 (file)
index 0000000..26ecf6a
--- /dev/null
@@ -0,0 +1,193 @@
+// XMLSerializer.java\r
+// $Id: XMLSerializer.java,v 1.2 2010/06/15 17:53:01 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 1999.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+package org.w3c.tools.resources.serialization.xml;\r
+\r
+import java.io.Writer;\r
+import java.io.Reader;\r
+import java.io.IOException;\r
+\r
+import org.w3c.util.LookupTable;\r
+\r
+import org.xml.sax.Parser;\r
+\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.AttributeHolder;\r
+import org.w3c.tools.resources.serialization.Serializer;\r
+import org.w3c.tools.resources.serialization.ResourceDescription;\r
+import org.w3c.tools.resources.serialization.SerializationException;\r
+\r
+/**\r
+ * @version $Revision: 1.2 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class XMLSerializer implements Serializer, JigXML {\r
+\r
+\r
+    public static final String PARSER_P1 = "com.bluecast.xml.Piccolo";\r
+    public static final String PARSER_P2 = "com.jclark.xml.sax.Driver";\r
+    public static final String PARSER_P3 = \r
+                                         "org.apache.xerces.parsers.SAXParser";\r
+\r
+    protected Class parser_class = null;\r
+    protected String parser_name = null;\r
+\r
+    /**\r
+     * Write the resource descriptions using the given writer.\r
+     * @param descr the resource descriptions array\r
+     * @param writer the writer\r
+     */\r
+    public void writeResourceDescriptions(ResourceDescription descr[],\r
+                                         Writer writer)\r
+       throws IOException, SerializationException\r
+    {\r
+       XMLDescrWriter xmlwriter = null;\r
+       try {\r
+           xmlwriter = new XMLDescrWriter(writer);\r
+           //XML headers\r
+           xmlwriter.startDocument();\r
+           //dump the resources in XML...\r
+           for (int i = 0 ; i < descr.length ; i++) {\r
+               xmlwriter.writeResourceDescription(descr[i]);\r
+           }\r
+       } finally {\r
+           if (xmlwriter != null) {\r
+               xmlwriter.closeDocument();\r
+           }\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Write the resource descriptions using the given writer.\r
+     * @param descr the resource array\r
+     * @param writer the writer\r
+     */\r
+    public void writeResourceDescriptions(Resource descr[],\r
+                                         Writer writer)\r
+       throws IOException, SerializationException\r
+    {\r
+       XMLResourceWriter xmlwriter = null;\r
+       try {\r
+           xmlwriter = new XMLResourceWriter(writer);\r
+           //XML headers\r
+           xmlwriter.startDocument();\r
+           //dump the resources in XML...\r
+           for (int i = 0 ; i < descr.length ; i++) {\r
+               xmlwriter.writeResourceDescription(descr[i]);\r
+           }\r
+       } finally {\r
+           if (xmlwriter != null) {\r
+               xmlwriter.closeDocument();\r
+           }\r
+       }\r
+    }\r
+    /**\r
+     * Write the resources using the given writer.\r
+     * @param descr the resource array\r
+     * @param writer the writer\r
+     */\r
+    public void writeResources(AttributeHolder holders[], Writer writer) \r
+       throws IOException, SerializationException\r
+    {\r
+       XMLResourceWriter xmlwriter = null;\r
+       try {\r
+           xmlwriter = new XMLResourceWriter(writer);\r
+           //XML headers\r
+           xmlwriter.startDocument();\r
+           //dump the holders in XML...\r
+           for (int i = 0 ; i < holders.length ; i++) {\r
+               xmlwriter.writeResource(holders[i]);\r
+           }\r
+           //close the writer;\r
+       } finally {\r
+           if (xmlwriter != null) {\r
+               xmlwriter.closeDocument();\r
+           }\r
+       }\r
+    }\r
+\r
+    protected Parser getParser() \r
+       throws SerializationException\r
+    {\r
+       try {\r
+           return (Parser) parser_class.newInstance();\r
+       } catch (Exception ex) {\r
+           throw new SerializationException("Unable to intantiate : "+\r
+                                            parser_name);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Read the resource descriptions using the given reader.\r
+     * @param writer the reader\r
+     * @return a ResourceDescription array\r
+     */\r
+    public ResourceDescription[] readResourceDescriptions(Reader reader) \r
+       throws IOException, SerializationException\r
+    {\r
+       Parser         parser    = getParser();\r
+       XMLDescrReader xmlreader = new XMLDescrReader(reader, parser);\r
+       return xmlreader.readResourceDescriptions();\r
+    }\r
+\r
+    /**\r
+     * Read the resources using the given reader.\r
+     * @param writer the reader\r
+     * @return a Resources array\r
+     */\r
+    public Resource[] readResources(Reader reader) \r
+       throws IOException, SerializationException\r
+    {\r
+       Parser    parser    = getParser();\r
+       XMLReader xmlreader = new XMLReader(reader, parser);\r
+       return xmlreader.readResources();\r
+    }\r
+\r
+    /**\r
+     * Read the attribute holders using the given reader.\r
+     * @param writer the reader\r
+     * @return a Resources array\r
+     */\r
+    public AttributeHolder[] readAttributeHolders(Reader reader) \r
+       throws IOException, SerializationException\r
+    {\r
+       Parser    parser    = getParser();\r
+       XMLReader xmlreader = new XMLReader(reader, parser);\r
+       return xmlreader.readAttributeHolders();\r
+    } \r
+\r
+    /**\r
+     * Load only some attributes\r
+     * @param attributes the attribute names array.\r
+     */\r
+    public LookupTable[] readAttributes(Reader reader, String attributes[]) \r
+       throws IOException, SerializationException\r
+    {\r
+       Parser parser = getParser();\r
+       XMLSubsetReader xmlreader = \r
+           new XMLSubsetReader(reader, parser, attributes);\r
+       return xmlreader.readAttributeTables();\r
+    }\r
+\r
+    public XMLSerializer() {\r
+       try {\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+           parser_class = Class.forName(PARSER_P1);\r
+           parser_name = PARSER_P1;\r
+       } catch (ClassNotFoundException pex) {\r
+           try {\r
+               parser_class = Class.forName(PARSER_P2);\r
+               parser_name = PARSER_P2;\r
+           } catch (ClassNotFoundException ppex) {     \r
+               try {\r
+                   parser_class = Class.forName(PARSER_P3);\r
+                   parser_name = PARSER_P3;\r
+               } catch (ClassNotFoundException pppex) {\r
+                   parser_class = null;\r
+               }\r
+           }\r
+       }\r
+    }\r
+}\r