Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / tools / resources / serialization / xml / XMLReader.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/serialization/xml/XMLReader.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/serialization/xml/XMLReader.java
new file mode 100644 (file)
index 0000000..529dc2c
--- /dev/null
@@ -0,0 +1,275 @@
+// XMLReader.java\r
+// $Id: XMLReader.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.IOException;\r
+import java.io.Reader;\r
+\r
+import java.util.Vector;\r
+import java.util.Hashtable;\r
+import java.util.Stack;\r
+\r
+import org.xml.sax.AttributeList;\r
+import org.xml.sax.HandlerBase;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.Parser;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.SAXParseException;\r
+\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.AttributeHolder;\r
+import org.w3c.tools.resources.UnknownResource;\r
+import org.w3c.tools.resources.ResourceFrame;\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.SimpleAttribute;\r
+import org.w3c.tools.resources.ArrayAttribute;\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 XMLReader extends HandlerBase implements JigXML{\r
+\r
+    Parser          parser           = null;\r
+    AttributeHolder holders[]        = null;\r
+    Reader          reader           = null;\r
+    Stack           resourceSetStack = null;\r
+    Stack           resourceStack    = null;\r
+    Stack           defsStack        = null;\r
+    SimpleAttribute currentS         = null;\r
+    ArrayAttribute  currentA         = null;\r
+    Stack           faNameStack      = null;\r
+    int             length           = -1;\r
+    boolean         isavalue         = false;\r
+    String          array[]          = null;\r
+    int             arrayidx         = -1;\r
+    StringBuffer    characters       = null;\r
+    String          charvalue        = null;\r
+\r
+    public void startElement(String name, AttributeList attributes) \r
+       throws SAXException\r
+    {\r
+       endCharacters();\r
+       String iname = name.intern();\r
+       if (iname == iRESOURCE_TAG) {\r
+           String resourceclass = attributes.getValue(CLASS_ATTR);\r
+           try {\r
+                   //Added by Jeff Huang\r
+                   //TODO: FIXIT\r
+               Class c = Class.forName(resourceclass);\r
+               resourceStack.push(c.newInstance());\r
+           } catch (Exception ex) {\r
+               UnknownResource unknown = new UnknownResource();\r
+               resourceStack.push(unknown);\r
+           }\r
+           Hashtable defs = new Hashtable(5);\r
+           defsStack.push(defs);\r
+       } else if (iname == iATTRIBUTE_TAG) {\r
+           String attrclass = attributes.getValue(CLASS_ATTR);\r
+           try {\r
+               Class c = Class.forName(attrclass);\r
+               currentS = (SimpleAttribute) c.newInstance();\r
+               currentS.setName(attributes.getValue(NAME_ATTR));\r
+               currentS.setFlag(attributes.getValue(FLAG_ATTR));\r
+           } catch (Exception ex) {\r
+               ex.printStackTrace();\r
+               currentS = null;\r
+           }\r
+       } else if (iname == iARRAY_TAG) {\r
+           String attrclass = attributes.getValue(CLASS_ATTR);\r
+           try {\r
+               Class c  = Class.forName(attrclass);\r
+               currentA = (ArrayAttribute) c.newInstance();\r
+               currentA.setName(attributes.getValue(NAME_ATTR));\r
+               currentA.setFlag(attributes.getValue(FLAG_ATTR));\r
+               length   = Integer.parseInt(attributes.getValue(LENGTH_ATTR));\r
+               array    = new String[length];\r
+               arrayidx = 0;\r
+           } catch (Exception ex) {\r
+               ex.printStackTrace();\r
+               currentA = null;\r
+           }\r
+       } else if (iname == iRESARRAY_TAG) {\r
+           resourceSetStack.push(new Vector(10));\r
+           faNameStack.push(attributes.getValue(NAME_ATTR));\r
+       } else if (iname == iVALUE_TAG) {\r
+           isavalue = true;\r
+       }\r
+    }\r
+\r
+    public void endElement(String name) \r
+       throws SAXException\r
+    {\r
+       endCharacters();\r
+       String iname = name.intern();\r
+       if (iname == iRESOURCE_TAG) {\r
+           AttributeHolder res = (AttributeHolder)resourceStack.pop();\r
+           res.pickleValues((Hashtable)defsStack.pop());\r
+           Vector vresources = (Vector)resourceSetStack.peek();\r
+           vresources.addElement(res);\r
+       } else if (iname == iATTRIBUTE_TAG) {\r
+           currentS = null;\r
+       } else if (iname == iARRAY_TAG) {\r
+           Hashtable defs = (Hashtable)defsStack.peek();\r
+           Object value = currentA.unpickle(array);\r
+           if (value != null)\r
+               defs.put(currentA.getName(), value);\r
+           currentA = null;\r
+       } else if (iname == iRESARRAY_TAG) {\r
+           Hashtable defs = (Hashtable)defsStack.peek();\r
+           Vector vframes = (Vector) resourceSetStack.pop();\r
+           ResourceFrame frames[] = new ResourceFrame[vframes.size()];\r
+           vframes.copyInto(frames);\r
+           defs.put((String)faNameStack.pop(), frames);\r
+       } else if (iname == iVALUE_TAG) {\r
+           isavalue = false;\r
+       }\r
+    }\r
+\r
+    public void startDocument() \r
+       throws SAXException\r
+    {\r
+       defsStack        = new Stack();\r
+       faNameStack      = new Stack();\r
+       resourceStack    = new Stack();\r
+       resourceSetStack = new Stack();\r
+       resourceSetStack.push(new Vector(10));\r
+    }\r
+\r
+    public void endDocument() \r
+       throws SAXException\r
+    {\r
+       Vector vresources = (Vector) resourceSetStack.pop();\r
+       holders = new AttributeHolder[vresources.size()];\r
+       vresources.copyInto(holders);\r
+       try {\r
+           reader.close();\r
+       } catch (IOException ex) {\r
+           ex.printStackTrace();\r
+       }\r
+    }\r
+        \r
+    public void characters(char ch[],\r
+                          int start,\r
+                          int length) \r
+       throws SAXException\r
+    {\r
+       if (charvalue == null) {\r
+           charvalue = new String(ch, start, length);\r
+       } else if (length > 0) {\r
+           if (characters == null) {\r
+               characters = new StringBuffer(charvalue);\r
+           }\r
+           characters.append(ch, start, length);\r
+       }\r
+    }\r
+\r
+    private void endCharacters() {\r
+       String value;\r
+       if (charvalue == null) {\r
+           return;\r
+       }\r
+       if (characters == null) {\r
+           value = charvalue;\r
+       } else {\r
+           value = characters.toString();\r
+       }\r
+       characters = null;\r
+       charvalue  = null;\r
+       if (currentS != null) {\r
+           Hashtable defs = (Hashtable)defsStack.peek();\r
+           if (! value.equals(NULL)) {\r
+               defs.put(currentS.getName(), currentS.unpickle(value));\r
+           }\r
+       } else if ((currentA != null) && (isavalue)) {\r
+           array[arrayidx++] = value;\r
+       }\r
+    }\r
+\r
+    public void warning(SAXParseException e) \r
+       throws SAXException\r
+    {\r
+       System.out.println("WARNING in element "+e.getPublicId());\r
+       System.out.println("Sys  : "+e.getSystemId());\r
+       System.out.println("Line : "+e.getLineNumber());\r
+       System.out.println("Col  : "+e.getColumnNumber());\r
+       e.printStackTrace();\r
+    }\r
+\r
+    public void error(SAXParseException e) \r
+       throws SAXException\r
+    {\r
+       System.out.println("ERROR in element "+e.getPublicId());\r
+       System.out.println("Sys  : "+e.getSystemId());\r
+       System.out.println("Line : "+e.getLineNumber());\r
+       System.out.println("Col  : "+e.getColumnNumber());\r
+       e.printStackTrace();\r
+    }\r
+\r
+    public void fatalError(SAXParseException e) \r
+       throws SAXException\r
+    {\r
+       System.out.println("FATAL ERROR in element "+e.getPublicId());\r
+       System.out.println("Sys  : "+e.getSystemId());\r
+       System.out.println("Line : "+e.getLineNumber());\r
+       System.out.println("Col  : "+e.getColumnNumber());\r
+       e.printStackTrace();\r
+    }\r
+\r
+    protected void parse() \r
+       throws SAXException, IOException\r
+    {\r
+       try {\r
+           parser.setDocumentHandler(this);\r
+           parser.setErrorHandler(this);\r
+           parser.parse(new InputSource(reader));\r
+//     } catch (IOException ex) {\r
+//         try { reader.close(); } catch (IOException ioex) {}\r
+//         throw ex;\r
+//     } catch (SAXException sex) {\r
+//         try { reader.close(); } catch (IOException ioex) {}\r
+//         throw sex;\r
+       } finally {\r
+           try { reader.close(); } catch (IOException ioex) {}\r
+       }\r
+    }\r
+\r
+    public Resource[] readResources() \r
+       throws IOException, SerializationException\r
+    {\r
+       try {\r
+           parse();\r
+       } catch (SAXException ex) {\r
+           ex.printStackTrace();\r
+           return new Resource[0];\r
+       }\r
+       \r
+       int      len   = holders.length;\r
+       Resource crs[] = new Resource[len];\r
+       for (int i = 0 ; i < len ; i++)\r
+           crs[i] = (Resource) holders[i];\r
+       return crs;\r
+    }\r
+\r
+    public AttributeHolder[] readAttributeHolders() \r
+       throws IOException, SerializationException\r
+    {\r
+       try {\r
+           parse();\r
+       } catch (SAXException ex) {\r
+           ex.printStackTrace();\r
+           return new AttributeHolder[0];\r
+       }\r
+\r
+       return holders;\r
+    }\r
+\r
+    public XMLReader(Reader reader, Parser parser) {\r
+       this.reader = reader;\r
+       this.parser = parser;\r
+    }\r
+\r
+}\r