/** * Returns the string value of the given XML node object, depending on its type. * * @param object * @return String */ public static String objectToString(Object object) { if (object instanceof Element) { return ((Element) object).getText(); } else if (object instanceof Attribute) { return ((Attribute) object).getValue(); } else if (object instanceof Text) { return ((Text) object).getText(); } else if (object instanceof CDATA) { return ((CDATA) object).getText(); } else if (object instanceof Comment) { return ((Comment) object).getText(); } else if (object instanceof Double) { return String.valueOf(object); } else if (object instanceof Boolean) { return String.valueOf(object); } else if (object instanceof String) { return (String) object; } else if (object != null) { logger.error("Unknown object type: {}", object.getClass().getName()); return null; } else { return null; } }
private DataDrivenTest(Element e) { super(e.getAttributeValue("desc")); Filter prologFilter = new AbstractFilter() { @Override public Object filter(Object o) { if (o instanceof Element || o instanceof Comment || o instanceof ProcessingInstruction) { return o; } return null; } }; target.addContent(XmlHelper.clone(e.getChild("target").getContent(prologFilter))); diff.setRootElement((Element) e.getChild("diff").clone()); expectedResult.addContent(XmlHelper.clone(e.getChild("result").getContent(prologFilter))); String errorName = e.getChild("result").getAttributeValue("error"); expectedError = errorName == null ? null : ErrorCondition.valueOf(errorName); }
@Override public Document process(final Map<String, Object> context, final Document document, final Slide slide, final Resources resources, final Configuration configuration) throws JODTemplateException { final IteratorIterable<Element> atElements = document.getDescendants(Filters.element(PPTXDocument.T_ELEMENT, getNamespace())); final List<Element> atElementsList = new ArrayList<>(); while (atElements.hasNext()) { atElementsList.add(atElements.next()); } for (Element at : atElementsList) { if (at.getContentSize() != 0) { final Content content = at.getContent(0); if (content instanceof Comment) { final Comment comment = (Comment) content; processComment(comment, at, slide, configuration); } } } return document; }
public static boolean equivalent(Comment c1, Comment c2) { String v1 = c1.getValue(); String v2 = c2.getValue(); boolean equals = v1.equals(v2); if (!equals && LOGGER.isDebugEnabled()) { LOGGER.debug("Comment differs \"{}\"!=\"{}\"", c1, c2); } return equals; }
public static boolean equivalentContent(List<Content> l1, List<Content> l2) { if (l1.size() != l2.size()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Number of content list elements differ {}!={}", l1.size(), l2.size()); } return false; } boolean result = true; Iterator<Content> i1 = l1.iterator(); Iterator<Content> i2 = l2.iterator(); while (result && i1.hasNext() && i2.hasNext()) { Object o1 = i1.next(); Object o2 = i2.next(); if (o1 instanceof Element && o2 instanceof Element) { result = equivalent((Element) o1, (Element) o2); } else if (o1 instanceof Text && o2 instanceof Text) { result = equivalent((Text) o1, (Text) o2); } else if (o1 instanceof Comment && o2 instanceof Comment) { result = equivalent((Comment) o1, (Comment) o2); } else if (o1 instanceof ProcessingInstruction && o2 instanceof ProcessingInstruction) { result = equivalent((ProcessingInstruction) o1, (ProcessingInstruction) o2); } else if (o1 instanceof DocType && o2 instanceof DocType) { result = equivalent((DocType) o1, (DocType) o2); } else { result = false; } } return result; }
@Override public Document process(final Map<String, Object> context, final Document document, final Slide slide, final Resources resources, final Configuration configuration) throws JODTemplateException { final IteratorIterable<Element> parentElements = document.getDescendants( Filters.element(parentElement, getNamespace())); final List<Element> parentElementsList = new ArrayList<>(); while (parentElements.hasNext()) { parentElementsList.add(parentElements.next()); } for (final Element parent : parentElementsList) { final IteratorIterable<Element> atElements = parent.getDescendants( Filters.element(PPTXDocument.T_ELEMENT, getNamespace())); final List<Element> atElementsList = new ArrayList<>(); while (atElements.hasNext()) { atElementsList.add(atElements.next()); } final ExpressionHandler expressionHandler = configuration.getExpressionHandler(); boolean isLoop = false; InlineListExpression expression = null; for (final Element at : atElementsList) { final String text = at.getText(); if (configuration.getExpressionHandler().isInlineList(text)) { expression = expressionHandler.createInlineListExpression(text); at.setText(expressionHandler.createVariable(expression.getVariable())); isLoop = true; } } if (isLoop) { int apIndex = parent.getParent().indexOf(parent); final String beginList = expressionHandler.createBeginList(expression.getWhat(), expression.getAs()); final String endList = expressionHandler.createEndList(); parent.getParent().addContent(apIndex, new Comment(beginList)); apIndex++; parent.getParent().addContent(apIndex + 1, new Comment(endList)); } } return document; }
private void processComment(final Comment comment, final Element at, final Slide slide, final Configuration configuration) throws JODTemplateException { String commentText = comment.getText(); if (commentText.startsWith(STYLIZED_KEYWORD)) { commentText = StringUtils.removeStart(commentText, STYLIZED_KEYWORD); final String className = StringUtils.substringBefore(commentText, ":"); commentText = StringUtils.removeStart(commentText, className + ": "); final Stylizer stylizer = configuration.getStylizer(className); if (stylizer == null) { throw new JODTemplateException("Unable to find stylizer"); } final String text = StringUtils.removeStart(commentText, " stylized: "); final Element ar = at.getParentElement(); final Element ap = ar.getParentElement(); final int arIndex = ap.indexOf(ar); final Element arPr = getArPrElement(ar); final Element apPr = getApPrElement(ap); final Element sourceApPr = ObjectUtils.clone(apPr); cleanApPrElement(apPr); final List<Element> stylizedElements = stylizer.stylize(text, arPr, apPr, slide); ap.removeContent(ar); final List<Element> remains = getRemainingElements(arIndex, ap); for (Element el : remains) { ap.removeContent(el); } final int currentApIndex = injectElementsInDocument(stylizedElements, ap, apPr, arIndex); injectRemainsInDocument(remains, ap, sourceApPr, currentApIndex); } }
private CommandSnippetComment getComment(Document document) { for (Object content : document.getContent()) { if (content instanceof Comment) { return new CommandSnippetTextComment(((Comment) content).getText()); } } return new EmptySnippetComment(); }
static void logMessage(String message, Element parent) { logMessage(message); parent.addContent(new Comment(message)); }
private static void doRemove(Element patch, Object node) throws JDOMException { if (node instanceof Element || node instanceof Comment || node instanceof ProcessingInstruction) { String ws = patch.getAttributeValue("ws"); boolean before = "both".equals(ws) || "before".equals(ws); boolean after = "both".equals(ws) || "after".equals(ws); Content c = (Content) node; Element e = c.getParentElement(); if (e == null) { throw new PatchException(ErrorCondition.INVALID_ROOT_ELEMENT_OPERATION, "can't remove root element"); } int index = e.indexOf(c); List<Content> nodesToDetach = new ArrayList<>(); nodesToDetach.add(c); if (before) { nodesToDetach.add(getWhitespace(e, index - 1)); } if (after) { nodesToDetach.add(getWhitespace(e, index + 1)); } for (Content detachMe : nodesToDetach) { detachMe.detach(); } return; } if (patch.getAttribute("ws") != null) { throw new PatchException(ErrorCondition.INVALID_PATCH_DIRECTIVE, "The 'ws' attribute is not allowed when removing " + "Attribute, Text or Namespace nodes."); } if (node instanceof Attribute) { Attribute a = (Attribute) node; a.getParent().removeAttribute(a); return; } if (node instanceof Text) { ((Content) node).detach(); return; } if (node instanceof Namespace) { throw new UnsupportedOperationException("removing namespace declarations is not yet implemented"); // return; } }
private void createSubnodes(Element element, List<? extends BaseToken> tagChildren) { if (tagChildren != null) { for (BaseToken item : tagChildren) { if (item instanceof CommentNode) { CommentNode commentNode = (CommentNode) item; Comment comment = factory.comment(commentNode.getContent()); element.addContent(comment); } else if (item instanceof ContentNode) { String nodeName = element.getName(); String content = item.toString(); boolean specialCase = props.isUseCdataForScriptAndStyle() && ("script".equalsIgnoreCase(nodeName) || "style".equalsIgnoreCase(nodeName)); if (escapeXml && !specialCase) { content = Utils.escapeXml(content, props, true); } // // For CDATA sections we don't want to return the start and // end tokens. See issue #106. // if (specialCase && item instanceof CData) { content = ((CData) item).getContentWithoutStartAndEndTokens(); } Text text = specialCase ? factory.cdata(content) : factory.text(content); element.addContent(text); } else if (item instanceof TagNode) { TagNode subTagNode = (TagNode) item; Element subelement = createElement(subTagNode); setAttributes(subTagNode, subelement); // recursively create subnodes createSubnodes(subelement, subTagNode.getAllChildren()); element.addContent(subelement); } else if (item instanceof List) { List sublist = (List) item; createSubnodes(element, sublist); } } } }
public static Element addComment(Element project, String comment) { return project.addContent(new Comment(comment)); }