# HG changeset patch # User Peter Hofer # Date 1319029893 -7200 # Node ID 78c94d5d23fb21c22aad08d6e67d4ff62cd31e42 # Parent 3a0a2601137168e1c80c2a63d8cb935ba581ac12 IdealGraphVisualizer: implement validation of graph documents against an XML schema. For now, validation errors are only printed to the console as warnings. diff -r 3a0a26011371 -r 78c94d5d23fb src/share/tools/IdealGraphVisualizer/Coordinator/src/com/sun/hotspot/igv/coordinator/actions/ImportAction.java --- a/src/share/tools/IdealGraphVisualizer/Coordinator/src/com/sun/hotspot/igv/coordinator/actions/ImportAction.java Mon Oct 17 16:30:41 2011 +0200 +++ b/src/share/tools/IdealGraphVisualizer/Coordinator/src/com/sun/hotspot/igv/coordinator/actions/ImportAction.java Wed Oct 19 15:11:33 2011 +0200 @@ -47,10 +47,8 @@ import org.openide.util.NbBundle; import org.openide.util.RequestProcessor; import org.openide.util.actions.CallableSystemAction; -import org.openide.xml.XMLUtil; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; /** * @@ -88,7 +86,6 @@ Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); try { - final XMLReader reader = XMLUtil.createXMLReader(); final FileInputStream inputStream = new FileInputStream(file); final InputSource is = new InputSource(inputStream); @@ -123,7 +120,7 @@ public void run() { GraphDocument document = null; try { - document = parser.parse(reader, is, parseMonitor); + document = parser.parse(is, parseMonitor); parseMonitor.setState("Finishing"); component.getDocument().addGraphDocument(document); } catch (SAXException ex) { @@ -140,8 +137,6 @@ } }); - } catch (SAXException ex) { - ex.printStackTrace(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { diff -r 3a0a26011371 -r 78c94d5d23fb src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/Parser.java --- 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); + } + } } diff -r 3a0a26011371 -r 78c94d5d23fb src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/graphdocument.xsd --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/serialization/graphdocument.xsd Wed Oct 19 15:11:33 2011 +0200 @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 3a0a26011371 -r 78c94d5d23fb src/share/tools/IdealGraphVisualizer/Data/test/unit/src/com/sun/hotspot/igv/data/serialization/ParserTest.java --- a/src/share/tools/IdealGraphVisualizer/Data/test/unit/src/com/sun/hotspot/igv/data/serialization/ParserTest.java Mon Oct 17 16:30:41 2011 +0200 +++ b/src/share/tools/IdealGraphVisualizer/Data/test/unit/src/com/sun/hotspot/igv/data/serialization/ParserTest.java Wed Oct 19 15:11:33 2011 +0200 @@ -40,11 +40,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.openide.xml.XMLUtil; import static org.junit.Assert.*; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; /** * @@ -84,9 +82,8 @@ InputSource is = new InputSource(sr); try { - XMLReader reader = XMLUtil.createXMLReader(); Parser parser = new Parser(); - final GraphDocument parsedDocument = parser.parse(reader, is, null); + final GraphDocument parsedDocument = parser.parse(is, null); Util.assertGraphDocumentEquals(document, parsedDocument); } catch (SAXException ex) { fail(ex.toString()); diff -r 3a0a26011371 -r 78c94d5d23fb src/share/tools/IdealGraphVisualizer/NetworkConnection/src/com/sun/hotspot/igv/connection/Client.java --- a/src/share/tools/IdealGraphVisualizer/NetworkConnection/src/com/sun/hotspot/igv/connection/Client.java Mon Oct 17 16:30:41 2011 +0200 +++ b/src/share/tools/IdealGraphVisualizer/NetworkConnection/src/com/sun/hotspot/igv/connection/Client.java Wed Oct 19 15:11:33 2011 +0200 @@ -33,10 +33,8 @@ import java.net.Socket; import javax.swing.JTextField; import org.openide.util.Exceptions; -import org.openide.xml.XMLUtil; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; /** * @@ -65,9 +63,8 @@ InputSource is = new InputSource(inputStream); try { - XMLReader reader = XMLUtil.createXMLReader(); Parser parser = new Parser(this); - parser.parse(reader, is, null); + parser.parse(is, null); } catch (SAXException ex) { ex.printStackTrace(); }