Example usage for org.xml.sax SAXParseException getMessage

List of usage examples for org.xml.sax SAXParseException getMessage

Introduction

In this page you can find the example usage for org.xml.sax SAXParseException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Return a detail message for this exception.

Usage

From source file:org.alfresco.util.xml.SchemaHelper.java

public static void main(String... args) {
    if (args.length < 2 || !args[0].startsWith("--compile-xsd=") && !args[1].startsWith("--output-dir=")) {
        System.out.println("Usage: SchemaHelper --compile-xsd=<URL> --output-dir=<directory>");
        System.exit(1);//from  ww  w .  j av a  2 s.  c o  m
    }
    final String urlStr = args[0].substring(14);
    if (urlStr.length() == 0) {
        System.out.println("Usage: SchemaHelper --compile-xsd=<URL> --output-dir=<directory>");
        System.exit(1);
    }
    final String dirStr = args[1].substring(13);
    if (dirStr.length() == 0) {
        System.out.println("Usage: SchemaHelper --compile-xsd=<URL> --output-dir=<directory>");
        System.exit(1);
    }
    try {
        URL url = ResourceUtils.getURL(urlStr);
        File dir = new File(dirStr);
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("Output directory not found: " + dirStr);
            System.exit(1);
        }

        ErrorListener errorListener = new ErrorListener() {
            public void warning(SAXParseException e) {
                System.out.println("WARNING: " + e.getMessage());
            }

            public void info(SAXParseException e) {
                System.out.println("INFO: " + e.getMessage());
            }

            public void fatalError(SAXParseException e) {
                handleException(urlStr, e);
            }

            public void error(SAXParseException e) {
                handleException(urlStr, e);
            }
        };

        SchemaCompiler compiler = XJC.createSchemaCompiler();
        compiler.setErrorListener(errorListener);
        compiler.parseSchema(new InputSource(url.toExternalForm()));
        S2JJAXBModel model = compiler.bind();
        if (model == null) {
            System.out.println("Failed to produce binding model for URL " + urlStr);
            System.exit(1);
        }
        JCodeModel codeModel = model.generateCode(null, errorListener);
        codeModel.build(dir);
    } catch (Throwable e) {
        handleException(urlStr, e);
        System.exit(1);
    }
}

From source file:Main.java

public static boolean validateWithDTDUsingDOM(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from www .ja  va 2 s . c o m*/
    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 boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from   ww w  .j av 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:MainClass.java

static void fail(SAXException e) {
    if (e instanceof SAXParseException) {
        SAXParseException spe = (SAXParseException) e;
        System.err.printf("%s:%d:%d: %s%n", spe.getSystemId(), spe.getLineNumber(), spe.getColumnNumber(),
                spe.getMessage());
    } else {/*from   w  w  w  .ja  v  a2 s  .c  o m*/
        System.err.println(e.getMessage());
    }
    System.exit(1);
}

From source file:com.aol.advertising.qiao.util.XmlConfigUtil.java

/**
 * Validate an XML document against the given schema.
 *
 * @param xmlFileLocationUri//  ww  w  .  ja  v  a2s  .co m
 *            Location URI of the document to be validated.
 * @param schemaLocationUri
 *            Location URI of the XML schema in W3C XML Schema Language.
 * @return true if valid, false otherwise.
 */
public static boolean validateXml(String xmlFileLocationUri, String schemaLocationUri) {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    InputStream is = null;
    try {
        Schema schema = factory.newSchema(CommonUtils.uriToURL(schemaLocationUri));
        Validator validator = schema.newValidator();
        URL url = CommonUtils.uriToURL(xmlFileLocationUri);
        is = url.openStream();
        Source source = new StreamSource(is);

        validator.validate(source);
        logger.info(">> successfully validated configuration file: " + xmlFileLocationUri);

        return true;
    } catch (SAXParseException e) {
        logger.error(e.getMessage() + " (line:" + e.getLineNumber() + ", col:" + e.getColumnNumber() + ")");

    } catch (Exception e) {
        if (e instanceof SAXParseException) {
            SAXParseException e2 = (SAXParseException) e;
            logger.error(e2.getMessage() + "at line:" + e2.getLineNumber() + ", col:" + e2.getColumnNumber());
        } else {
            logger.error(e.getMessage());
        }
    } finally {
        IOUtils.closeQuietly(is);
    }

    return false;
}

From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java

public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) {
    boolean validXml = false;
    try {/*  ww  w  .  ja v a2 s.c  o m*/
        // parse an XML document into a DOM tree
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(false);
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xmlContent)));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile());

        Schema schema = schemaFactory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
        validXml = true;
    } catch (FileNotFoundException ex) {
        throw new CaaersSystemException("File Not found Exception", ex);
    } catch (IOException ioe) {
        validationResult.append(ioe.getMessage());
        logger.error(ioe.getMessage());
    } catch (SAXParseException spe) {
        validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
        logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
    } catch (SAXException e) {
        validationResult.append(e.toString());
        logger.error(e.toString());
    } catch (ParserConfigurationException pce) {
        validationResult.append(pce.getMessage());
    }
    return validXml;
}

From source file:Main.java

public static Document openXmlStream(InputStream stream, Schema schema, boolean isNamespaceAware,
        boolean isXIncludeAware) throws IOException, SAXParseException, SAXException {
    DocumentBuilder builder = newDocumentBuilder(schema, isNamespaceAware, isXIncludeAware);
    Document docu;/* www. ja  v  a2  s .  co m*/

    builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
        @Override
        public void error(SAXParseException exception) throws SAXParseException {
            throw new SAXParseException("Parse Error in instream, line " + exception.getLineNumber() + ": "
                    + exception.getMessage(), "", "instream", exception.getLineNumber(), 0);
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXParseException {
            throw new SAXParseException("Parse Error in instream, line " + exception.getLineNumber() + ": "
                    + exception.getMessage(), "", "instream", exception.getLineNumber(), 0);
        }

        @Override
        public void warning(SAXParseException exception) {
            System.err.println("Parse Warning: " + exception.getMessage() + exception.getLineNumber());
        }
    });
    docu = builder.parse(stream);

    return docu;
}

From source file:info.magnolia.module.model.reader.BetwixtModuleDefinitionReader.java

private static String getSaxParseExceptionMessage(SAXParseException e) {
    return "at line " + e.getLineNumber() + " column " + e.getColumnNumber() + ": " + e.getMessage();
}

From source file:Main.java

/**
 * Parse the XML file and create Document
 * @param fileName/*from  w w  w  . j  av  a 2s .  c o m*/
 * @return Document
 */
public static Document parse(InputStream fs) {
    Document document = null;
    // Initiate DocumentBuilderFactory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // To get a validating parser
    factory.setValidating(false);

    // To get one that understands namespaces
    factory.setNamespaceAware(true);
    try {
        // Get DocumentBuilder
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Parse and load into memory the Document
        //document = builder.parse( new File(fileName));
        document = builder.parse(fs);
        return document;
    } catch (SAXParseException spe) {
        // Error generated by the parser
        System.err.println("\n** Parsing error , line " + spe.getLineNumber() + ", uri " + spe.getSystemId());
        System.err.println(" " + spe.getMessage());
        // Use the contained exception, if any
        Exception x = spe;
        if (spe.getException() != null)
            x = spe.getException();
        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated during parsing
        Exception x = sxe;
        if (sxe.getException() != null)
            x = sxe.getException();
        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Element loadDocument(File location) {
    Document doc = null;//from   ww  w.  ja v a  2s .co m
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
        doc = parser.parse(location);
        Element root = doc.getDocumentElement();
        root.normalize();

        return root;
    } catch (SAXParseException err) {
        System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri "
                + err.getSystemId());
        System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
    } catch (SAXException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (MalformedURLException mfx) {
        System.err.println("URLMappingsXmlDAO error: " + mfx);
    } catch (IOException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (Exception pce) {
        System.err.println("URLMappingsXmlDAO error: " + pce);
    }
    return null;
}