S
steven.bagnall
Hi,
Have a java application that attempts to create a DOM document from an
XML f
ile by validating against an XSD which is defined in the code. The
RuntimeE
xception, IllegalStateException is raised on the line:
document = builder.parse(new File(argv[0]));
If you validate the XML with the XSD using say an online validation
utility
it validates fine. I have posted the stack trace, and the Java file
below.
Thanks in advance.
Steve.
XMLDOM.JAVA ============================
package com.churchillchina.utils;
import ...
public class XMLDOM extends JPanel {
static Document document;
static final int windowHeight = 460;
static final int leftWidth = 300;
static final int rightWidth = 340;
static final int windowWidth = leftWidth + rightWidth;
private static PrintWriter m_ErrLog;
public XMLDOM() {
// Set up the tree
JTree tree = new JTree();
// Build left-side view
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(new Dimension( leftWidth, windowHeight ));
// Build right-side view
JEditorPane htmlPane = new JEditorPane("text/html","");
htmlPane.setEditable(false);
JScrollPane htmlView = new JScrollPane(htmlPane);
htmlView.setPreferredSize(new Dimension( rightWidth, windowHeight ));
// Build split-pane view
JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
treeVi
ew, htmlView );
splitPane.setContinuousLayout( true );
splitPane.setDividerLocation( leftWidth );
splitPane.setPreferredSize(
new Dimension( windowWidth + 10, windowHeight+10 ));
// Add GUI components
this.setLayout(new BorderLayout());
this.add("Center", splitPane );
} // Constructor
public static void main(String [] argv) {
System.out.println("ABOUT TO CREAT ERR.TXT");
try {
// set up error log
m_ErrLog = new PrintWriter(new File("err.txt"));
System.out.println("CREATED ERR.TXT");
} catch (java.io.FileNotFoundException ioe) {
ioe.printStackTrace();
}
if (argv.length != 1) {
System.err.println("Usage: java XMLDOM filename");
System.exit(1);
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// set validation awareness
// factory.setValidating(true);
factory.setNamespaceAware(true);
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLang
uage", "http://www.w3.org/2001/XMLSchema");
try {
javax.xml.validation.Schema schema =
javax.xml.validation.SchemaFactory.n
ewInstance("http://www.w3.org/2001/XMLSchema").newSchema(
new File("com/churchillchina/utils/test.xsd"));
factory.setSchema(schema);
DocumentBuilder builder = factory.newDocumentBuilder();
/* complicated exception stuff - maybe delete later! */
builder.setErrorHandler(
new org.xml.sax.ErrorHandler() {
// ignore fatal errors (an exception is guaranteed)
public void fatalError(SAXParseException exception) throws
SAXException
{
}
// treat validation errors as fatal
public void error(SAXParseException e) throws SAXParseException {
throw e;
}
// dump warnings too
public void warning(SAXParseException err) throws
SAXParseException {
System.out.println("** Warning" + ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
}
);
/* end of exception bit */
/*
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
org.xml.sax.XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/validation/schema",
true);
*/
File file = new File(argv[0]);
System.out.println("FILE: " + file.toString() + " exists: " +
file.exists
());
document = builder.parse(new
File("com/churchillchina/utils/test.xml"));
//document = builder.parse(new File(argv[0]));
//document = builder.parse(argv[0]);
//document = builder.parse(new FileInputStream(new File(argv[0])));
makeFrame();
} catch (IllegalStateException ise) {
ise.printStackTrace();
System.out.println("PRINTING 0");
m_ErrLog.print(getStackTrace(ise));
m_ErrLog.flush();
m_ErrLog.close();
} catch (SAXException e) {
// Error generated during parsing
System.out.println("PRINTING 1");
m_ErrLog.print(getStackTrace(e));
Exception x = e;
if (e.getException() != null) {
x = e.getException();
}
x.printStackTrace();
System.out.println(x.getMessage());
} catch (ParserConfigurationException e) {
System.out.println("PRINTING 2");
m_ErrLog.print(getStackTrace(e));
// Parser with specified options can't be built
e.printStackTrace();
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("PRINTING 3");
m_ErrLog.print(getStackTrace(e));
// I/O Error
e.printStackTrace();
System.out.println(e.getMessage());
}
} // end of main
public static void makeFrame() {
// Set up a GUI framework
JFrame frame = new JFrame("DOM Test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
// Set up the tree, the views, and display it all
final XMLDOM domPanel = new XMLDOM();
frame.getContentPane().add("Center", domPanel );
frame.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int w = windowWidth + 10;
int h = windowHeight + 10;
frame.setLocation(screenSize.width/3 - w/2, screenSize.height/2 -
h/2);
frame.setSize(w, h);
frame.setVisible(true);
} // makeFrame
public static String getStackTrace(Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
}
STACK TRACE ============================
java.lang.IllegalStateException
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$
3.checkState(ValidatorHandlerImpl.java:411)
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$
3.getElementTypeInfo(ValidatorHandlerImpl.java:441)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.e
lementAug(JAXPValidatorComponent.java:299)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.e
ndElement(JAXPValidatorComponent.java:291)
at
com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:
163)
at
com.sun.org.apache.xerces.internal.jaxp.validation.XNI2SAXEx.endElement(
XNI2SAXEx.java:108)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement
(XMLSchemaValidator.java:818)
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.
endElement(ValidatorHandlerImpl.java:339)
at
com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:
163)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.endElemen
t(JAXPValidatorComponent.java:206)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.emptyElem
ent(JAXPValidatorComponent.java:211)
at
com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler.emptyElement
(XIncludeHandler.java:755)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta
rtElement(XMLNSDocumentScannerImpl.java:316)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$F
ragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1693)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
canDocument(XMLDocumentFragmentScannerImpl.java:368)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:834)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:764)
at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.jav
a:148)
at
com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.jav
a:248)
at
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Docume
ntBuilderImpl.java:292)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:169)
at com.churchillchina.utils.XMLDOM.main(XMLDOM.java:165)
Have a java application that attempts to create a DOM document from an
XML f
ile by validating against an XSD which is defined in the code. The
RuntimeE
xception, IllegalStateException is raised on the line:
document = builder.parse(new File(argv[0]));
If you validate the XML with the XSD using say an online validation
utility
it validates fine. I have posted the stack trace, and the Java file
below.
Thanks in advance.
Steve.
XMLDOM.JAVA ============================
package com.churchillchina.utils;
import ...
public class XMLDOM extends JPanel {
static Document document;
static final int windowHeight = 460;
static final int leftWidth = 300;
static final int rightWidth = 340;
static final int windowWidth = leftWidth + rightWidth;
private static PrintWriter m_ErrLog;
public XMLDOM() {
// Set up the tree
JTree tree = new JTree();
// Build left-side view
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(new Dimension( leftWidth, windowHeight ));
// Build right-side view
JEditorPane htmlPane = new JEditorPane("text/html","");
htmlPane.setEditable(false);
JScrollPane htmlView = new JScrollPane(htmlPane);
htmlView.setPreferredSize(new Dimension( rightWidth, windowHeight ));
// Build split-pane view
JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
treeVi
ew, htmlView );
splitPane.setContinuousLayout( true );
splitPane.setDividerLocation( leftWidth );
splitPane.setPreferredSize(
new Dimension( windowWidth + 10, windowHeight+10 ));
// Add GUI components
this.setLayout(new BorderLayout());
this.add("Center", splitPane );
} // Constructor
public static void main(String [] argv) {
System.out.println("ABOUT TO CREAT ERR.TXT");
try {
// set up error log
m_ErrLog = new PrintWriter(new File("err.txt"));
System.out.println("CREATED ERR.TXT");
} catch (java.io.FileNotFoundException ioe) {
ioe.printStackTrace();
}
if (argv.length != 1) {
System.err.println("Usage: java XMLDOM filename");
System.exit(1);
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// set validation awareness
// factory.setValidating(true);
factory.setNamespaceAware(true);
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLang
uage", "http://www.w3.org/2001/XMLSchema");
try {
javax.xml.validation.Schema schema =
javax.xml.validation.SchemaFactory.n
ewInstance("http://www.w3.org/2001/XMLSchema").newSchema(
new File("com/churchillchina/utils/test.xsd"));
factory.setSchema(schema);
DocumentBuilder builder = factory.newDocumentBuilder();
/* complicated exception stuff - maybe delete later! */
builder.setErrorHandler(
new org.xml.sax.ErrorHandler() {
// ignore fatal errors (an exception is guaranteed)
public void fatalError(SAXParseException exception) throws
SAXException
{
}
// treat validation errors as fatal
public void error(SAXParseException e) throws SAXParseException {
throw e;
}
// dump warnings too
public void warning(SAXParseException err) throws
SAXParseException {
System.out.println("** Warning" + ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
}
);
/* end of exception bit */
/*
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
org.xml.sax.XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/validation/schema",
true);
*/
File file = new File(argv[0]);
System.out.println("FILE: " + file.toString() + " exists: " +
file.exists
());
document = builder.parse(new
File("com/churchillchina/utils/test.xml"));
//document = builder.parse(new File(argv[0]));
//document = builder.parse(argv[0]);
//document = builder.parse(new FileInputStream(new File(argv[0])));
makeFrame();
} catch (IllegalStateException ise) {
ise.printStackTrace();
System.out.println("PRINTING 0");
m_ErrLog.print(getStackTrace(ise));
m_ErrLog.flush();
m_ErrLog.close();
} catch (SAXException e) {
// Error generated during parsing
System.out.println("PRINTING 1");
m_ErrLog.print(getStackTrace(e));
Exception x = e;
if (e.getException() != null) {
x = e.getException();
}
x.printStackTrace();
System.out.println(x.getMessage());
} catch (ParserConfigurationException e) {
System.out.println("PRINTING 2");
m_ErrLog.print(getStackTrace(e));
// Parser with specified options can't be built
e.printStackTrace();
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("PRINTING 3");
m_ErrLog.print(getStackTrace(e));
// I/O Error
e.printStackTrace();
System.out.println(e.getMessage());
}
} // end of main
public static void makeFrame() {
// Set up a GUI framework
JFrame frame = new JFrame("DOM Test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
// Set up the tree, the views, and display it all
final XMLDOM domPanel = new XMLDOM();
frame.getContentPane().add("Center", domPanel );
frame.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int w = windowWidth + 10;
int h = windowHeight + 10;
frame.setLocation(screenSize.width/3 - w/2, screenSize.height/2 -
h/2);
frame.setSize(w, h);
frame.setVisible(true);
} // makeFrame
public static String getStackTrace(Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
}
STACK TRACE ============================
java.lang.IllegalStateException
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$
3.checkState(ValidatorHandlerImpl.java:411)
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$
3.getElementTypeInfo(ValidatorHandlerImpl.java:441)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.e
lementAug(JAXPValidatorComponent.java:299)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.e
ndElement(JAXPValidatorComponent.java:291)
at
com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:
163)
at
com.sun.org.apache.xerces.internal.jaxp.validation.XNI2SAXEx.endElement(
XNI2SAXEx.java:108)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement
(XMLSchemaValidator.java:818)
at
com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.
endElement(ValidatorHandlerImpl.java:339)
at
com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:
163)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.endElemen
t(JAXPValidatorComponent.java:206)
at
com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.emptyElem
ent(JAXPValidatorComponent.java:211)
at
com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler.emptyElement
(XIncludeHandler.java:755)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta
rtElement(XMLNSDocumentScannerImpl.java:316)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$F
ragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1693)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
canDocument(XMLDocumentFragmentScannerImpl.java:368)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:834)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:764)
at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.jav
a:148)
at
com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.jav
a:248)
at
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Docume
ntBuilderImpl.java:292)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:169)
at com.churchillchina.utils.XMLDOM.main(XMLDOM.java:165)