List of usage examples for org.xml.sax ErrorHandler ErrorHandler
ErrorHandler
From source file:Main.java
public static Document parse(String s) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override//from ww w.j av a 2 s . co m public void warning(SAXParseException exception) throws SAXException { } @Override public void error(SAXParseException exception) throws SAXException { } @Override public void fatalError(SAXParseException exception) throws SAXException { } }); Document rv = builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8"))); return rv; }
From source file:Main.java
public static boolean validateWithDTDUsingDOM(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*w w w . j a va2 s .com*/ factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException e) throws SAXException { System.out.println("WARNING : " + e.getMessage()); // do nothing } public void error(SAXParseException e) throws SAXException { System.out.println("ERROR : " + e.getMessage()); throw e; } public void fatalError(SAXParseException e) throws SAXException { System.out.println("FATAL : " + e.getMessage()); throw e; } }); builder.parse(new InputSource(xml)); return true; }
From source file:Main.java
public static Document validate(File xml, File xmlSchema) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from w ww . j a v a2s . c o m*/ factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException ex) throws SAXException { throw ex; } public void error(SAXParseException ex) throws SAXException { throw ex; } public void fatalError(SAXParseException ex) throws SAXException { throw ex; } }); return documentBuilder.parse(xml); }
From source file:Main.java
public static String tryFormattingString(String s) { try {// w ww. jav a 2 s . c o m ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { } @Override public void fatalError(SAXParseException exception) throws SAXException { } @Override public void error(SAXParseException exception) throws SAXException { } }); Document doc = builder.parse(in); TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(2)); Transformer trans = tf.newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.transform(source, result); return out.toString(); } catch (Exception e) { // Console.println("Could not validate the soapmessage, although I'll show it anyway.."); } return s; }
From source file:Main.java
/** * Valida un documento XML con un esquema XML (XSD). * @param xml File to validate/* w ww .j av a 2 s . co m*/ * @param xmlSchema File with the schema * @return Dom document * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Document validate(File xml, File xmlSchema) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema); // Parsing DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException ex) throws SAXException { throw ex; } public void error(SAXParseException ex) throws SAXException { throw ex; } public void fatalError(SAXParseException ex) throws SAXException { throw ex; } }); return documentBuilder.parse(xml); }
From source file:Main.java
private static void validateDTD0(String xmlFile, final String dtdPath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (dtdPath != null && !dtdPath.isEmpty()) { documentBuilder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new FileInputStream(dtdPath)); }/* ww w . jav a2 s .c o m*/ }); } documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); documentBuilder.parse(new FileInputStream(xmlFile)); }
From source file:Main.java
public static Document toDocument(String xml, boolean namespaceAware, boolean ignoreDtd) throws Exception { Reader input = new StringReader(xml); InputSource is = new InputSource(input); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(namespaceAware); // ignore dtd files if (ignoreDtd) { dbf.setValidating(false);//from www . j av a2 s . co m dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { throw exception; } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); return db.parse(is); }
From source file:Main.java
public static boolean validateWithDTDUsingSAX(String xml) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true);/* ww w .ja v a2 s .co m*/ factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException e) throws SAXException { System.out.println("WARNING : " + e.getMessage()); // do nothing } public void error(SAXParseException e) throws SAXException { System.out.println("ERROR : " + e.getMessage()); throw e; } public void fatalError(SAXParseException e) throws SAXException { System.out.println("FATAL : " + e.getMessage()); throw e; } }); reader.parse(new InputSource(xml)); return true; }
From source file:pydio.sdk.java.http.XMLDocEntity.java
public XMLDocEntity(HttpEntity notConsumedEntity) throws ParserConfigurationException, IOException, SAXException { ErrorHandler myErrorHandler = new ErrorHandler() { public void fatalError(SAXParseException exception) throws SAXException { System.err.println("fatalError: " + exception); }/*from w w w. java 2s .c om*/ public void error(SAXParseException exception) throws SAXException { System.err.println("error: " + exception); } public void warning(SAXParseException exception) throws SAXException { System.err.println("warning: " + exception); } }; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(myErrorHandler); try { doc = db.parse(notConsumedEntity.getContent()); } catch (SAXException e) { this.currentE = e; } catch (IOException e) { e.printStackTrace(); } }
From source file:com.limegroup.gnutella.licenses.PublishedCCLicenseTest.java
/** * Tests that the verification RDF validates * against a RELAX-NG Schema/*from w w w. j a v a 2s . c o m*/ */ public void testVerifyRDFValidates() throws Exception { String rdfEmbeddedComment = PublishedCCLicense.getRDFRepresentation("TIMMAY", "TIMMAY SLEEPAY", "2005", "Timmay Drink Espresso", "urn:sha1:TIMMAYESPRESSOH32VV4A2LJUDNLPAJ6", CCConstants.ATTRIBUTION_SHARE_NON_COMMERCIAL); // first extract the RDF out of the html comment StringReader reader = new StringReader(rdfEmbeddedComment); HTMLEditorKit.ParserCallback callback = new CommentExtractor(); getHTMLEditorKitParser().parse(reader, callback, true); synchronized (rdfLock) { // wait for parser to finish while (rdf == null) { try { rdfLock.wait(); } catch (InterruptedException e) { } } } // now let's parse the RDF and validate it against the schema // set up ManekiNeko parser SAXParser parser = new SAXParser(); parser.setErrorHandler(new ErrorHandler() { public void error(SAXParseException exception) throws SAXException { LOG.error("error in sax", exception); } public void fatalError(SAXParseException exception) throws SAXException { LOG.fatal("error in sax", exception); throw exception; } public void warning(SAXParseException exception) throws SAXException { LOG.warn("error in sax", exception); } }); parser.setFeature("http://xml.org/sax/features/namespaces", true); parser.setFeature("http://xml.org/sax/features/validation", true); // bug in MinekiNeko prevents use of compact schema. use regular schema instead // parser.setFeature("http://cyberneko.org/xml/features/relaxng/compact-syntax", true); File f = TestUtils.getResourceFile("com/limegroup/gnutella/licenses/ccverificationrdf-schema.rng"); assertTrue(f.exists()); // must have rng to validate. parser.setProperty("http://cyberneko.org/xml/properties/relaxng/schema-location", f.getAbsoluteFile().toURI()); parser.parse(new InputSource(new StringReader(rdf))); }