List of usage examples for org.w3c.dom Element getParentNode
public Node getParentNode();
From source file:org.directwebremoting.drapgen.generate.gi.GiType.java
/** * Replace a stub with an implementation * @param newMethod The element to replace the declaration with * @param oldMethod A method name to replace in the input document *///from w ww . j a v a 2s . c om public void replaceImplementation(GiMethod newMethod, GiMethod oldMethod) { Element oldChild = oldMethod.getElement(); Element newChild = newMethod.getElement(); Node replacement = document.importNode(newChild, true); oldChild.getParentNode().replaceChild(replacement, oldChild); }
From source file:org.dita.dost.module.BranchFilterModule.java
private Set<URI> getBranchFilters(final Element e) { final Set<URI> res = new HashSet<>(); Element current = e; while (current != null) { final List<Element> ditavalref = getChildElements(current, DITAVAREF_D_DITAVALREF); if (!ditavalref.isEmpty()) { res.add(toURI(ditavalref.get(0).getAttribute(ATTRIBUTE_NAME_HREF))); }/* w w w . j a va 2 s . c o m*/ final Node parent = current.getParentNode(); if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { current = (Element) parent; } else { break; } } return res; }
From source file:org.dita.dost.module.BranchFilterModule.java
private void filterBranches(final Element elem, final List<FilterUtils> filters, final QName[][] props) { final List<FilterUtils> fs = combineFilterUtils(elem, filters); boolean exclude = false; for (final FilterUtils f : fs) { exclude = f.needExclude(elem, props); if (exclude) { break; }/* w w w . j ava 2 s. com*/ } if (exclude) { elem.getParentNode().removeChild(elem); } else { final List<Element> childElements = getChildElements(elem); final Set<Flag> flags = fs.stream().flatMap(f -> f.getFlags(elem, props).stream()) .map(f -> f.adjustPath(currentFile, job)).collect(Collectors.toSet()); for (Flag flag : flags) { final Element startElement = (Element) elem.getOwnerDocument().importNode(flag.getStartFlag(), true); final Node firstChild = elem.getFirstChild(); if (firstChild != null) { elem.insertBefore(startElement, firstChild); } else { elem.appendChild(startElement); } final Element endElement = (Element) elem.getOwnerDocument().importNode(flag.getEndFlag(), true); elem.appendChild(endElement); } for (final Element child : childElements) { filterBranches(child, fs, props); } } }
From source file:org.dita.dost.module.BranchFilterModule.java
/** * Duplicate branches so that each {@code ditavalref} will in a separate branch. *///from w w w. ja v a 2 s .c om void splitBranches(final Element elem, final Branch filter) { final List<Element> ditavalRefs = getChildElements(elem, DITAVAREF_D_DITAVALREF); if (ditavalRefs.size() > 0) { // remove ditavalrefs for (final Element branch : ditavalRefs) { elem.removeChild(branch); } // create additional branches after current element final List<Element> branches = new ArrayList<>(ditavalRefs.size()); branches.add(elem); final Node next = elem.getNextSibling(); for (int i = 1; i < ditavalRefs.size(); i++) { final Element clone = (Element) elem.cloneNode(true); if (next != null) { elem.getParentNode().insertBefore(clone, next); } else { elem.getParentNode().appendChild(clone); } branches.add(clone); } // insert ditavalrefs for (int i = 0; i < branches.size(); i++) { final Element branch = branches.get(i); final Element ditavalref = ditavalRefs.get(i); branch.appendChild(ditavalref); final Branch currentFilter = filter.merge(ditavalref); processAttributes(branch, currentFilter); final Branch childFilter = new Branch(currentFilter.resourcePrefix, currentFilter.resourceSuffix, Optional.empty(), Optional.empty()); // process children of all branches for (final Element child : getChildElements(branch, MAP_TOPICREF)) { if (DITAVAREF_D_DITAVALREF.matches(child)) { continue; } splitBranches(child, childFilter); } } } else { processAttributes(elem, filter); for (final Element child : getChildElements(elem, MAP_TOPICREF)) { splitBranches(child, filter); } } }
From source file:org.dita.dost.reader.ChunkMapReader.java
/** * Create new map and refer to it with navref. * @param topicref/*from w w w . j a v a2 s . c o m*/ */ private void processNavitation(final Element topicref) { // create new map's root element final Element root = (Element) topicref.getOwnerDocument().getDocumentElement().cloneNode(false); // create navref element final Element navref = topicref.getOwnerDocument().createElement(MAP_NAVREF.localName); final String newMapFile = chunkFilenameGenerator.generateFilename("MAPCHUNK", FILE_EXTENSION_DITAMAP); navref.setAttribute(ATTRIBUTE_NAME_MAPREF, newMapFile); navref.setAttribute(ATTRIBUTE_NAME_CLASS, MAP_NAVREF.toString()); // replace topicref with navref topicref.getParentNode().replaceChild(navref, topicref); root.appendChild(topicref); // generate new file final File navmap = resolve(fileDir, newMapFile); changeTable.put(navmap.getPath(), navmap.getPath()); outputMapFile(navmap, buildOutputDocument(root)); }
From source file:org.dita.dost.reader.ChunkMapReader.java
public static String getCascadeValue(final Element elem, final String attrName) { Element current = elem; while (current != null) { final Attr attr = current.getAttributeNode(attrName); if (attr != null) { return attr.getValue(); }//from w ww. j a v a 2 s .c o m final Node parent = current.getParentNode(); if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { current = (Element) parent; } else { break; } } return null; }
From source file:org.etudes.mneme.impl.ImportQti2ServiceImpl.java
/** * return answer text if answerText is true. Multiple choice needs id and fill blanks need text. * @param response/*from ww w.j a va 2 s. c om*/ * @param contentsDOM * @return * @throws Exception */ private String getCorrectResponse(Element value, Document contentsDOM, boolean answerTextFlag) throws Exception { if (value == null) return ""; Element response = (Element) value.getParentNode().getParentNode(); String baseType = response.getAttribute("baseType"); String responseIdentifier = response.getAttribute("identifier"); // if basetype is string then value is the answer. If identifier then fetch the element with this identifier for answer. String answerText = ""; if (("string").equalsIgnoreCase(baseType) && value != null) { answerText = value.getTextContent(); } else if ("identifier".equalsIgnoreCase(baseType)) { // /assessmentItem/responseDeclaration[1]/correctResponse[1]/value[1] has identifier answerText = value.getTextContent(); if (answerTextFlag) { // sometimes answertext is also identifier XPath answerPath = new DOMXPath(".//*[@identifier='" + answerText + "']"); String checkIdentifier = answerPath.stringValueOf(contentsDOM); if (checkIdentifier != null && checkIdentifier != "" && checkIdentifier.length() != 0) answerText = checkIdentifier; } } else answerText = value.getTextContent(); return answerText; }
From source file:org.etudes.mneme.impl.ImportQti2ServiceImpl.java
/** * /*ww w . jav a 2 s . co m*/ * @param contentsDOM * @param responseIdentifier * @param answerTextFlag * @return */ private List<String> getCorrectResponsefromResponseProcessing(Document contentsDOM, String responseIdentifier, boolean answerTextFlag) { ArrayList<String> correctResponses = new ArrayList<String>(); try { XPath answerPath = new DOMXPath( "/assessmentItem/responseProcessing/responseCondition//variable[@identifier='" + responseIdentifier + "']"); List<Element> answerIdElements = answerPath.selectNodes(contentsDOM); for (Element answerIdElement : answerIdElements) { // match element Element matchElement = (Element) answerIdElement.getParentNode(); answerPath = new DOMXPath(".//*[@baseType='identifier']"); answerIdElement = (Element) answerPath.selectSingleNode(matchElement); String answerIdentifier = answerIdElement.getTextContent(); // setoutcome element Element responseConditionElement = (Element) matchElement.getParentNode(); XPath outcomePath = new DOMXPath( ".//setOutcomeValue[contains(@identifier,'SCORE')] | .//setOutcomeValue[contains(@identifier,'score')] | .//setOutcomeValue[contains(@identifier,'Correct')]"); List<Element> scoreElements = outcomePath.selectNodes(responseConditionElement); String scoreValue = ""; for (Element score : scoreElements) { scoreValue = score.getTextContent(); if (scoreValue == null || scoreValue.equals("0") || scoreValue.equals("0.0") || scoreValue.startsWith("-")) continue; } if (scoreValue == null || scoreValue.equals("") || scoreValue.equals("0") || scoreValue.equals("0.0") || scoreValue.startsWith("-")) continue; if (!answerTextFlag) correctResponses.add(answerIdentifier); else { // get answer text answerPath = new DOMXPath(".//*[@identifier='" + answerIdentifier + "']"); correctResponses.add(answerPath.stringValueOf(contentsDOM)); } } } catch (Exception e) { // do nothing } return correctResponses; }
From source file:org.etudes.tool.melete.ViewSectionsPage.java
private Node getPreviousNode(Element secElement) { if (secElement.getPreviousSibling() != null) { if (secElement.getPreviousSibling().hasChildNodes() == false) { return secElement.getPreviousSibling(); } else {/* ww w .ja v a2 s .c o m*/ return getInnerLastChild((Element) secElement.getPreviousSibling()); } } else { if (secElement.getParentNode() != null) { if (secElement.getParentNode().getNodeName().equals("module")) { return null; } else { return secElement.getParentNode(); } } else { return null; } } }
From source file:org.etudes.tool.melete.ViewSectionsPage.java
private Node getNextNode(Element secElement) { if (secElement.hasChildNodes()) { return secElement.getFirstChild(); } else {/* w ww.ja v a2 s. c om*/ if (secElement.getNextSibling() != null) { return secElement.getNextSibling(); } else { if (secElement.getParentNode() != null) { if (secElement.getParentNode().getNodeName().equals("module")) { return null; } else { return getParentsNextSibling(secElement); } } else { return null; } } } }