Search in sources :

Example 1 with ErrorHandler

use of org.xml.sax.ErrorHandler in project jphp by jphp-compiler.

the class WrapXmlProcessor method __construct.

@Signature
public void __construct(final Environment env) throws ParserConfigurationException, TransformerConfigurationException {
    transformerFactory = TransformerFactory.newInstance();
    transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    builderFactory = DocumentBuilderFactory.newInstance();
    builder = builderFactory.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) throws SAXException {
            if (onWarning != null) {
                onWarning.callAny(new JavaException(env, exception));
            }
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            if (onError != null) {
                onError.callAny(new JavaException(env, exception));
            }
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            if (onFatalError != null) {
                onFatalError.callAny(new JavaException(env, exception));
            } else {
                throw exception;
            }
        }
    });
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) JavaException(php.runtime.ext.java.JavaException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) Signature(php.runtime.annotation.Reflection.Signature)

Example 2 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 3 with ErrorHandler

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

the class AutoConvertTypesUtils method load.

public static List<AutoConversionType> load(File file) {
    beanList = new ArrayList<>();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder analyseur = documentBuilderFactory.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.parse(file);
        //$NON-NLS-1$
        NodeList typeNodes = document.getElementsByTagName("conversionType");
        for (int i = 0; i < typeNodes.getLength(); i++) {
            Node typeNode = typeNodes.item(i);
            NamedNodeMap typeAttributes = typeNode.getAttributes();
            AutoConversionType typeObj = new AutoConversionType();
            //$NON-NLS-1$
            typeObj.setSourceDataType(typeAttributes.getNamedItem("source").getNodeValue());
            //$NON-NLS-1$
            typeObj.setTargetDataType(typeAttributes.getNamedItem("target").getNodeValue());
            //$NON-NLS-1$
            typeObj.setConversionFunction(typeAttributes.getNamedItem("function").getNodeValue());
            beanList.add(typeObj);
        }
    } catch (Exception e) {
        return beanList;
    }
    return beanList;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) PersistenceException(org.talend.commons.exception.PersistenceException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) AutoConversionType(org.talend.core.model.metadata.types.AutoConversionType) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException)

Example 4 with ErrorHandler

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

the class GenerateSpagoBIXML method createSpagoBIXML.

private void createSpagoBIXML() {
    if (file != null) {
        try {
            Project project = ((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getProject();
            final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
            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 spagobi = document.createElement("etl");
            document.appendChild(spagobi);
            // ///////////////////add job element.
            //$NON-NLS-1$
            Element projectElement = document.createElement("job");
            spagobi.appendChild(projectElement);
            //$NON-NLS-1$
            Attr attr = document.createAttribute("project");
            attr.setNodeValue(project.getEmfProject().getLabel());
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("jobName");
            attr.setNodeValue(item.getProperty().getLabel());
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("context");
            attr.setNodeValue(contextName);
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("language");
            attr.setNodeValue(project.getLanguage().getName());
            projectElement.setAttributeNode(attr);
            XMLSerializer serializer = new XMLSerializer();
            OutputFormat outputFormat = new OutputFormat();
            outputFormat.setIndenting(true);
            serializer.setOutputFormat(outputFormat);
            serializer.setOutputCharStream(new java.io.FileWriter(file));
            serializer.serialize(document);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) RepositoryContext(org.talend.core.context.RepositoryContext) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) Project(org.talend.core.model.general.Project) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException)

Example 5 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)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)69 SAXException (org.xml.sax.SAXException)59 SAXParseException (org.xml.sax.SAXParseException)55 DocumentBuilder (javax.xml.parsers.DocumentBuilder)26 IOException (java.io.IOException)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)24 Document (org.w3c.dom.Document)20 InputSource (org.xml.sax.InputSource)17 FileInputStream (java.io.FileInputStream)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 Element (org.w3c.dom.Element)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 Validator (javax.xml.validation.Validator)8 EntityResolver (org.xml.sax.EntityResolver)8 StreamSource (javax.xml.transform.stream.StreamSource)6 Schema (javax.xml.validation.Schema)6 SchemaFactory (javax.xml.validation.SchemaFactory)6 Node (org.w3c.dom.Node)5