List of usage examples for org.xml.sax SAXParseException printStackTrace
public void printStackTrace(PrintStream s)
From source file:jp.ikedam.jenkins.plugins.viewcopy_builder.ViewcopyBuilder.java
/** * Returns the configuration XML document of a view * /* w w w . ja va2 s .c o m*/ * @param view * @param logger * @return * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ private Document getViewConfigXmlDocument(View view, final PrintStream logger) throws IOException, SAXException, ParserConfigurationException { XStream2 xStream2 = new XStream2(new DomDriver("UTF-8")); xStream2.omitField(View.class, "owner"); xStream2.omitField(View.class, "name"); // this field causes disaster when overwriting. PipedOutputStream sout = new PipedOutputStream(); PipedInputStream sin = new PipedInputStream(sout); xStream2.toXML(view, sout); sout.close(); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFactory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { exception.printStackTrace(logger); } @Override public void error(SAXParseException exception) throws SAXException { exception.printStackTrace(logger); } @Override public void fatalError(SAXParseException exception) throws SAXException { exception.printStackTrace(logger); } }); return builder.parse(sin); }
From source file:InlineSchemaValidator.java
public void validate(Validator validator, Source source, String systemId, int repetitions, boolean memoryUsage) { try {//from w w w.j a va 2 s. c o m long timeBefore = System.currentTimeMillis(); long memoryBefore = Runtime.getRuntime().freeMemory(); for (int j = 0; j < repetitions; ++j) { validator.validate(source); } long memoryAfter = Runtime.getRuntime().freeMemory(); long timeAfter = System.currentTimeMillis(); long time = timeAfter - timeBefore; long memory = memoryUsage ? memoryBefore - memoryAfter : Long.MIN_VALUE; printResults(fOut, systemId, time, memory, repetitions); } catch (SAXParseException e) { // ignore } catch (Exception e) { System.err.println("error: Parse error occurred - " + e.getMessage()); Exception se = e; if (e instanceof SAXException) { se = ((SAXException) e).getException(); } if (se != null) se.printStackTrace(System.err); else e.printStackTrace(System.err); } }
From source file:eionet.gdem.validation.ValidationService.java
/** /**// w w w .ja va 2s . co m * Validate XML. If schema is null, then read the schema or DTD from the header of XML. If schema or DTD is defined, then ignore * the defined schema or DTD. * * @param srcStream XML file as InputStream to be validated. * @param schema XML Schema URL. * @return Validation result as HTML snippet. * @throws DCMException in case of unknnown system error. */ public String validateSchema(InputStream srcStream, String schema) throws DCMException { String result = ""; boolean isDTD = false; boolean isBlocker = false; if (Utils.isNullStr(schema)) { schema = null; } try { SAXParserFactory spfact = SAXParserFactory.newInstance(); SAXParser parser = spfact.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(errHandler); XmlconvCatalogResolver catalogResolver = new XmlconvCatalogResolver(); reader.setEntityResolver(catalogResolver); // make parser to validate reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false); InputAnalyser inputAnalyser = new InputAnalyser(); inputAnalyser.parseXML(uriXml); String namespace = inputAnalyser.getSchemaNamespace(); // if schema is not in the parameter, then sniff it from the header of xml if (schema == null) { schema = inputAnalyser.getSchemaOrDTD(); isDTD = inputAnalyser.isDTD(); } else { // if the given schema ends with dtd, then don't do schema validation isDTD = schema.endsWith("dtd"); } // schema is already given as a parameter. Read the default namespace from XML file and set external schema. if (schema != null) { if (!isDTD) { if (Utils.isNullStr(namespace)) { // XML file does not have default namespace setNoNamespaceSchemaProperty(reader, schema); } else { setNamespaceSchemaProperty(reader, namespace, schema); } } else { // validate against DTD setLocalSchemaUrl(schema); LocalEntityResolver localResolver = new LocalEntityResolver(schema, getValidatedSchema()); reader.setEntityResolver(localResolver); } } else { return validationFeedback.formatFeedbackText( "Could not validate XML file. Unable to locate XML Schema reference.", QAFeedbackType.WARNING, isBlocker); } // if schema is not available, then do not parse the XML and throw error if (!Utils.resourceExists(getValidatedSchema())) { return validationFeedback.formatFeedbackText( "Failed to read schema document from the following URL: " + getValidatedSchema(), QAFeedbackType.ERROR, isBlocker); } Schema schemaObj = schemaManager.getSchema(getOriginalSchema()); if (schemaObj != null) { isBlocker = schemaObj.isBlocker(); } validationFeedback.setSchema(getOriginalSchema()); InputSource is = new InputSource(srcStream); reader.parse(is); } catch (SAXParseException se) { return validationFeedback.formatFeedbackText("Document is not well-formed. Column: " + se.getColumnNumber() + "; line:" + se.getLineNumber() + "; " + se.getMessage(), QAFeedbackType.ERROR, isBlocker); } catch (IOException ioe) { return validationFeedback.formatFeedbackText( "Due to an IOException, the parser could not check the document. " + ioe.getMessage(), QAFeedbackType.ERROR, isBlocker); } catch (Exception e) { Exception se = e; if (e instanceof SAXException) { se = ((SAXException) e).getException(); } if (se != null) { se.printStackTrace(System.err); } else { e.printStackTrace(System.err); } return validationFeedback.formatFeedbackText( "The parser could not check the document. " + e.getMessage(), QAFeedbackType.ERROR, isBlocker); } validationFeedback.setValidationErrors(getErrorList()); result = validationFeedback.formatFeedbackText(isBlocker); // validation post-processor QAResultPostProcessor postProcessor = new QAResultPostProcessor(); result = postProcessor.processQAResult(result, schema); warningMessage = postProcessor.getWarningMessage(schema); return result; }