List of usage examples for org.dom4j.io SAXReader setErrorHandler
public void setErrorHandler(ErrorHandler errorHandler)
ErrorHandler
used by the SAX XMLReader
. From source file:ca.coder2000.recipes.RecipeFile.java
License:Mozilla Public License
/** * Creates a new instance of RecipeFile/*ww w . j a v a 2 s . com*/ * @param file the file we want this class to load and manage. */ public RecipeFile(File file) { SAXReader reader = new SAXReader(true); reader.setValidation(true); try { reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "schemas/recipe.xsd"); reader.setErrorHandler(new RecipeErrorHandler()); doc = reader.read(file); checkVersion(); } catch (DocumentException de) { JOptionPane.showMessageDialog(null, de.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } catch (SAXException sxe) { JOptionPane.showMessageDialog(null, sxe.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } }
From source file:com.arc.xml.XmlProcessor.java
License:Open Source License
/** * Read a document from the source given the constraints defined by the meta-xml that was received in the * {@link #XmlProcessor(File) constructor}. * <P>//from w w w.ja va 2s. c om * The result is a document whose root element's <code>getData()</code> method will return the resulting data * obect. * @param source the XML file to be read. * @param instantiator the object for instantiating classes to form objects as we walk the XML document. * @param rootPackage the package relative to which classes are located. * @param ehandler error handler */ public Document read(InputSource source, IBuilderInstantiator instantiator, String rootPackage, ErrorHandler ehandler) throws SAXParseException, SAXException { mInstantiator = instantiator; mRootPackage = rootPackage; SAXReader reader = new MySAXReader(); if (mXMLReader != null) reader.setXMLReader(mXMLReader); mErrorHandler = ehandler; reader.setErrorHandler(ehandler); try { Document doc = reader.read(source); walk(doc); return doc; } catch (SAXParseException x) { if (x.getException() instanceof RuntimeException) throw (RuntimeException) x.getException(); if (mErrorHandler != null) mErrorHandler.error(x); else throw x; } catch (SAXException x) { // unwrap nested exceptions while (x.getException() != null) { Exception t = x.getException(); if (t instanceof SAXException && !(t instanceof SAXParseException)) x = (SAXException) t; else if (t instanceof RuntimeException) throw (RuntimeException) t; } if (mErrorHandler != null && x instanceof SAXParseException) { mErrorHandler.error((SAXParseException) x); } else throw x; } catch (DocumentException x) { throw new SAXException("DocumentException", x); } return null; }
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
/** * Validates the document for this URL./* w ww. j a v a2 s . co m*/ * * @param url the URL of the document. * * @return the Dom4J document. */ public static synchronized XDocument validate(ErrorHandler handler, BufferedReader isReader, String systemId) throws IOException, SAXParseException { if (DEBUG) System.out.println("DocumentUtilities.validate( " + isReader + ", " + systemId + ")"); XDocument document = null; try { SAXReader reader = createReader(true, false); reader.setEntityResolver(getCatalogResolver()); reader.setErrorHandler(handler); String encoding = null; try { isReader.mark(1024); encoding = getXMLDeclaration(isReader).getEncoding(); // } catch ( NotXMLException e) { // e.printStackTrace(); // throw( e); } finally { isReader.reset(); } XMLReader xmlReader = createReader(isReader, encoding); document = (XDocument) reader.read(xmlReader, systemId); document.setEncoding(xmlReader.getEncoding()); document.setVersion(xmlReader.getVersion()); } catch (DocumentException e) { Exception x = (Exception) e.getNestedException(); if (x instanceof SAXParseException) { SAXParseException spe = (SAXParseException) x; Exception ex = spe.getException(); if (ex instanceof IOException) { throw (IOException) ex; } else { throw (SAXParseException) x; } } else if (x instanceof IOException) { throw (IOException) x; } } return document; }
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
public static synchronized void validate(ErrorHandler handler, BufferedReader isReader, String systemId, String encoding, int type, String grammarLocation) throws IOException, SAXParseException { if (DEBUG)/*w w w .j a v a 2 s. c om*/ System.out.println("XMLUtilities.validate( " + isReader + ", " + systemId + ", " + encoding + ", " + type + ", " + grammarLocation + ")"); if (type == XMLGrammar.TYPE_RNG || type == XMLGrammar.TYPE_RNC || type == XMLGrammar.TYPE_NRL) { ValidationDriver driver = null; String encode = encoding; if (type == XMLGrammar.TYPE_RNC) { driver = new ValidationDriver(makePropertyMap(null, handler, CHECK_ID_IDREF, false), CompactSchemaReader.getInstance()); } else if (type == XMLGrammar.TYPE_NRL) { driver = new ValidationDriver(makePropertyMap(null, handler, CHECK_ID_IDREF, true), new AutoSchemaReader()); } else { driver = new ValidationDriver(makePropertyMap(null, handler, CHECK_ID_IDREF, false)); } try { isReader.mark(1024); encode = getXMLDeclaration(isReader).getEncoding(); // } catch ( NotXMLException e) { // encode = encoding; // e.printStackTrace(); } finally { isReader.reset(); } InputSource source = new InputSource(isReader); source.setEncoding(encode); try { URL url = null; if (systemId != null) { try { url = new URL(systemId); } catch (MalformedURLException e) { // does not matter really, the base url is only null ... url = null; } } URL schemaURL = null; if (url != null) { schemaURL = new URL(url, grammarLocation); } else { schemaURL = new URL(grammarLocation); } driver.loadSchema(new InputSource(schemaURL.toString())); driver.validate(source); } catch (SAXException x) { x.printStackTrace(); if (x instanceof SAXParseException) { SAXParseException spe = (SAXParseException) x; Exception ex = spe.getException(); if (ex instanceof IOException) { throw (IOException) ex; } else { throw (SAXParseException) x; } } } catch (IOException e) { e.printStackTrace(); throw e; } } else { // type == XMLGrammar.TYPE_DTD || type == XMLGrammar.TYPE_XSD try { SAXReader reader = createReader(true, false); reader.setErrorHandler(handler); String encode = encoding; try { isReader.mark(1024); encode = getXMLDeclaration(isReader).getEncoding(); // } catch ( NotXMLException e) { // encode = encoding; // e.printStackTrace(); } finally { isReader.reset(); } XMLReader xmlReader = createReader(isReader, encode); try { if (type == XMLGrammar.TYPE_DTD) { reader.setFeature("http://apache.org/xml/features/validation/schema", false); reader.setEntityResolver(new DummyEntityResolver(grammarLocation)); } else { // type == XMLGrammar.TYPE_XSD URL url = null; if (systemId != null) { try { url = new URL(systemId); } catch (MalformedURLException e) { // does not matter really, the base url is only null ... url = null; } } URL schemaURL = null; if (url != null) { schemaURL = new URL(url, grammarLocation); } else { schemaURL = new URL(grammarLocation); } reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new InputSource(schemaURL.toString())); } } catch (Exception e) { e.printStackTrace(); } // try { // System.out.println( "http://apache.org/xml/features/validation/schema = "+reader.getXMLReader().getFeature( "http://apache.org/xml/features/validation/schema")); // } catch ( Exception e) { // e.printStackTrace(); // } reader.read(xmlReader, systemId); } catch (DocumentException e) { Exception x = (Exception) e.getNestedException(); // x.printStackTrace(); if (x instanceof SAXParseException) { SAXParseException spe = (SAXParseException) x; Exception ex = spe.getException(); if (ex instanceof IOException) { throw (IOException) ex; } else { throw (SAXParseException) x; } } else if (x instanceof IOException) { throw (IOException) x; } } } }
From source file:com.genericworkflownodes.knime.config.impl.GalaxyNodeConfigurationReader.java
License:Open Source License
@Override public INodeConfiguration read(InputStream in) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(false);//from w w w. java 2s . c o m SimpleErrorHandler errorHandler = new SimpleErrorHandler(); reader.setErrorHandler(errorHandler); doc = reader.read(in); if (!errorHandler.isValid()) { System.err.println(errorHandler.getErrorReport()); throw new Exception("Galaxy tool xml file is not valid !"); } readPorts(); readParameters(); readDescription(); config.setXml(doc.asXML()); return config; }
From source file:com.genericworkflownodes.knime.schemas.SchemaValidator.java
License:Open Source License
/** * Validate the given xml stream against the stored schemata. * /* www .ja v a 2 s . c o m*/ * @param xmlstream * The stream to validate. * @return True if the file is valid, false otherwise. */ public boolean validates(InputStream xmlstream) { SAXParserFactory factory = SAXParserFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); try { factory.setSchema(schemaFactory.newSchema(getSchemaSources())); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(false); reader.setErrorHandler(errorHandler); reader.read(xmlstream); } catch (SAXException e) { throw new RuntimeException(e); } catch (DocumentException e) { throw new RuntimeException(e); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } if (!errorHandler.isValid()) { errorReport = errorHandler.getErrorReport(); return false; } return true; }
From source file:com.petpet.c3po.utils.XMLUtils.java
License:Apache License
public static boolean validate(File f) { try {//from w w w .ja v a 2s . c o m SAXParser parser = factory.newSAXParser(); parser.setProperty(Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(true); reader.setErrorHandler(errorHandler); reader.read(f); return errorHandler.isValid(); } catch (ParserConfigurationException e) { LOG.error("ParserConfigurationException: {}", e.getMessage()); } catch (SAXException e) { LOG.error("SAXException: {}", e.getMessage()); } catch (DocumentException e) { LOG.error("DocumentException: {}", e.getMessage()); } catch (NullPointerException e) { LOG.warn("Factory is not initialized. Did you call init()"); } return false; }
From source file:eu.scape_project.planning.xml.C3POProfileParser.java
License:Apache License
private boolean isValid(SAXParser parser, InputStream stream) { log.debug("validating collection profile"); try {/* w w w .ja v a2 s . co m*/ SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(true); reader.setErrorHandler(errorHandler); reader.read(stream); return errorHandler.isValid(); } catch (SAXException e) { log.error("SAXException: {}", e.getMessage()); } catch (DocumentException e) { e.printStackTrace(); log.error("DocumentException: {}", e.getMessage()); } catch (NullPointerException e) { log.warn("Factory is not initialized. Did you call init()"); } return false; }
From source file:hudson.plugins.tolerantxmlparsing.TolerantXMLErrorHandler.java
License:Open Source License
/** * Attach an instance of this class as this reader's error handler. * This will also set the feature {@link #CONTINUE_AFTER_FATAL_ERROR_FEATURE CONTINUE_AFTER_FATAL_ERROR_FEATURE}, * assuming the SAX parser is XERCES.//from w w w. ja v a 2 s . c o m * @param reader */ public static void attach(SAXReader reader, boolean ignoreSetFeatureException) throws SAXException { try { reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true); } catch (SAXException ex) { if (!ignoreSetFeatureException) { throw ex; } ex.printStackTrace(); } reader.setErrorHandler(new TolerantXMLErrorHandler()); }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private Document parse(URL url) throws DocumentException { /*SAXReader reader = new SAXReader(); Document document = reader.read(url); return document;*//*from w w w. ja va2 s. c o m*/ SAXReader reader = new SAXReader(); reader.setValidation(true); reader.setErrorHandler(new SimpleErrorHandler()); return reader.read(url); }