List of usage examples for org.dom4j Element node
Node node(int index) throws IndexOutOfBoundsException;
Node
at the specified index position. From source file:org.openadaptor.auxil.connector.jdbc.writer.xml.AbstractXMLWriter.java
License:Open Source License
private void populateStatementFromElements(Element root) throws SQLException { for (int i = 0; i < root.nodeCount(); i++) { Node node = root.node(i); populateParameter(i + 1, node.getStringValue()); }// w w w .ja v a 2 s .c o m }
From source file:org.openadaptor.auxil.convertor.xml.XmlToOrderedMapConvertor.java
License:Open Source License
/** * Convert an Element into an orderedMap. essentially loops through each child * node of the supplied element and add a corresponding attribute to the ordered * map.//from w w w. j a v a 2 s .c o m * * @param current the element to convert * * @return the equivalent IOrderedMap * * @throws RecordException if there was a problem decoding the value */ private IOrderedMap generateMapFromXml(Element current) throws RecordException { IOrderedMap map = new OrderedHashMap(); //ToDo: Decouple OHM Dependency - i.e. make this factory generated. //This is faster than iterator for (int i = 0, size = current.nodeCount(); i < size; i++) { Node node = current.node(i); //It will need to be added if (node instanceof Element) { Element element = (Element) node; String name = element.getName(); log.debug("Mapping Element:" + name); insert(map, name, generateValue(element)); } } return map; }
From source file:org.orbeon.oxf.processor.sql.SQLProcessor.java
License:Open Source License
protected void execute(final PipelineContext context, XMLReceiver xmlReceiver) { try {// ww w. j ava2s . c om // Cache, read and interpret the config input Config config = readCacheInputAsObject(context, getInputByName(INPUT_CONFIG), new CacheableInputReader<Config>() { public Config read(PipelineContext context, ProcessorInput input) { // Read the config input document Node config = readInputAsDOM4J(context, input); Document configDocument = config.getDocument(); // Extract XPath expressions and also check whether any XPath expression is used at all // NOTE: This could be done through streaming below as well // NOTE: For now, just match <sql:param select="/*" type="xs:base64Binary"/> List xpathExpressions = new ArrayList(); boolean useXPathExpressions = false; for (Iterator i = XPathUtils.selectIterator(configDocument, "//*[namespace-uri() = '" + SQL_NAMESPACE_URI + "' and @select]"); i .hasNext();) { Element element = (Element) i.next(); useXPathExpressions = true; String typeAttribute = element.attributeValue("type"); if ("xs:base64Binary".equals(typeAttribute)) { String selectAttribute = element.attributeValue("select"); xpathExpressions.add(selectAttribute); } } // Normalize spaces. What this does is to coalesce adjacent text nodes, and to remove // resulting empty text, unless the text is contained within a sql:text element. configDocument.accept(new VisitorSupport() { private boolean endTextSequence(Element element, Text previousText) { if (previousText != null) { String value = previousText.getText(); if (value == null || value.trim().equals("")) { element.remove(previousText); return true; } } return false; } @Override public void visit(Element element) { // Don't touch text within sql:text elements if (!SQL_NAMESPACE_URI.equals(element.getNamespaceURI()) || !"text".equals(element.getName())) { Text previousText = null; for (int i = 0, size = element.nodeCount(); i < size;) { Node node = element.node(i); if (node instanceof Text) { Text text = (Text) node; if (previousText != null) { previousText.appendText(text.getText()); element.remove(text); } else { String value = text.getText(); // Remove empty text nodes if (value == null || value.length() < 1) { element.remove(text); } else { previousText = text; i++; } } } else { if (!endTextSequence(element, previousText)) i++; previousText = null; } } endTextSequence(element, previousText); } } }); // Create SAXStore try { final SAXStore store = new SAXStore(); final LocationSAXWriter locationSAXWriter = new LocationSAXWriter(); locationSAXWriter.setContentHandler(store); locationSAXWriter.write(configDocument); // Return the normalized document return new Config(store, useXPathExpressions, xpathExpressions); } catch (SAXException e) { throw new OXFException(e); } } }); // Either read the whole input as a DOM, or try to serialize Node data = null; XPathXMLReceiver xpathReceiver = null; // Check if the data input is connected boolean hasDataInput = getConnectedInputs().get(INPUT_DATA) != null; if (!hasDataInput && config.useXPathExpressions) throw new OXFException( "The data input must be connected when the configuration uses XPath expressions."); if (!hasDataInput || !config.useXPathExpressions) { // Just use an empty document data = Dom4jUtils.NULL_DOCUMENT; } else { // There is a data input connected and there are some XPath expressions operating on it boolean useXPathContentHandler = false; if (config.xpathExpressions.size() > 0) { // Create XPath content handler final XPathXMLReceiver _xpathReceiver = new XPathXMLReceiver(); // Add expressions and check whether we can try to stream useXPathContentHandler = true; for (Iterator i = config.xpathExpressions.iterator(); i.hasNext();) { String expression = (String) i.next(); boolean canStream = _xpathReceiver.addExpresssion(expression, false);// FIXME: boolean nodeSet if (!canStream) { useXPathContentHandler = false; break; } } // Finish setting up the XPathContentHandler if (useXPathContentHandler) { _xpathReceiver.setReadInputCallback(new Runnable() { public void run() { readInputAsSAX(context, INPUT_DATA, _xpathReceiver); } }); xpathReceiver = _xpathReceiver; } } // If we can't stream, read everything in if (!useXPathContentHandler) data = readInputAsDOM4J(context, INPUT_DATA); } // Try to read datasource input if any Datasource datasource = null; { List datasourceInputs = (List) getConnectedInputs().get(INPUT_DATASOURCE); if (datasourceInputs != null) { if (datasourceInputs.size() > 1) throw new OXFException("At most one one datasource input can be connected."); ProcessorInput datasourceInput = (ProcessorInput) datasourceInputs.get(0); datasource = Datasource.getDatasource(context, this, datasourceInput); } } // Replay the config SAX store through the interpreter config.configInput.replay( new RootInterpreter(context, getPropertySet(), data, datasource, xpathReceiver, xmlReceiver)); } catch (OXFException e) { throw e; } catch (Exception e) { throw new OXFException(e); } }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.GetXMLDataDialog.java
License:Open Source License
private void ChildNode(Node node, String realXPath, String realXPathCleaned) { Element ce = (Element) node; // List child for (int j = 0; j < ce.nodeCount(); j++) { Node cnode = ce.node(j); if (!Const.isEmpty(cnode.getName())) { Element cce = (Element) cnode; if (cce.nodeCount() > 1) { if (Const.getOccurenceString(cnode.asXML(), "/>") <= 1) { // We do not have child nodes ... setNodeField(cnode, realXPathCleaned); } else { // let's get child nodes ChildNode(cnode, realXPath, realXPathCleaned); }//from w w w .j a v a 2 s . c om } else { setNodeField(cnode, realXPathCleaned); } } } }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.GetXMLDataDialog.java
License:Open Source License
private void addLoopXPath(Node node) { Element ce = (Element) node; // List child for (int j = 0; j < ce.nodeCount(); j++) { Node cnode = ce.node(j); if (!Const.isEmpty(cnode.getName())) { Element cce = (Element) cnode; if (!listpath.contains(cnode.getPath())) listpath.add(cnode.getPath()); // let's get child nodes if (cce.nodeCount() > 1) addLoopXPath(cnode);/*from w ww . ja v a 2 s . c o m*/ } } }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.LoopNodesImportProgressDialog.java
License:Apache License
private void addLoopXPath(Node node, IProgressMonitor monitor) { Element ce = (Element) node; monitor.worked(1);// w w w .j a v a2 s. c om // List child for (int j = 0; j < ce.nodeCount(); j++) { if (monitor.isCanceled()) { return; } Node cnode = ce.node(j); if (!Utils.isEmpty(cnode.getName())) { Element cce = (Element) cnode; if (!listpath.contains(cnode.getPath())) { nr++; monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr))); monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode", cnode.getPath())); listpath.add(cnode.getPath()); } // let's get child nodes if (cce.nodeCount() > 1) { addLoopXPath(cnode, monitor); } } } }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.XMLInputFieldsImportProgressDialog.java
License:Apache License
private boolean childNode(Node node, IProgressMonitor monitor) { boolean rc = false; // true: we found child nodes Element ce = (Element) node; // List child for (int j = 0; j < ce.nodeCount(); j++) { Node cnode = ce.node(j); if (!Utils.isEmpty(cnode.getName())) { Element cce = (Element) cnode; if (cce.nodeCount() > 1) { if (childNode(cnode, monitor) == false) { // We do not have child nodes ... setNodeField(cnode, monitor); rc = true;// w w w .jav a 2 s. c o m } } else { setNodeField(cnode, monitor); rc = true; } } } return rc; }
From source file:org.pentaho.supportutility.config.retriever.JackRabbitConfigRetriever.java
License:Open Source License
/** * removes password node/*from ww w. ja v a2s. c o m*/ * * @param element * @return boolean */ public static boolean removeNode(Element element) { for (int j = 0; j < element.nodeCount(); j++) { Node param = element.node(j); Element par = (Element) param; for (Iterator<?> n = par.attributeIterator(); n.hasNext();) { Attribute attribute = (Attribute) n.next(); if (attribute.getName().equalsIgnoreCase("name")) { if (attribute.getStringValue().equals("password")) { element.remove(param); } } } } return true; }
From source file:org.talend.mdm.webapp.base.server.util.XmlUtil.java
License:Open Source License
public static void treeWalk(Element element, NodeProcess nodeProcess) { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Element) { treeWalk((Element) node, nodeProcess); } else {//from ww w . j ava 2 s . com nodeProcess.process(element); } } }
From source file:org.talend.mdm.webapp.base.server.util.XmlUtil.java
License:Open Source License
public static void recursionNode(Node node, StringBuffer value) { Element element = (Element) node; int size = element.nodeCount(); for (int i = 0; i < size; i++) { Node chilidNode = element.node(i); if (chilidNode instanceof Element) { if (!"".equals(value.toString())) { //$NON-NLS-1$ value.append(" "); //$NON-NLS-1$ }/*from w w w .j a v a 2 s .c o m*/ value.append(chilidNode.getText()); recursionNode(chilidNode, value); } } }