List of usage examples for org.jdom2 Element getContent
@Override
public List<Content> getContent()
Text
, Element
, Comment
, ProcessingInstruction
, CDATA
, and EntityRef
. From source file:org.esa.snap.productlibrary.rcp.toolviews.AOIMonitoring.model.AOI.java
License:Open Source License
private boolean load(final File file) { Document doc;/*from www . j av a 2 s . c o m*/ try { doc = XMLSupport.LoadXML(file.getAbsolutePath()); final Element root = doc.getRootElement(); final Attribute nameAttrib = root.getAttribute("name"); if (nameAttrib != null) this.name = nameAttrib.getValue(); final List<GeoPos> geoPosList = new ArrayList<GeoPos>(); final List<Content> children = root.getContent(); for (Object aChild : children) { if (aChild instanceof Element) { final Element child = (Element) aChild; if (child.getName().equals("param")) { inputFolder = XMLSupport.getAttrib(child, "inputFolder"); outputFolder = XMLSupport.getAttrib(child, "outputFolder"); processingGraph = XMLSupport.getAttrib(child, "graph"); lastProcessed = XMLSupport.getAttrib(child, "lastProcessed"); final Attribute findSlavesAttrib = child.getAttribute("findSlaves"); if (findSlavesAttrib != null) findSlaves = Boolean.parseBoolean(findSlavesAttrib.getValue()); final Attribute maxSlavesAttrib = child.getAttribute("maxSlaves"); if (maxSlavesAttrib != null) maxSlaves = Integer.parseInt(maxSlavesAttrib.getValue()); } else if (child.getName().equals("points")) { final List<Content> pntsList = child.getContent(); for (Object o : pntsList) { if (o instanceof Element) { final Element pntElem = (Element) o; final String latStr = XMLSupport.getAttrib(pntElem, "lat"); final String lonStr = XMLSupport.getAttrib(pntElem, "lon"); if (!latStr.isEmpty() && !lonStr.isEmpty()) { final float lat = Float.parseFloat(latStr); final float lon = Float.parseFloat(lonStr); geoPosList.add(new GeoPos(lat, lon)); } } } } else if (child.getName().equals(DBQuery.DB_QUERY)) { slaveDBQuery = new DBQuery(); //todo slaveDBQuery.fromXML(child); } } } aoiPoints = geoPosList.toArray(new GeoPos[geoPosList.size()]); } catch (IOException e) { SnapApp.getDefault().handleError("Unable to load AOI", e); return false; } return true; }
From source file:org.esa.snap.util.ftpUtils.java
License:Open Source License
public static Map<String, Long> readRemoteFileList(final ftpUtils ftp, final String server, final String remotePath) { boolean useCachedListing = true; final String tmpDirUrl = ResourceUtils.getApplicationUserTempDataDir().getAbsolutePath(); final File listingFile = new File(tmpDirUrl + "//" + server + ".listing.xml"); if (!listingFile.exists()) useCachedListing = false;/*from www. ja v a 2s .c o m*/ final Map<String, Long> fileSizeMap = new HashMap<>(900); if (useCachedListing) { Document doc = null; try { doc = XMLSupport.LoadXML(listingFile.getAbsolutePath()); } catch (IOException e) { useCachedListing = false; } if (useCachedListing) { final Element root = doc.getRootElement(); boolean listingFound = false; final List children1 = root.getContent(); for (Object c1 : children1) { if (!(c1 instanceof Element)) continue; final Element remotePathElem = (Element) c1; final Attribute pathAttrib = remotePathElem.getAttribute("path"); if (pathAttrib != null && pathAttrib.getValue().equalsIgnoreCase(remotePath)) { listingFound = true; final List children2 = remotePathElem.getContent(); for (Object c2 : children2) { if (!(c2 instanceof Element)) continue; final Element fileElem = (Element) c2; final Attribute attrib = fileElem.getAttribute("size"); if (attrib != null) { try { fileSizeMap.put(fileElem.getName(), attrib.getLongValue()); } catch (Exception e) { // } } } } } if (!listingFound) useCachedListing = false; } } if (!useCachedListing) { try { final FTPFile[] remoteFileList = ftp.getRemoteFileList(remotePath); writeRemoteFileList(remoteFileList, server, remotePath, listingFile); for (FTPFile ftpFile : remoteFileList) { fileSizeMap.put(ftpFile.getName(), ftpFile.getSize()); } } catch (Exception e) { System.out.println("Unable to get remote file list " + e.getMessage()); } } return fileSizeMap; }
From source file:org.graphwalker.io.GraphML.java
License:Open Source License
/** * Parses the graphml file, and returns the model as a edu.uci.ics.jung.graph.impl.Graph * /*from w ww . j a v a 2s. c o m*/ * @param fileName The graphml file * @return The graph */ private Graph parseFile(String fileName) { Graph graph = new Graph(); graph.setFileKey(fileName); SAXBuilder parser = new SAXBuilder(); try { logger.debug("Parsing file: " + fileName); Document doc = parser.build(Util.getFile(fileName)); // Parse all vertices (nodes) Iterator<Element> iter_node = doc.getDescendants(new ElementFilter("node")); while (iter_node.hasNext()) { Object o = iter_node.next(); if (o instanceof Element) { Element element = (Element) o; if (element.getAttributeValue("yfiles.foldertype") != null) { logger.debug(" Excluded node: " + element.getAttributeValue("yfiles.foldertype")); continue; } Iterator<Element> iterUMLNoteIter = element.getDescendants(new ElementFilter("UMLNoteNode")); if (iterUMLNoteIter.hasNext()) { Iterator<Element> iter_label = element.getDescendants(new ElementFilter("NodeLabel")); if (iter_label.hasNext()) { Object o3 = iter_label.next(); Element nodeLabel = (Element) o3; logger.debug(" Full name: '" + nodeLabel.getQualifiedName() + "'"); logger.debug(" Name: '" + nodeLabel.getTextTrim() + "'"); graph.setDescriptionKey(nodeLabel.getTextTrim()); } continue; } logger.debug(" id: " + element.getAttributeValue("id")); // Used to remember which vertex to store the image location. Vertex currentVertex = null; Iterator<Element> iterNodeLabel = element.getDescendants(new ElementFilter("NodeLabel")); while (iterNodeLabel.hasNext()) { Object o2 = iterNodeLabel.next(); if (o2 instanceof Element) { Element nodeLabel = (Element) o2; logger.debug(" Full name: '" + nodeLabel.getQualifiedName() + "'"); logger.debug(" Name: '" + nodeLabel.getTextTrim() + "'"); String str = nodeLabel.getTextTrim(); Vertex v = new Vertex(); graph.addVertex(v); currentVertex = v; // Parse description Iterator<Element> iter_data = element.getDescendants(new ElementFilter("data")); while (iter_data.hasNext()) { Object o3 = iter_data.next(); if (o instanceof Element) { Element data = (Element) o3; if (!data.getAttributeValue("key").equals("d5")) continue; v.setDesctiptionKey(data.getText()); break; } } v.setIdKey(element.getAttributeValue("id")); v.setVisitedKey(0); v.setFileKey(fileName); v.setFullLabelKey(str); v.setIndexKey(getNewVertexAndEdgeIndex()); v.setLabelKey(Vertex.getLabel(str)); v.setMergeKey(AbstractElement.isMerged(str)); v.setNoMergeKey(AbstractElement.isNoMerge(str)); v.setBlockedKey(AbstractElement.isBlocked(str)); v.setSwitchModelKey(Vertex.isSwitchModel(str)); Integer index = AbstractElement.getIndex(str); if (index != 0) { v.setIndexKey(index); } v.setReqTagKey(AbstractElement.getReqTags(str)); } } // Extract any manual test instructions Iterator<Element> iterData = element.getDescendants(new ElementFilter("data")); while (iterData.hasNext() && currentVertex != null) { Object o2 = iterData.next(); if (o2 instanceof Element) { Element data = (Element) o2; if (!data.getContent().isEmpty() && data.getContent(0) != null) { String text = data.getContent(0).getValue().trim(); if (!text.isEmpty()) { logger.debug(" Data: '" + text + "'"); currentVertex.setManualInstructions(text); } } } } // Using the yEd editor, an image can be used to depict the vertex. // When merging multiple // graphs into one, the code below, stores the image location, which // will be used when // writing that merged graphml file. Iterator<Element> iterImage = element.getDescendants(new ElementFilter("Image")); while (iterImage.hasNext() && currentVertex != null) { Object o2 = iterImage.next(); if (o2 instanceof Element) { Element image = (Element) o2; if (image.getAttributeValue("href") != null) { logger.debug(" Image: '" + image.getAttributeValue("href") + "'"); currentVertex.setImageKey(image.getAttributeValue("href")); } } } Iterator<Element> iterGeometry = element.getDescendants(new ElementFilter("Geometry")); while (iterGeometry.hasNext() && currentVertex != null) { Object o2 = iterGeometry.next(); if (o2 instanceof Element) { Element geometry = (Element) o2; logger.debug(" width: '" + geometry.getAttributeValue("width") + "'"); logger.debug(" height: '" + geometry.getAttributeValue("height") + "'"); logger.debug(" x position: '" + geometry.getAttributeValue("x") + "'"); logger.debug(" y position: '" + geometry.getAttributeValue("y") + "'"); currentVertex.setWidth(Float.parseFloat(geometry.getAttributeValue("width"))); currentVertex.setHeight(Float.parseFloat(geometry.getAttributeValue("height"))); currentVertex.setLocation( new Point2D.Float(Float.parseFloat(geometry.getAttributeValue("x")), Float.parseFloat(geometry.getAttributeValue("y")))); } } Iterator<Element> iterFill = element.getDescendants(new ElementFilter("Fill")); while (iterFill.hasNext() && currentVertex != null) { Object o2 = iterFill.next(); if (o2 instanceof Element) { Element fill = (Element) o2; logger.debug(" fill color: '" + fill.getAttributeValue("color") + "'"); currentVertex.setFillColor(new Color( Integer.parseInt(fill.getAttributeValue("color").replace("#", ""), 16))); } } } } Object[] vertices = graph.getVertices().toArray(); // Parse all edges (arrows or transitions) Iterator<Element> iter_edge = doc.getDescendants(new ElementFilter("edge")); while (iter_edge.hasNext()) { Object o = iter_edge.next(); if (o instanceof Element) { Element element = (Element) o; logger.debug(" id: " + element.getAttributeValue("id")); Edge e = new Edge(); Iterator<Element> iter2 = element.getDescendants(new ElementFilter("EdgeLabel")); Element edgeLabel = null; if (iter2.hasNext()) { Object o2 = iter2.next(); if (o2 instanceof Element) { edgeLabel = (Element) o2; logger.debug(" Full name: '" + edgeLabel.getQualifiedName() + "'"); logger.debug(" Name: '" + edgeLabel.getTextTrim() + "'"); logger.debug(" Edge label x: " + edgeLabel.getAttributeValue("x")); logger.debug(" Edge label y: " + edgeLabel.getAttributeValue("y")); logger.debug(" Edge label width: " + edgeLabel.getAttributeValue("width")); logger.debug(" Edge label height: " + edgeLabel.getAttributeValue("height")); e.setLabelHeight(Float.parseFloat(edgeLabel.getAttributeValue("height"))); e.setLabelWidth(Float.parseFloat(edgeLabel.getAttributeValue("width"))); e.setLabelLocation(new Point2D.Float(Float.parseFloat(edgeLabel.getAttributeValue("x")), Float.parseFloat(edgeLabel.getAttributeValue("y")))); } } Iterator<Element> iter3 = element.getDescendants(new ElementFilter("Path")); Element edgePath = null; if (iter3.hasNext()) { Object o3 = iter3.next(); if (o3 instanceof Element) { edgePath = (Element) o3; logger.debug(" Path sx: '" + edgePath.getAttributeValue("sx")); logger.debug(" Path sy: '" + edgePath.getAttributeValue("sy")); logger.debug(" Path tx: '" + edgePath.getAttributeValue("tx")); logger.debug(" Path ty: '" + edgePath.getAttributeValue("ty")); e.setPathSourceLocation( new Point2D.Float(Float.parseFloat(edgePath.getAttributeValue("sx")), Float.parseFloat(edgePath.getAttributeValue("sy")))); e.setPathTargetLocation( new Point2D.Float(Float.parseFloat(edgePath.getAttributeValue("tx")), Float.parseFloat(edgePath.getAttributeValue("ty")))); } } // Add edge path points if there is any. Iterator<Element> iter4 = element.getDescendants(new ElementFilter("Point")); Element edgePathPoint = null; while (iter4.hasNext()) { Object o4 = iter4.next(); if (o4 instanceof Element) { edgePathPoint = (Element) o4; logger.debug(" PathPoint x: '" + edgePathPoint.getAttributeValue("x")); logger.debug(" PathPoint y: '" + edgePathPoint.getAttributeValue("y")); e.setPathPoints( new Point2D.Float(Float.parseFloat(edgePathPoint.getAttributeValue("x")), Float.parseFloat(edgePathPoint.getAttributeValue("y")))); } } logger.debug(" source: " + element.getAttributeValue("source")); logger.debug(" target: " + element.getAttributeValue("target")); Vertex source = null; Vertex dest = null; for (Object vertice : vertices) { Vertex vertex = (Vertex) vertice; // Find source vertex if (vertex.getIdKey().equals(element.getAttributeValue("source")) && vertex.getFileKey().equals(fileName)) { source = vertex; } if (vertex.getIdKey().equals(element.getAttributeValue("target")) && vertex.getFileKey().equals(fileName)) { dest = vertex; } } if (source == null) { String msg = "Could not find starting node for edge. Name: '" + element.getAttributeValue("source") + "' In file '" + fileName + "'"; logger.error(msg); throw new RuntimeException(msg); } if (dest == null) { String msg = "Could not find end node for edge. Name: '" + element.getAttributeValue("target") + "' In file '" + fileName + "'"; logger.error(msg); throw new RuntimeException(msg); } e.setIdKey(element.getAttributeValue("id")); e.setFileKey(fileName); e.setIndexKey(getNewVertexAndEdgeIndex()); // Parse description Iterator<Element> iter_data = element.getDescendants(new ElementFilter("data")); while (iter_data.hasNext()) { Object o3 = iter_data.next(); if (o instanceof Element) { Element data = (Element) o3; if (!data.getAttributeValue("key").equals("d9")) continue; e.setDesctiptionKey(data.getText()); break; } } if (!graph.addEdge(e, source, dest)) { String msg = "Failed adding edge: " + e + ", to graph: " + graph; logger.error(msg); throw new RuntimeException(msg); } if (edgeLabel != null) { // The label of an edge has the following format: // Label Parameter [Guard] / Action1;Action2;ActionN; // Keyword // Where the Label, Parameter. Guard, Actions and Keyword are // optional. String str = edgeLabel.getText(); e.setFullLabelKey(str); String[] guardAndAction = Edge.getGuardAndActions(str); String[] labelAndParameter = Edge.getLabelAndParameter(str); e.setGuardKey(guardAndAction[0]); e.setActionsKey(guardAndAction[1]); e.setLabelKey(labelAndParameter[0]); e.setParameterKey(labelAndParameter[1]); e.setWeightKey(Edge.getWeight(str)); e.setBlockedKey(AbstractElement.isBlocked(str)); Integer index = AbstractElement.getIndex(str); if (index != 0) { e.setIndexKey(index); } e.setReqTagKey(AbstractElement.getReqTags(str)); } e.setVisitedKey(0); logger.debug(" Added edge: '" + e.getLabelKey() + "', with id: " + e.getIndexKey()); // Extract any manual test instructions Iterator<Element> iterData = element.getDescendants(new ElementFilter("data")); while (iterData.hasNext() && e != null) { Object o2 = iterData.next(); if (o2 instanceof Element) { Element data = (Element) o2; if (!data.getContent().isEmpty() && data.getContent(0) != null) { String text = data.getContent(0).getValue().trim(); if (!text.isEmpty()) { logger.debug(" Data: '" + text + "'"); e.setManualInstructions(text); } } } } } } } catch (RuntimeException e) { throw new RuntimeException("Could not parse file: '" + fileName + "'. " + e.getMessage()); } catch (IOException e) { throw new RuntimeException("Could not parse file: '" + fileName + "'. " + e.getMessage()); } catch (JDOMException e) { throw new RuntimeException("Could not parse file: '" + fileName + "'. " + e.getMessage()); } logger.debug("Finished parsing graph: " + graph); removeBlockedEntities(graph); logger.debug("Graph after removing BLOCKED entities: " + graph); return graph; }
From source file:org.isisaddons.module.docx.dom.DocxService.java
License:Apache License
private static void merge(final Element htmlBody, final Body docXBody, final MatchingPolicy matchingPolicy) throws MergeException { final List<String> matchedInputIds = Lists.newArrayList(); final List<String> unmatchedInputIds = Lists.newArrayList(); final List<Content> htmlBodyContents = htmlBody.getContent(); for (final Content input : htmlBodyContents) { if (!(input instanceof Element)) { continue; }/*from w w w .j a v a 2 s. c o m*/ mergeInto((Element) input, docXBody, matchedInputIds, unmatchedInputIds); } final List<String> unmatchedPlaceHolders = unmatchedPlaceholders(docXBody, matchedInputIds); matchingPolicy.unmatchedInputs(unmatchedInputIds); matchingPolicy.unmatchedPlaceholders(unmatchedPlaceHolders); }
From source file:org.isisaddons.module.docx.dom.util.Jdom2.java
License:Apache License
public static String textValueOf(Element htmlElement) { List<Content> htmlContent = htmlElement.getContent(); if (htmlContent.isEmpty()) { return null; }/*from w ww . j av a 2 s . c o m*/ Content content = htmlContent.get(0); if (!(content instanceof Text)) { return null; } Text htmlText = (Text) content; return normalized(htmlText.getValue()); }
From source file:org.kdp.word.transformer.ListParagraphTransformer.java
License:Apache License
private void processListItemBatch(Context context, Element parent, List<Element> listItems) { boolean ordered = false; for (Element el : listItems) { removeNestedSpanElements(el);//from w w w . jav a 2 s . com normalizeListItemText(el); ordered = processItemMarker(el); el.getAttributes().clear(); } Element firstItem = listItems.get(0); int index = parent.indexOf(firstItem); for (Element el : listItems) { parent.removeContent(el); } JDOMFactory factory = context.getJDOMFactory(); Element ul = factory.element(ordered ? "ol" : "ul"); for (Element el : listItems) { Element li = factory.element("li"); li.setAttribute("class", "MsoListParagraph"); for (Content co : el.getContent()) { li.addContent(co.clone()); } ul.addContent(li); } parent.addContent(index, ul); }
From source file:org.kdp.word.transformer.ListParagraphTransformer.java
License:Apache License
private void normalizeListItemText(Element el) { for (Content co : el.getContent()) { if (co.getCType() == CType.Text) { Text tco = (Text) co; String text = tco.getText().trim(); tco.setText(text + " "); }// w w w . j a v a 2 s .c o m } }
From source file:org.mycore.frontend.editor.MCREditorDefReader.java
License:Open Source License
/** * Recursively resolves references by the @ref attribute and * replaces them with the referenced component. */// w ww. j a v a2 s . c o m private void resolveReferences() { for (Iterator<Element> it = referencing2ref.keySet().iterator(); it.hasNext();) { Element referencing = it.next(); String id = referencing2ref.get(referencing); LOGGER.debug("Resolving reference to " + id); Element found = id2component.get(id); if (found == null) { String msg = "Reference to component " + id + " could not be resolved"; throw new MCRConfigurationException(msg); } String name = referencing.getName(); referencing.removeAttribute("ref"); it.remove(); if (name.equals("cell") || name.equals("repeater")) { if (found.getParentElement().getName().equals("components")) { referencing.addContent(0, found.detach()); } else { referencing.addContent(0, (Element) found.clone()); } } else if (name.equals("panel")) { if (referencing2ref.containsValue(id)) { referencing.addContent(0, found.cloneContent()); } else { found.detach(); List<Content> content = found.getContent(); for (int i = 0; !content.isEmpty(); i++) { Content child = content.remove(0); referencing.addContent(i, child); } } } else if (name.equals("include")) { Element parent = referencing.getParentElement(); int pos = parent.indexOf(referencing); referencing.detach(); if (referencing2ref.containsValue(id)) { parent.addContent(pos, found.cloneContent()); } else { found.detach(); List<Content> content = found.getContent(); for (int i = pos; !content.isEmpty(); i++) { Content child = content.remove(0); parent.addContent(i, child); } } } } Element components = editor.getChild("components"); String root = components.getAttributeValue("root"); for (int i = 0; i < components.getContentSize(); i++) { Content child = components.getContent(i); if (!(child instanceof Element)) { continue; } if (((Element) child).getName().equals("headline")) { continue; } if (!root.equals(((Element) child).getAttributeValue("id"))) { components.removeContent(i--); } } }
From source file:org.rometools.feed.module.content.io.ContentModuleParser.java
License:Open Source License
protected String getXmlInnerText(Element e) { StringBuffer sb = new StringBuffer(); XMLOutputter xo = new XMLOutputter(); List children = e.getContent(); sb.append(xo.outputString(children)); return sb.toString(); }
From source file:org.xcri.types.DescriptiveTextType.java
License:Open Source License
@Override public void fromXml(Element element) throws InvalidElementException { super.fromXml(element); ///* ww w . j av a 2s . c o m*/ // Add XHTML content if present // Element div = Lax.getChildQuietly(element, "div", Namespaces.XHTML_NAMESPACE_NS, log); if (div != null) { div = (Element) div.detach(); processXhtml(div); } // // How about some CDATA with HTML in it? // for (Object child : element.getContent()) { if (child instanceof CDATA) { if (ParserConfiguration.getInstance().fixCDATA()) { log.warn( "description: uses CDATA instead of XHTML. Attempting to convert inline HTML into XHTML."); processCDATA((CDATA) child); } else { throw new InvalidElementException( "description: contains CDATA. To allow CDATA to be converted to XHTML, run with fixCDATA=true"); } } } // // Add HREF // this.setHref(element.getAttributeValue("href")); // // Cannot have both linked and inline content // if ((this.getValue() != null && this.getValue().length() > 0) || this.xhtml != null) { if (this.getHref() != null && this.getHref().length() > 0) { throw new InvalidElementException( "Description contains both text content and href attribute; only one or the other may be used"); } } }