Search in sources :

Example 36 with KettleXMLException

use of org.pentaho.di.core.exception.KettleXMLException in project pentaho-kettle by pentaho.

the class MessagesSourceCrawler method crawl.

public void crawl() throws Exception {
    for (final String sourceDirectory : sourceDirectories) {
        FileObject folder = KettleVFS.getFileObject(sourceDirectory);
        FileObject[] javaFiles = folder.findFiles(new FileSelector() {

            @Override
            public boolean traverseDescendents(FileSelectInfo info) throws Exception {
                return true;
            }

            @Override
            public boolean includeFile(FileSelectInfo info) throws Exception {
                return info.getFile().getName().getExtension().equals("java");
            }
        });
        for (FileObject javaFile : javaFiles) {
            /**
             * We don't want the Messages.java files, there is nothing in there for us.
             */
            boolean skip = false;
            for (String filename : filesToAvoid) {
                if (javaFile.getName().getBaseName().equals(filename)) {
                    skip = true;
                }
            }
            if (skip) {
                // don't process this file.
                continue;
            }
            // For each of these files we look for keys...
            // 
            lookForOccurrencesInFile(sourceDirectory, javaFile);
        }
    }
    // 
    for (SourceCrawlerXMLFolder xmlFolder : xmlFolders) {
        String[] xmlDirs = { xmlFolder.getFolder() };
        String[] xmlMasks = { xmlFolder.getWildcard() };
        String[] xmlReq = { "N" };
        // search sub-folders too
        boolean[] xmlSubdirs = { true };
        FileInputList xulFileInputList = FileInputList.createFileList(new Variables(), xmlDirs, xmlMasks, xmlReq, xmlSubdirs);
        for (FileObject fileObject : xulFileInputList.getFiles()) {
            try {
                Document doc = XMLHandler.loadXMLFile(fileObject);
                // 
                for (SourceCrawlerXMLElement xmlElement : xmlFolder.getElements()) {
                    addLabelOccurrences(xmlFolder.getDefaultSourceFolder(), fileObject, doc.getElementsByTagName(xmlElement.getSearchElement()), xmlFolder.getKeyPrefix(), xmlElement.getKeyTag(), xmlElement.getKeyAttribute(), xmlFolder.getDefaultPackage(), xmlFolder.getPackageExceptions());
                }
            } catch (KettleXMLException e) {
                log.logError("Unable to open XUL / XML document: " + fileObject);
            }
        }
    }
}
Also used : FileSelector(org.apache.commons.vfs2.FileSelector) Document(org.w3c.dom.Document) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) Variables(org.pentaho.di.core.variables.Variables) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) FileObject(org.apache.commons.vfs2.FileObject) FileInputList(org.pentaho.di.core.fileinput.FileInputList)

Example 37 with KettleXMLException

use of org.pentaho.di.core.exception.KettleXMLException in project pentaho-kettle by pentaho.

the class XMLJoin method processRow.

@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (XMLJoinMeta) smi;
    data = (XMLJoinData) sdi;
    XPath xpath = XPathFactory.newInstance().newXPath();
    // if first row we do some initializing and process the first row of the target XML Step
    if (first) {
        first = false;
        int target_field_id = -1;
        XMLJoinMeta meta = (XMLJoinMeta) smi;
        // Get the two input row sets
        data.TargetRowSet = findInputRowSet(meta.getTargetXMLstep());
        data.SourceRowSet = findInputRowSet(meta.getSourceXMLstep());
        // get the first line from the target row set
        Object[] rTarget = getRowFrom(data.TargetRowSet);
        if (rTarget == null) {
            // nothing to do
            logBasic(BaseMessages.getString(PKG, "XMLJoin.NoRowsFoundInTarget"));
            setOutputDone();
            return false;
        }
        // get target xml
        String[] target_field_names = data.TargetRowSet.getRowMeta().getFieldNames();
        for (int i = 0; i < target_field_names.length; i++) {
            if (meta.getTargetXMLfield().equals(target_field_names[i])) {
                target_field_id = i;
            }
        }
        // Throw exception if target field has not been found
        if (target_field_id == -1) {
            throw new KettleException(BaseMessages.getString(PKG, "XMLJoin.Exception.FieldNotFound", meta.getTargetXMLfield()));
        }
        data.outputRowMeta = data.TargetRowSet.getRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), new RowMetaInterface[] { data.TargetRowSet.getRowMeta() }, null, getTransMeta(), repository, metaStore);
        data.outputRowData = rTarget.clone();
        // get the target xml structure and create a DOM
        String strTarget = (String) rTarget[target_field_id];
        // parse the XML as a W3C Document
        InputSource inputSource = new InputSource(new StringReader(strTarget));
        data.XPathStatement = meta.getTargetXPath();
        try {
            DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder();
            data.targetDOM = builder.parse(inputSource);
            if (!meta.isComplexJoin()) {
                data.targetNode = (Node) xpath.evaluate(data.XPathStatement, data.targetDOM, XPathConstants.NODE);
                if (data.targetNode == null) {
                    throw new KettleXMLException("XPath statement returned no result [" + data.XPathStatement + "]");
                }
            }
        } catch (Exception e) {
            throw new KettleXMLException(e);
        }
    }
    // This also waits for a row to be finished.
    Object[] rJoinSource = getRowFrom(data.SourceRowSet);
    if (rJoinSource == null) {
        // no more input to be expected... create the output row
        try {
            if (meta.isOmitNullValues()) {
                removeEmptyNodes(data.targetDOM.getChildNodes());
            }
            // create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult resultXML = new StreamResult(sw);
            DOMSource source = new DOMSource(data.targetDOM);
            getTransformer().transform(source, resultXML);
            int outputIndex = data.outputRowMeta.size() - 1;
            // send the row to the next steps...
            putRow(data.outputRowMeta, RowDataUtil.addValueData(data.outputRowData, outputIndex, sw.toString()));
            // finishing up
            setOutputDone();
            return false;
        } catch (Exception e) {
            throw new KettleException(e);
        }
    } else {
        if (data.iSourceXMLField == -1) {
            // assume failure
            // get the column of the join xml set
            // get target xml
            String[] source_field_names = data.SourceRowSet.getRowMeta().getFieldNames();
            for (int i = 0; i < source_field_names.length; i++) {
                if (meta.getSourceXMLfield().equals(source_field_names[i])) {
                    data.iSourceXMLField = i;
                }
            }
            // Throw exception if source xml field has not been found
            if (data.iSourceXMLField == -1) {
                throw new KettleException(BaseMessages.getString(PKG, "XMLJoin.Exception.FieldNotFound", meta.getSourceXMLfield()));
            }
        }
        if (meta.isComplexJoin() && data.iCompareFieldID == -1) {
            // get the column of the compare value
            String[] source_field_names = data.SourceRowSet.getRowMeta().getFieldNames();
            for (int i = 0; i < source_field_names.length; i++) {
                if (meta.getJoinCompareField().equals(source_field_names[i])) {
                    data.iCompareFieldID = i;
                }
            }
            // Throw exception if source xml field has not been found
            if (data.iCompareFieldID == -1) {
                throw new KettleException(BaseMessages.getString(PKG, "XMLJoin.Exception.FieldNotFound", meta.getJoinCompareField()));
            }
        }
        // get XML tags to join
        String strJoinXML = (String) rJoinSource[data.iSourceXMLField];
        try {
            DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder();
            Document joinDocument = builder.parse(new InputSource(new StringReader(strJoinXML)));
            Node node = data.targetDOM.importNode(joinDocument.getDocumentElement(), true);
            if (meta.isComplexJoin()) {
                String strCompareValue = rJoinSource[data.iCompareFieldID].toString();
                String strXPathStatement = data.XPathStatement.replace("?", strCompareValue);
                data.targetNode = (Node) xpath.evaluate(strXPathStatement, data.targetDOM, XPathConstants.NODE);
                if (data.targetNode == null) {
                    throw new KettleXMLException("XPath statement returned no result [" + strXPathStatement + "]");
                }
            }
            data.targetNode.appendChild(node);
        } catch (Exception e) {
            throw new KettleException(e);
        }
    }
    return true;
}
Also used : XPath(javax.xml.xpath.XPath) KettleException(org.pentaho.di.core.exception.KettleException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException)

Example 38 with KettleXMLException

use of org.pentaho.di.core.exception.KettleXMLException in project pentaho-kettle by pentaho.

the class XMLJoinMeta method readData.

private void readData(Node stepnode) throws KettleXMLException {
    try {
        valueXMLfield = XMLHandler.getTagValue(stepnode, "valueXMLfield");
        targetXMLstep = XMLHandler.getTagValue(stepnode, "targetXMLstep");
        targetXMLfield = XMLHandler.getTagValue(stepnode, "targetXMLfield");
        sourceXMLstep = XMLHandler.getTagValue(stepnode, "sourceXMLstep");
        sourceXMLfield = XMLHandler.getTagValue(stepnode, "sourceXMLfield");
        targetXPath = XMLHandler.getTagValue(stepnode, "targetXPath");
        joinCompareField = XMLHandler.getTagValue(stepnode, "joinCompareField");
        encoding = XMLHandler.getTagValue(stepnode, "encoding");
        complexJoin = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "complexJoin"));
        omitXMLHeader = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "omitXMLHeader"));
        omitNullValues = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "omitNullValues"));
    } catch (Exception e) {
        throw new KettleXMLException("Unable to load step info from XML", e);
    }
}
Also used : KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 39 with KettleXMLException

use of org.pentaho.di.core.exception.KettleXMLException in project pentaho-kettle by pentaho.

the class XsdValidatorMeta method readData.

private void readData(Node stepnode) throws KettleXMLException {
    try {
        xsdFilename = XMLHandler.getTagValue(stepnode, "xdsfilename");
        xmlStream = XMLHandler.getTagValue(stepnode, "xmlstream");
        resultFieldname = XMLHandler.getTagValue(stepnode, "resultfieldname");
        xsdDefinedField = XMLHandler.getTagValue(stepnode, "xsddefinedfield");
        xsdSource = XMLHandler.getTagValue(stepnode, "xsdsource");
        addValidationMessage = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "addvalidationmsg"));
        validationMessageField = XMLHandler.getTagValue(stepnode, "validationmsgfield");
        ifXmlValid = XMLHandler.getTagValue(stepnode, "ifxmlvalid");
        ifXmlInvalid = XMLHandler.getTagValue(stepnode, "ifxmlunvalid");
        outputStringField = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "outputstringfield"));
        xmlSourceFile = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "xmlsourcefile"));
    } catch (Exception e) {
        throw new KettleXMLException(BaseMessages.getString(PKG, "XsdValidatorMeta.Exception.UnableToLoadStepInfoFromXML"), e);
    }
}
Also used : KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 40 with KettleXMLException

use of org.pentaho.di.core.exception.KettleXMLException in project pentaho-kettle by pentaho.

the class XsltMeta method readData.

private void readData(Node stepnode) throws KettleXMLException {
    try {
        xslFilename = XMLHandler.getTagValue(stepnode, "xslfilename");
        fieldName = XMLHandler.getTagValue(stepnode, "fieldname");
        resultFieldname = XMLHandler.getTagValue(stepnode, "resultfieldname");
        xslFileField = XMLHandler.getTagValue(stepnode, "xslfilefield");
        xslFileFieldUse = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "xslfilefielduse"));
        String isAFile = XMLHandler.getTagValue(stepnode, "xslfieldisafile");
        if (xslFileFieldUse && Utils.isEmpty(isAFile)) {
            xslFieldIsAFile = true;
        } else {
            xslFieldIsAFile = "Y".equalsIgnoreCase(isAFile);
        }
        xslFactory = XMLHandler.getTagValue(stepnode, "xslfactory");
        Node parametersNode = XMLHandler.getSubNode(stepnode, "parameters");
        int nrparams = XMLHandler.countNodes(parametersNode, "parameter");
        Node parametersOutputProps = XMLHandler.getSubNode(stepnode, "outputproperties");
        int nroutputprops = XMLHandler.countNodes(parametersOutputProps, "outputproperty");
        allocate(nrparams, nroutputprops);
        for (int i = 0; i < nrparams; i++) {
            Node anode = XMLHandler.getSubNodeByNr(parametersNode, "parameter", i);
            parameterField[i] = XMLHandler.getTagValue(anode, "field");
            parameterName[i] = XMLHandler.getTagValue(anode, "name");
        }
        for (int i = 0; i < nroutputprops; i++) {
            Node anode = XMLHandler.getSubNodeByNr(parametersOutputProps, "outputproperty", i);
            outputPropertyName[i] = XMLHandler.getTagValue(anode, "name");
            outputPropertyValue[i] = XMLHandler.getTagValue(anode, "value");
        }
    } catch (Exception e) {
        throw new KettleXMLException(BaseMessages.getString(PKG, "XsltMeta.Exception.UnableToLoadStepInfoFromXML"), e);
    }
}
Also used : Node(org.w3c.dom.Node) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Aggregations

KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)286 KettleException (org.pentaho.di.core.exception.KettleException)209 Node (org.w3c.dom.Node)164 KettleStepException (org.pentaho.di.core.exception.KettleStepException)150 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)25 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)23 KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)20 Document (org.w3c.dom.Document)13 IOException (java.io.IOException)10 KettleValueException (org.pentaho.di.core.exception.KettleValueException)10 StringObjectId (org.pentaho.di.repository.StringObjectId)8 KettleFileException (org.pentaho.di.core.exception.KettleFileException)7 FileNotFoundException (java.io.FileNotFoundException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 StreamInterface (org.pentaho.di.trans.step.errorhandling.StreamInterface)5 InputStream (java.io.InputStream)4 MalformedURLException (java.net.MalformedURLException)4 ParseException (java.text.ParseException)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)4