Search in sources :

Example 6 with ErrorHandler

use of org.xml.sax.ErrorHandler in project j2objc by google.

the class DocumentBuilderTest method testSetErrorHandler.

public void testSetErrorHandler() {
    // Ordinary case
    InputStream source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected, ErrorHandler does not mask exception
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals("error", logger.getMethod());
    assertTrue(logger.getArgs()[0] instanceof SAXParseException);
    // null case
    source = new ByteArrayInputStream("</a>".getBytes());
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(null);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) MethodLogger(org.apache.harmony.tests.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 7 with ErrorHandler

use of org.xml.sax.ErrorHandler in project j2objc by google.

the class DocumentBuilderTest method testReset.

public void testReset() {
    // Make sure EntityResolver gets reset
    InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
    InputStream entity = new ByteArrayInputStream("bar".getBytes());
    MockResolver resolver = new MockResolver();
    resolver.addEntity("foo", "foo", new InputSource(entity));
    Document d;
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(resolver);
        db.reset();
        d = db.parse(source);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    Element root = (Element) d.getElementsByTagName("a").item(0);
    assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
    // Make sure ErrorHandler gets reset
    source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.reset();
        d = db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals(0, logger.size());
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) MockResolver(org.apache.harmony.tests.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) MethodLogger(org.apache.harmony.tests.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 8 with ErrorHandler

use of org.xml.sax.ErrorHandler in project XobotOS by xamarin.

the class Properties method loadFromXML.

/**
     * Loads the properties from an {@code InputStream} containing the
     * properties in XML form. The XML document must begin with (and conform to)
     * following DOCTYPE:
     *
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
     * </pre>
     *
     * Also the content of the XML data must satisfy the DTD but the xml is not
     * validated against it. The DTD is not loaded from the SYSTEM ID. After
     * this method returns the InputStream is not closed.
     *
     * @param in the InputStream containing the XML document.
     * @throws IOException in case an error occurs during a read operation.
     * @throws InvalidPropertiesFormatException if the XML data is not a valid
     *             properties file.
     */
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new NullPointerException();
    }
    if (builder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException e) throws SAXException {
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    try {
        Document doc = builder.parse(in);
        NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            Element entry = (Element) entries.item(i);
            String key = entry.getAttribute("key");
            String value = entry.getTextContent();
            /*
                 * key != null & value != null but key or(and) value can be
                 * empty String
                 */
            put(key, value);
        }
    } catch (IOException e) {
        throw e;
    } catch (SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 9 with ErrorHandler

use of org.xml.sax.ErrorHandler in project tdi-studio-se by Talend.

the class ExpressionFileOperation method saveExpressionToFile.

/**
     * yzhang Comment method "savingExpression".
     * 
     * @return
     * @throws IOException
     * @throws ParserConfigurationException
     */
public boolean saveExpressionToFile(File file, List<Variable> variables, String expressionContent) throws IOException, ParserConfigurationException {
    final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
    final Bundle b = Platform.getBundle(PLUGIN_ID);
    final URL url = FileLocator.toFileURL(FileLocator.find(b, new Path(SCHEMA_XSD), null));
    final File schema = new File(url.getPath());
    //$NON-NLS-1$
    fabrique.setAttribute(SCHEMA_LANGUAGE, "http://www.w3.org/2001/XMLSchema");
    fabrique.setAttribute(SCHEMA_VALIDATOR, schema);
    fabrique.setValidating(true);
    final DocumentBuilder analyseur = fabrique.newDocumentBuilder();
    analyseur.setErrorHandler(new ErrorHandler() {

        public void error(final SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void fatalError(final SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void warning(final SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    Document document = analyseur.newDocument();
    //$NON-NLS-1$
    Element expressionElement = document.createElement("expression");
    document.appendChild(expressionElement);
    //$NON-NLS-1$
    Attr content = document.createAttribute(Messages.getString("ExpressionFileOperation.content"));
    content.setNodeValue(expressionContent);
    expressionElement.setAttributeNode(content);
    for (Variable variable : variables) {
        //$NON-NLS-1$
        Element variableElement = document.createElement("variable");
        expressionElement.appendChild(variableElement);
        //$NON-NLS-1$
        Attr name = document.createAttribute(Messages.getString("ExpressionFileOperation.name"));
        name.setNodeValue(variable.getName());
        variableElement.setAttributeNode(name);
        //$NON-NLS-1$
        Attr value = document.createAttribute(Messages.getString("ExpressionFileOperation.value"));
        value.setNodeValue(variable.getValue());
        variableElement.setAttributeNode(value);
        //$NON-NLS-1$
        Attr talendType = document.createAttribute(Messages.getString("ExpressionFileOperation.talendType"));
        //$NON-NLS-1$
        talendType.setNodeValue(variable.getTalendType());
        variableElement.setAttributeNode(talendType);
        //$NON-NLS-1$
        Attr nullable = document.createAttribute(Messages.getString("ExpressionFileOperation.nullable"));
        //$NON-NLS-1$
        nullable.setNodeValue(String.valueOf(variable.isNullable()));
        variableElement.setAttributeNode(nullable);
    }
    // use specific Xerces class to write DOM-data to a file:
    XMLSerializer serializer = new XMLSerializer();
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setIndenting(true);
    serializer.setOutputFormat(outputFormat);
    FileWriter writer = new FileWriter(file);
    serializer.setOutputCharStream(writer);
    serializer.serialize(document);
    writer.close();
    return true;
}
Also used : Path(org.eclipse.core.runtime.Path) ErrorHandler(org.xml.sax.ErrorHandler) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) Bundle(org.osgi.framework.Bundle) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) URL(java.net.URL) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File)

Example 10 with ErrorHandler

use of org.xml.sax.ErrorHandler in project tdi-studio-se by Talend.

the class ExportProjectSettings method saveProjectSettings.

public void saveProjectSettings() {
    if (path == null) {
        return;
    }
    File xmlFile = new File(path);
    org.talend.core.model.properties.Project project = pro.getEmfProject();
    try {
        final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
        DocumentBuilder analyseur = fabrique.newDocumentBuilder();
        analyseur.setErrorHandler(new ErrorHandler() {

            @Override
            public void error(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(final SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        Document document = analyseur.newDocument();
        //$NON-NLS-1$
        Element root = document.createElement("exportParameters");
        createVersionAttr(document, root);
        document.appendChild(root);
        // status
        List technicals = project.getTechnicalStatus();
        //$NON-NLS-1$
        createStatus(technicals, document, root, "technicalStatus");
        List documentation = project.getDocumentationStatus();
        //$NON-NLS-1$
        createStatus(documentation, document, root, "documentationStatus");
        // security
        //$NON-NLS-1$
        Element security = document.createElement("exportParameter");
        root.appendChild(security);
        //$NON-NLS-1$
        Attr typeAttr = document.createAttribute("type");
        //$NON-NLS-1$
        typeAttr.setNodeValue("security");
        security.setAttributeNode(typeAttr);
        //$NON-NLS-1$
        Attr name = document.createAttribute("name");
        //$NON-NLS-1$
        name.setNodeValue("hidePassword");
        security.setAttributeNode(name);
        security.setTextContent(String.valueOf(project.isHidePassword()));
        // stats and logs
        if (project.getStatAndLogsSettings() != null) {
            List statAndLogs = project.getStatAndLogsSettings().getParameters().getElementParameter();
            //$NON-NLS-1$
            saveParameters(document, root, statAndLogs, "statAndLogs");
        }
        // implicit context
        if (project.getImplicitContextSettings() != null) {
            List implicit = project.getImplicitContextSettings().getParameters().getElementParameter();
            //$NON-NLS-1$
            saveParameters(document, root, implicit, "implicitContext");
        }
        // palette
        List componentSettings = project.getComponentsSettings();
        savePalette(document, root, componentSettings);
        saveDocumentByEncoding(document, xmlFile);
    } catch (ParserConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)38 SAXException (org.xml.sax.SAXException)36 SAXParseException (org.xml.sax.SAXParseException)33 IOException (java.io.IOException)16 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)14 Document (org.w3c.dom.Document)13 InputSource (org.xml.sax.InputSource)9 InputStream (java.io.InputStream)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 Element (org.w3c.dom.Element)7 FileInputStream (java.io.FileInputStream)6 EntityResolver (org.xml.sax.EntityResolver)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 StringReader (java.io.StringReader)4 OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)3 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)3 Attr (org.w3c.dom.Attr)3 NodeList (org.w3c.dom.NodeList)3