diff src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/Parser.java @ 3597:78c94d5d23fb

IdealGraphVisualizer: implement validation of graph documents against an XML schema. For now, validation errors are only printed to the console as warnings.
author Peter Hofer <peter.hofer@jku.at>
date Wed, 19 Oct 2011 15:11:33 +0200
parents fac58f1753a4
children 9fe4191f46af
line wrap: on
line diff
--- a/src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/Parser.java	Mon Oct 17 16:30:41 2011 +0200
+++ b/src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/Parser.java	Wed Oct 19 15:11:33 2011 +0200
@@ -38,9 +38,15 @@
 import com.sun.hotspot.igv.data.serialization.XMLParser.ParseMonitor;
 import com.sun.hotspot.igv.data.serialization.XMLParser.TopElementHandler;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.SchemaFactory;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;
@@ -378,7 +384,7 @@
                 if (fromIndexString != null) {
                     fromIndex = Integer.parseInt(fromIndexString);
                 }
-
+                
                 String toIndexString = readAttribute(TO_INDEX_PROPERTY);
                 if (toIndexString == null) {
                     toIndexString = readAttribute(TO_INDEX_ALT_PROPERTY);
@@ -495,7 +501,9 @@
     }
 
     // Returns a new GraphDocument object deserialized from an XML input source.
-    public synchronized GraphDocument parse(XMLReader reader, InputSource source, XMLParser.ParseMonitor monitor) throws SAXException {
+    public synchronized GraphDocument parse(InputSource source, XMLParser.ParseMonitor monitor) throws SAXException {
+        XMLReader reader = createReader();
+
         reader.setContentHandler(new XMLParser(xmlDocument, monitor));
         try {
             reader.parse(source);
@@ -505,4 +513,21 @@
 
         return topHandler.getObject();
     }
+
+    private XMLReader createReader() throws SAXException {
+        try {
+            SAXParserFactory pfactory = SAXParserFactory.newInstance();
+            pfactory.setValidating(false);
+            pfactory.setNamespaceAware(true);
+
+            // Enable schema validation
+            SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
+            InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
+            pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));
+
+            return pfactory.newSAXParser().getXMLReader();
+        } catch (ParserConfigurationException ex) {
+            throw new SAXException(ex);
+        }
+    }
 }