Search in sources :

Example 16 with SAXParseException

use of org.xml.sax.SAXParseException 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 17 with SAXParseException

use of org.xml.sax.SAXParseException 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 18 with SAXParseException

use of org.xml.sax.SAXParseException 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)

Example 19 with SAXParseException

use of org.xml.sax.SAXParseException in project ACS by ACS-Community.

the class ScriptFilter method readStatusFile.

private void readStatusFile(String filename, boolean startup) throws ParserConfigurationException, SAXException, IOException {
    try {
        if (startup) {
            int n = JOptionPane.showConfirmDialog(this, "An old status file has been found\nWould you like to load it?", "Old status file found", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
            if (n == JOptionPane.NO_OPTION) {
                return;
            }
        }
        if (validateStatusFile(filename)) {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(filename));
            doc.getDocumentElement().normalize();
            NodeList listOfSamplingGroup = doc.getElementsByTagName("SamplingGroup");
            for (int s = 0; s < listOfSamplingGroup.getLength(); s++) {
                Node firstSamplingGroup = listOfSamplingGroup.item(s);
                if (firstSamplingGroup.getNodeType() == Node.ELEMENT_NODE) {
                    Element firstSamplingGroupElement = (Element) firstSamplingGroup;
                    // -------
                    NodeList nameList = firstSamplingGroupElement.getElementsByTagName("SamplingGroupName");
                    Element nameElement = (Element) nameList.item(0);
                    NodeList nameText = nameElement.getChildNodes();
                    String samplingGroupName = ((Node) nameText.item(0)).getNodeValue().trim();
                    // MAN_NAME
                    NodeList manNameList = firstSamplingGroupElement.getElementsByTagName("SamplingManagerName");
                    Element manNameElement = (Element) manNameList.item(0);
                    NodeList manNameText = manNameElement.getChildNodes();
                    String manName = ((Node) manNameText.item(0)).getNodeValue().trim();
                    String[] managers = SampTool.getSamplingManagers();
                    for (int i = 0; i < managers.length; i++) {
                        if (managers[i].equals(manName)) {
                            correctManName = true;
                        }
                    }
                    if (correctManName) {
                        MAN_NAME = manName;
                    } else {
                        System.out.println("ERROR: The Sampling Manager Name on the loaded file doesn't exist.");
                        return;
                    }
                    //-------
                    NodeList freqList = firstSamplingGroupElement.getElementsByTagName("Frequency");
                    Element freqElement = (Element) freqList.item(0);
                    NodeList freqText = freqElement.getChildNodes();
                    double frequency = Double.parseDouble(((Node) freqText.item(0)).getNodeValue().trim());
                    //-------
                    NodeList stList = firstSamplingGroupElement.getElementsByTagName("SamplingTime");
                    Element stElement = (Element) stList.item(0);
                    NodeList stText = stElement.getChildNodes();
                    int samplingtime = Integer.parseInt(((Node) stText.item(0)).getNodeValue().trim());
                    //-------
                    NodeList twList = firstSamplingGroupElement.getElementsByTagName("TimeWindow");
                    Element twElement = (Element) twList.item(0);
                    NodeList twText = twElement.getChildNodes();
                    int timewindow = Integer.parseInt(((Node) twText.item(0)).getNodeValue().trim());
                    //----- Properties
                    NodeList listProperties = firstSamplingGroupElement.getElementsByTagName("Sample");
                    for (int o = 0; o < listProperties.getLength(); o++) {
                        Node firstProperty = listProperties.item(o);
                        if (firstProperty.getNodeType() == Node.ELEMENT_NODE) {
                            Element firstPropertyElement = (Element) firstProperty;
                            //-------
                            NodeList compList = firstPropertyElement.getElementsByTagName("component");
                            Element compElement = (Element) compList.item(0);
                            NodeList compText = compElement.getChildNodes();
                            String component = ((Node) compText.item(0)).getNodeValue().trim();
                            //-------
                            NodeList propList = firstPropertyElement.getElementsByTagName("property");
                            Element propElement = (Element) propList.item(0);
                            NodeList propText = propElement.getChildNodes();
                            String property = ((Node) propText.item(0)).getNodeValue().trim();
                            boolean checkStatus = checkComponentProperty(component, property);
                            System.out.println("opening " + component + ", " + property + ", " + samplingGroupName);
                            if (checkStatus) {
                                addToSampling(component, property, samplingGroupName);
                            } else {
                                JOptionPane.showMessageDialog(this, "The component or the property in:\n\n- " + component + "#" + property + "\n\n are invalid on the Sampling Group: '" + samplingGroupName + "'");
                            }
                        }
                    }
                    for (BeanGrouper bg : BeanGrouperList) {
                        if (bg.getGroupName().equals(samplingGroupName)) {
                            bg.loadConfiguration(frequency, timewindow, samplingtime);
                            break;
                        }
                    }
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Your XML file have a bad format, please check it");
        }
    } catch (SAXParseException err) {
        System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
        System.out.println(" " + err.getMessage());
    } catch (SAXException e) {
        Exception x = e.getException();
        ((x == null) ? e : x).printStackTrace();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) SamplingManagerException(cl.utfsm.samplingSystemUI.core.SamplingManagerException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File)

Example 20 with SAXParseException

use of org.xml.sax.SAXParseException in project perun by CESNET.

the class MuPasswordManagerModule method parseResponse.

/**
	 * Parse XML response from IS MU to XML document.
	 *
	 * @param inputStream Stream to be parsed to Document
	 * @param requestID ID of request made to IS MU.
	 * @return XML document for further processing
	 * @throws InternalErrorException
	 */
private Document parseResponse(InputStream inputStream, int requestID) throws InternalErrorException {
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new InternalErrorException("Error when creating newDocumentBuilder. Request ID: " + requestID, ex);
    }
    String response = null;
    try {
        response = convertStreamToString(inputStream, "UTF-8");
    } catch (IOException ex) {
        log.error("Unable to convert InputStream to String: {}", ex);
    }
    log.trace("Request ID: " + requestID + " Response: " + response);
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(response)));
    } catch (SAXParseException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder. Request ID: " + requestID, ex);
    } catch (SAXException ex) {
        throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters. Request ID: " + requestID, ex);
    } catch (IOException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output. Request ID: " + requestID, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression isErrorExpr;
    XPathExpression getErrorTextExpr;
    XPathExpression getDbErrorTextExpr;
    try {
        isErrorExpr = xpath.compile("//resp/stav/text()");
        getErrorTextExpr = xpath.compile("//resp/error/text()");
        getDbErrorTextExpr = xpath.compile("//resp/dberror/text()");
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
    }
    // OK or ERROR
    String responseStatus;
    try {
        responseStatus = (String) isErrorExpr.evaluate(doc, XPathConstants.STRING);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on document to resolve response status. Request ID: " + requestID, ex);
    }
    log.trace("Request ID: " + requestID + " Response status: " + responseStatus);
    if ("OK".equals(responseStatus)) {
        return doc;
    } else {
        try {
            String error = (String) getErrorTextExpr.evaluate(doc, XPathConstants.STRING);
            if (error == null || error.isEmpty()) {
                error = (String) getDbErrorTextExpr.evaluate(doc, XPathConstants.STRING);
            }
            throw new InternalErrorException("IS MU (password manager backend) responded with error to a Request ID: " + requestID + " Error: " + error);
        } catch (XPathExpressionException ex) {
            throw new InternalErrorException("Error when evaluate xpath query on document to resolve error status. Request ID: " + requestID, ex);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)365 SAXException (org.xml.sax.SAXException)170 IOException (java.io.IOException)131 DocumentBuilder (javax.xml.parsers.DocumentBuilder)73 InputSource (org.xml.sax.InputSource)69 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)68 Document (org.w3c.dom.Document)64 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)56 ErrorHandler (org.xml.sax.ErrorHandler)52 InputStream (java.io.InputStream)36 File (java.io.File)34 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)31 FileNotFoundException (java.io.FileNotFoundException)31 Element (org.w3c.dom.Element)30 StringReader (java.io.StringReader)21 NodeList (org.w3c.dom.NodeList)21 URL (java.net.URL)20 Test (org.junit.jupiter.api.Test)19 Node (org.w3c.dom.Node)19