Search in sources :

Example 1 with XMLNode

use of org.talend.xml.sax.simpleparser.model.XMLNode in project tdi-studio-se by Talend.

the class SimpleSAXLoopHandler method startElement.

/*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
     * org.xml.sax.Attributes)
     */
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    this.currentPath = this.currentPath + "/" + qName;
    for (XMLNode node : nodes.getNodesCollection()) {
        node.outputText = false;
        if (this.currentPath.equals(node.loopPath)) {
            node.isLooping = true;
        }
        if (node.isLooping) {
            if (node.isAsXML && (this.currentPath.equals(node.nodePath) || this.currentPath.startsWith(node.nodePath + "/"))) {
                node.addTextValue("<");
                node.addTextValue(qName);
                if (attributes.getLength() > 0) {
                    for (int m = 0; m < attributes.getLength(); m++) {
                        node.addTextValue(" ");
                        node.addTextValue(attributes.getQName(m));
                        node.addTextValue("=");
                        node.addTextValue("\"");
                        node.addTextValue(escapeEntityHelper.escapeAttributeEntities(attributes.getValue(m)));
                        node.addTextValue("\"");
                    }
                }
                node.outputText = true;
                node.hasValue = false;
                node.addTextValue(">");
            } else if (node.isDot && (this.currentPath.equals(node.nodePath) || this.currentPath.startsWith(node.nodePath + "/"))) {
                node.outputText = true;
                node.hasValue = false;
            } else {
                int index = node.nodePath.lastIndexOf("@");
                if (index > 0) {
                    if (currentPath.equals(node.nodePath.substring(0, index - 1))) {
                        String attribute = attributes.getValue(node.nodePath.substring(index + 1));
                        if (attribute != null && false == node.hasValue) {
                            node.addTextValue(attribute);
                            node.hasValue = true;
                        }
                    }
                } else {
                    if (currentPath.equals(node.nodePath)) {
                        node.outputText = true;
                    }
                }
            }
        } else {
            //process the attribute out of the loop
            int index = node.nodePath.lastIndexOf("@");
            if (index > 0) {
                if (currentPath.equals(node.nodePath.substring(0, index - 1))) {
                    String attribute = attributes.getValue(node.nodePath.substring(index + 1));
                    if (attribute != null) {
                        node.addTextValue(attribute);
                    } else {
                        node.addTextValue("");
                    }
                }
            }
        }
    }
}
Also used : XMLNode(org.talend.xml.sax.simpleparser.model.XMLNode)

Example 2 with XMLNode

use of org.talend.xml.sax.simpleparser.model.XMLNode in project tdi-studio-se by Talend.

the class SimpleSAXLoopHandler method endElement.

/*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
public void endElement(String uri, String localName, String qName) throws SAXException {
    for (XMLNode node : nodes.getNodesCollection()) {
        if (node.isLooping) {
            if (node.outputText) {
                if (node.hasValue == false) {
                    node.addTextValue("");
                }
                node.hasValue = true;
            }
            if ((node.isAsXML || node.isDot) && (this.currentPath.equals(node.nodePath) || this.currentPath.startsWith(node.nodePath + "/"))) {
                if (node.isAsXML) {
                    node.addTextValue("</");
                    node.addTextValue(qName);
                    node.addTextValue(">");
                }
                if (this.currentPath.equals(node.nodePath)) {
                    node.hasValue = true;
                }
            }
        }
        node.outputText = false;
    }
    if (this.currentPath.equals(nodes.getLoopPath())) {
        if (isNotNull(nodes.getNodesCollection(), nodes.size())) {
            Map<String, String> map = new HashMap<String, String>();
            for (XMLNode node : nodes.getNodesCollection()) {
                map.put(node.originPath, node.getTextValue());
            }
            if (multiCache != null) {
                HashMap<String, Map<String, String>> row = new HashMap<String, Map<String, String>>(1);
                row.put(nodes.getOriginalLoopPath(), map);
                multiCache.writeData(row);
            } else {
                bufferCache.writeData(map);
            }
            if (stop) {
                throw new EnoughDataException("Get enough data,now stop the xml parse action");
            }
        }
        nodes.resetAll();
    }
    this.currentPath = this.currentPath.substring(0, this.currentPath.lastIndexOf("/"));
}
Also used : XMLNode(org.talend.xml.sax.simpleparser.model.XMLNode) HashMap(java.util.HashMap) EnoughDataException(org.talend.xml.sax.exception.EnoughDataException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with XMLNode

use of org.talend.xml.sax.simpleparser.model.XMLNode in project tdi-studio-se by Talend.

the class SimpleSAXLooper method initLoopEntries.

private void initLoopEntries() {
    multiCache = DataBufferCache2.getInstance();
    for (XMLNodes ns : nodesList) {
        for (XMLNode node : ns.getNodes().values()) {
            String column = node.originPath;
            String resultCol = node.loopPath;
            String[] splits = column.split("/");
            for (String tmp : splits) {
                if (tmp.equals("..")) {
                    resultCol = resultCol.substring(0, resultCol.lastIndexOf("/"));
                    node.setAttrOutOfLoop(true);
                } else if (tmp.equals(".")) {
                    node.isDot = true;
                } else {
                    resultCol += "/" + tmp;
                }
            }
            node.nodePath = resultCol;
        }
    }
}
Also used : XMLNode(org.talend.xml.sax.simpleparser.model.XMLNode) XMLNodes(org.talend.xml.sax.simpleparser.model.XMLNodes)

Example 4 with XMLNode

use of org.talend.xml.sax.simpleparser.model.XMLNode in project tdi-studio-se by Talend.

the class SimpleSAXLooper method initLoopEntry.

private void initLoopEntry() {
    bcache = DataBufferCache.getInstance();
    // parse the node path to loopEntry
    for (XMLNode node : nodes.getNodes().values()) {
        String column = node.originPath;
        String resultCol = node.loopPath;
        String[] splits = column.split("/");
        for (String tmp : splits) {
            if (tmp.equals("..")) {
                resultCol = resultCol.substring(0, resultCol.lastIndexOf("/"));
                node.setAttrOutOfLoop(true);
            } else if (tmp.equals(".")) {
                node.isDot = true;
            } else {
                resultCol += "/" + tmp;
            }
        }
        node.nodePath = resultCol;
    }
}
Also used : XMLNode(org.talend.xml.sax.simpleparser.model.XMLNode)

Aggregations

XMLNode (org.talend.xml.sax.simpleparser.model.XMLNode)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EnoughDataException (org.talend.xml.sax.exception.EnoughDataException)1 XMLNodes (org.talend.xml.sax.simpleparser.model.XMLNodes)1