Search in sources :

Example 66 with NodeList

use of org.w3c.dom.NodeList in project translationstudio8 by heartsome.

the class SaxonSearcher method PropagateQurey.

/**
	 * 繁殖翻译文本段的查询 robert 2012-04-03
	 * @param queryString
	 *            XQuery查询语句
	 * @return RowId集合
	 * @throws XQException
	 *             ;
	 */
public static Map<String, List<String>> PropagateQurey(String queryString) throws XQException {
    XQDataSource dataSource = new SaxonXQDataSource();
    XQConnection conn = null;
    XQExpression expression = null;
    XQSequence results = null;
    try {
        conn = dataSource.getConnection();
        expression = conn.createExpression();
        results = expression.executeQuery(queryString);
        Map<String, List<String>> resultMap = new HashMap<String, List<String>>();
        while (results.next()) {
            Node node = results.getNode();
            //				System.out.println("node.getChildNodes().getLength() = " + node.getChildNodes().getLength());
            if (node.getChildNodes().getLength() >= 1) {
                String rootFileName = node.getAttributes().getNamedItem("fileName").getNodeValue();
                rootFileName = new File(rootFileName).getAbsolutePath();
                String rootOriginal = node.getAttributes().getNamedItem("original").getNodeValue();
                String rootTuid = node.getAttributes().getNamedItem("tuid").getNodeValue();
                String rootRowId = RowIdUtil.getRowId(rootFileName, rootOriginal, rootTuid);
                if (!resultMap.keySet().contains(rootRowId)) {
                    resultMap.put(rootRowId, new ArrayList<String>());
                }
                NodeList nodeList = node.getChildNodes();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    if (nodeList.item(i).getAttributes() == null) {
                        continue;
                    }
                    String fileName = nodeList.item(i).getAttributes().getNamedItem("fileName").getNodeValue();
                    fileName = new File(fileName).getAbsolutePath();
                    String original = nodeList.item(i).getAttributes().getNamedItem("original").getNodeValue();
                    String tuid = nodeList.item(i).getAttributes().getNamedItem("tuid").getNodeValue();
                    String rowId = RowIdUtil.getRowId(fileName, original, tuid);
                    resultMap.get(rootRowId).add(rowId);
                }
            }
        }
        return resultMap;
    } finally {
        // 释放资源
        if (results != null && !results.isClosed()) {
            results.close();
        }
        if (expression != null && !expression.isClosed()) {
            expression.close();
        }
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) SaxonXQDataSource(net.sf.saxon.xqj.SaxonXQDataSource) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SaxonXQDataSource(net.sf.saxon.xqj.SaxonXQDataSource) XQDataSource(javax.xml.xquery.XQDataSource) XQSequence(javax.xml.xquery.XQSequence) XQExpression(javax.xml.xquery.XQExpression) XQConnection(javax.xml.xquery.XQConnection) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 67 with NodeList

use of org.w3c.dom.NodeList in project camel by apache.

the class BomGeneratorMojo method overwriteDependencyManagement.

private void overwriteDependencyManagement(Document pom, List<Dependency> dependencies) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("/project/dependencyManagement/dependencies");
    NodeList nodes = (NodeList) expr.evaluate(pom, XPathConstants.NODESET);
    if (nodes.getLength() == 0) {
        throw new IllegalStateException("No dependencies found in the dependencyManagement section of the current pom");
    }
    Node dependenciesSection = nodes.item(0);
    // cleanup the dependency management section
    while (dependenciesSection.hasChildNodes()) {
        Node child = dependenciesSection.getFirstChild();
        dependenciesSection.removeChild(child);
    }
    for (Dependency dep : dependencies) {
        Element dependencyEl = pom.createElement("dependency");
        Element groupIdEl = pom.createElement("groupId");
        groupIdEl.setTextContent(dep.getGroupId());
        dependencyEl.appendChild(groupIdEl);
        Element artifactIdEl = pom.createElement("artifactId");
        artifactIdEl.setTextContent(dep.getArtifactId());
        dependencyEl.appendChild(artifactIdEl);
        Element versionEl = pom.createElement("version");
        versionEl.setTextContent(dep.getVersion());
        dependencyEl.appendChild(versionEl);
        if (!"jar".equals(dep.getType())) {
            Element typeEl = pom.createElement("type");
            typeEl.setTextContent(dep.getType());
            dependencyEl.appendChild(typeEl);
        }
        if (dep.getClassifier() != null) {
            Element classifierEl = pom.createElement("classifier");
            classifierEl.setTextContent(dep.getClassifier());
            dependencyEl.appendChild(classifierEl);
        }
        if (dep.getScope() != null && !"compile".equals(dep.getScope())) {
            Element scopeEl = pom.createElement("scope");
            scopeEl.setTextContent(dep.getScope());
            dependencyEl.appendChild(scopeEl);
        }
        if (dep.getExclusions() != null && !dep.getExclusions().isEmpty()) {
            Element exclsEl = pom.createElement("exclusions");
            for (Exclusion e : dep.getExclusions()) {
                Element exclEl = pom.createElement("exclusion");
                Element groupIdExEl = pom.createElement("groupId");
                groupIdExEl.setTextContent(e.getGroupId());
                exclEl.appendChild(groupIdExEl);
                Element artifactIdExEl = pom.createElement("artifactId");
                artifactIdExEl.setTextContent(e.getArtifactId());
                exclEl.appendChild(artifactIdExEl);
                exclsEl.appendChild(exclEl);
            }
            dependencyEl.appendChild(exclsEl);
        }
        dependenciesSection.appendChild(dependencyEl);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Exclusion(org.apache.maven.model.Exclusion) Dependency(org.apache.maven.model.Dependency)

Example 68 with NodeList

use of org.w3c.dom.NodeList in project spring-framework by spring-projects.

the class ScheduledTasksBeanDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // lazy scheduled tasks are a contradiction in terms -> force to false
    builder.setLazyInit(false);
    ManagedList<RuntimeBeanReference> cronTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedDelayTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedRateTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> triggerTaskList = new ManagedList<>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (!isScheduledElement(child, parserContext)) {
            continue;
        }
        Element taskElement = (Element) child;
        String ref = taskElement.getAttribute("ref");
        String method = taskElement.getAttribute("method");
        // Check that 'ref' and 'method' are specified
        if (!StringUtils.hasText(ref) || !StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Both 'ref' and 'method' are required", taskElement);
            // Continue with the possible next task element
            continue;
        }
        String cronAttribute = taskElement.getAttribute("cron");
        String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
        String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
        String triggerAttribute = taskElement.getAttribute("trigger");
        String initialDelayAttribute = taskElement.getAttribute("initial-delay");
        boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
        boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
        boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
        boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
        boolean hasInitialDelayAttribute = StringUtils.hasText(initialDelayAttribute);
        if (!(hasCronAttribute || hasFixedDelayAttribute || hasFixedRateAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
            // with the possible next task element
            continue;
        }
        if (hasInitialDelayAttribute && (hasCronAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("the 'initial-delay' attribute may not be used with cron and trigger tasks", taskElement);
            // with the possible next task element
            continue;
        }
        String runnableName = runnableReference(ref, method, taskElement, parserContext).getBeanName();
        if (hasFixedDelayAttribute) {
            fixedDelayTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedDelayAttribute, taskElement, parserContext));
        }
        if (hasFixedRateAttribute) {
            fixedRateTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedRateAttribute, taskElement, parserContext));
        }
        if (hasCronAttribute) {
            cronTaskList.add(cronTaskReference(runnableName, cronAttribute, taskElement, parserContext));
        }
        if (hasTriggerAttribute) {
            String triggerName = new RuntimeBeanReference(triggerAttribute).getBeanName();
            triggerTaskList.add(triggerTaskReference(runnableName, triggerName, taskElement, parserContext));
        }
    }
    String schedulerRef = element.getAttribute("scheduler");
    if (StringUtils.hasText(schedulerRef)) {
        builder.addPropertyReference("taskScheduler", schedulerRef);
    }
    builder.addPropertyValue("cronTasksList", cronTaskList);
    builder.addPropertyValue("fixedDelayTasksList", fixedDelayTaskList);
    builder.addPropertyValue("fixedRateTasksList", fixedRateTaskList);
    builder.addPropertyValue("triggerTasksList", triggerTaskList);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 69 with NodeList

use of org.w3c.dom.NodeList in project platformlayer by platformlayer.

the class UntypedItemFormatter method visit.

@Override
public void visit(CliContext contextGeneric, UntypedItem o, OutputSink sink) throws IOException {
    PlatformLayerCliContext context = (PlatformLayerCliContext) contextGeneric;
    LinkedHashMap<String, Object> values = Maps.newLinkedHashMap();
    UntypedItemXml item = (UntypedItemXml) o;
    Element dataElement = item.getDataElement();
    // String xml = o.getModelData();
    //
    // Element documentElement;
    //
    // try {
    // Document dom = XmlHelper.parseXmlDocument(xml, false);
    // documentElement = dom.getDocumentElement();
    // } catch (Exception e) {
    // throw new IllegalArgumentException("Error parsing XML", e);
    // }
    values.put("key", Utils.formatUrl(context, item.getKey()));
    values.put("state", item.getState());
    Tags tags = item.getTags();
    values.put("tags", tagsToString(context, tags));
    NodeList childNodes = dataElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        String nodeName = node.getNodeName();
        String localName = node.getLocalName();
        String namespace = node.getNamespaceURI();
        if (namespace.equals("http://platformlayer.org/core/v1.0")) {
            if (localName.equals("tags")) {
                continue;
            }
            if (localName.equals("key")) {
                continue;
            }
            if (localName.equals("version")) {
                continue;
            }
            if (localName.equals("state")) {
                continue;
            }
        }
        String text = formatCell(node);
        text = Utils.reformatText(context, text);
        values.put(nodeName, text);
    }
    sink.outputRow(values);
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) PlatformLayerCliContext(org.platformlayer.client.cli.PlatformLayerCliContext) UntypedItemXml(org.platformlayer.UntypedItemXml) Tags(org.platformlayer.core.model.Tags)

Example 70 with NodeList

use of org.w3c.dom.NodeList in project camel by apache.

the class CamelNamespaceHandler method doBeforeParse.

/**
     * Prepares the nodes before parsing.
     */
public static void doBeforeParse(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        // ensure namespace with versions etc is renamed to be same namespace so we can parse using this handler
        Document doc = node.getOwnerDocument();
        if (node.getNamespaceURI().startsWith(SPRING_NS + "/v")) {
            doc.renameNode(node, SPRING_NS, node.getNodeName());
        }
        // remove whitespace noise from uri, xxxUri attributes, eg new lines, and tabs etc, which allows end users to format
        // their Camel routes in more human readable format, but at runtime those attributes must be trimmed
        // the parser removes most of the noise, but keeps double spaces in the attribute values
        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node att = map.item(i);
            if (att.getNodeName().equals("uri") || att.getNodeName().endsWith("Uri")) {
                final String value = att.getNodeValue();
                String before = ObjectHelper.before(value, "?");
                String after = ObjectHelper.after(value, "?");
                if (before != null && after != null) {
                    // remove all double spaces in the uri parameters
                    String changed = after.replaceAll("\\s{2,}", "");
                    if (!after.equals(changed)) {
                        String newAtr = before.trim() + "?" + changed.trim();
                        LOG.debug("Removed whitespace noise from attribute {} -> {}", value, newAtr);
                        att.setNodeValue(newAtr);
                    }
                }
            }
        }
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        doBeforeParse(list.item(i));
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Aggregations

NodeList (org.w3c.dom.NodeList)2090 Node (org.w3c.dom.Node)1217 Element (org.w3c.dom.Element)1061 Document (org.w3c.dom.Document)705 ArrayList (java.util.ArrayList)356 DocumentBuilder (javax.xml.parsers.DocumentBuilder)304 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)237 IOException (java.io.IOException)217 NamedNodeMap (org.w3c.dom.NamedNodeMap)171 InputSource (org.xml.sax.InputSource)145 SAXException (org.xml.sax.SAXException)145 Test (org.junit.Test)138 HashMap (java.util.HashMap)135 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)126 XPath (javax.xml.xpath.XPath)118 StringReader (java.io.StringReader)111 Attr (org.w3c.dom.Attr)88 XPathExpressionException (javax.xml.xpath.XPathExpressionException)85 File (java.io.File)76 InputStream (java.io.InputStream)70