Example usage for org.xml.sax SAXParseException getLineNumber

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

Introduction

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

Prototype

public int getLineNumber() 

Source Link

Document

The line number of the end of the text where the exception occurred.

Usage

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;//from w  ww .ja va2s . 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:Main.java

/**
 * Parse the XML file and create Document
 * @param fileName/*from   w  w w.  ja va 2 s. c om*/
 * @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: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:com.aol.advertising.qiao.util.XmlConfigUtil.java

/**
 * Validate an XML document against the given schema.
 *
 * @param xmlFileLocationUri//  w  ww . j av  a 2 s . c  o  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:net.sf.nmedit.jpatch.clavia.nordmodular.NM1ModuleDescriptions.java

public static NM1ModuleDescriptions parse(ClassLoader loader, InputStream stream)
        throws ParserConfigurationException, SAXException, IOException {
    NM1ModuleDescriptions mod = new NM1ModuleDescriptions(loader);

    try {/*  www. java2  s  . co m*/
        ModuleDescriptionsParser.parse(mod, stream);
    } catch (SAXParseException spe) {
        Log log = LogFactory.getLog(NM1ModuleDescriptions.class);
        if (log.isErrorEnabled()) {
            log.error("error in parse(" + loader + "," + stream + "); " + "@" + spe.getLineNumber() + ":"
                    + spe.getColumnNumber(), spe);
        }
    }
    return mod;
}

From source file:es.mityc.firmaJava.libreria.utilidades.AnalizadorFicheroFirma.java

public static void mostrarError(SAXParseException exc, String aviso) throws SAXException {
    log.error(aviso);/*from  w  ww . j ava2 s .  c om*/
    log.error(LINE_DOS_PUNTOS + exc.getLineNumber());
    log.error(URI_DOS_PUNTOS + exc.getSystemId());
    log.error(I18n.getResource(LIBRERIA_UTILIDADES_ANALIZADOR_ERROR_3) + exc.getMessage());
    throw new SAXException(aviso);
}

From source file:eu.delving.x3ml.X3MLEngine.java

private static String errorMessage(SAXParseException e) {
    return String.format("%d:%d - %s", e.getLineNumber(), e.getColumnNumber(), e.getMessage());
}

From source file:Main.java

/**
 * Builds a prettier exception message.// ww w. j  a  v  a  2 s.c  o  m
 *
 * @param ex the SAXParseException
 * @return an easier to read exception message
 */
public static String getPrettyParseExceptionInfo(SAXParseException ex) {

    final StringBuilder sb = new StringBuilder();

    if (ex.getSystemId() != null) {
        sb.append("systemId=").append(ex.getSystemId()).append(", ");
    }
    if (ex.getPublicId() != null) {
        sb.append("publicId=").append(ex.getPublicId()).append(", ");
    }
    if (ex.getLineNumber() > 0) {
        sb.append("Line=").append(ex.getLineNumber());
    }
    if (ex.getColumnNumber() > 0) {
        sb.append(", Column=").append(ex.getColumnNumber());
    }
    sb.append(": ").append(ex.getMessage());

    return sb.toString();
}

From source file:Main.java

public static Document openXmlFile(File file, Schema schema, boolean isNamespaceAware, boolean isXIncludeAware)
        throws IOException, SAXParseException, SAXException {
    final String fname = file.getCanonicalPath();
    DocumentBuilder builder = newDocumentBuilder(schema, isNamespaceAware, isXIncludeAware);
    Document docu;/*from   ww w. ja  v  a2s . co  m*/

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

        @Override
        public void fatalError(SAXParseException exception) throws SAXParseException {
            //System.err.println("Parse Fatal Error: " + exception.getMessage() + exception.getLineNumber());
            throw new SAXParseException("Parse Error in file " + fname + ", line " + exception.getLineNumber()
                    + ": " + exception.getMessage(), "", fname, exception.getLineNumber(), 0);
        }

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

    docu = builder.parse(file);
    return docu;
}

From source file:net.ontopia.topicmaps.entry.XMLConfigSource.java

private static List<TopicMapSourceIF> readSources(InputSource inp_source, Map<String, String> environ) {
    ConfigHandler handler = new ConfigHandler(environ);

    try {//  w w w  .j  a  va2  s.  com
        XMLReader parser = DefaultXMLReaderFactory.createXMLReader();
        parser.setContentHandler(handler);
        parser.setErrorHandler(new Slf4jSaxErrorHandler(log));
        parser.parse(inp_source);
    } catch (SAXParseException e) {
        String msg = "" + e.getSystemId() + ":" + e.getLineNumber() + ":" + e.getColumnNumber() + ": "
                + e.getMessage();
        throw new OntopiaRuntimeException(msg, e);
    } catch (Exception e) {
        throw new OntopiaRuntimeException(e);
    }
    return handler.sources;
}