/** * Returns the content of an element as string. The element itself * is ignored. * * @param e the element to get the content from * @return the content as string */ protected String getContent(Element e) throws IOException { XMLOutputter out = new XMLOutputter(); StringWriter writer = new StringWriter(); for (Content child : e.getContent()) { if (child instanceof Element) { out.output((Element) child, writer); } else if (child instanceof Text) { Text t = (Text) child; String trimmedText = t.getTextTrim(); if (!trimmedText.equals("")) { Text newText = new Text(trimmedText); out.output(newText, writer); } } } return writer.toString(); }
/** * 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; } }
/** * Parses a JDom2 Object into a string * @param e * Object (Element, Text, Document or Attribute) * @return String * @author sholzer (11.05.2015) */ public String parseString(Object e) { XMLOutputter element2String = new XMLOutputter(); if (e instanceof Element) { return element2String.outputString((Element) e); } if (e instanceof Text) { return element2String.outputString((Text) e); } if (e instanceof Document) { return element2String.outputString((Document) e); } if (e instanceof org.jdom2.Attribute) { Attribute a = (org.jdom2.Attribute) e; return a.getName() + "=\"" + a.getValue() + "\""; } return e.toString(); }
/** * Test method for {@link ElementComparatorImpl#compare(Element, Element)} * <p> * Compares two Elements through their text content. Expects mismatch * @throws Exception * when something somewhere goes wrong * @author sholzer (23.03.2015) */ @Test public void testCompareElementWithTextNodeMismatch() throws Exception { String rootName = "A"; String textValue1 = "0"; String textValue2 = "1"; String xpath = "./text()"; Element root1 = new Element(rootName); Text child1 = new Text(textValue1); root1.addContent(child1); Element root2 = new Element(rootName); Text child2 = new Text(textValue2); root2.addContent(child2); assertFalse("Expected false", getComparatorFromXpath(xpath).compare(root1, root2)); }
@Override public List<MCRIIIFMetadata> extractModsMetadata(Element xmlData) { Map<String, String> elementLabelMap = new HashMap<>(); elementLabelMap.put("title", "mods:mods/mods:titleInfo/mods:title/text()"); elementLabelMap.put("genre", "mods:mods/mods:genre/text()"); // TODO: add some more metadata return elementLabelMap.entrySet().stream().map(entry -> { XPathExpression<Text> pathExpression = XPathFactory.instance().compile(entry.getValue(), Filters.text(), null, MCRConstants.MODS_NAMESPACE); List<Text> texts = pathExpression.evaluate(xmlData); if (texts.size() == 0) { return null; } return new MCRIIIFMetadata(entry.getKey(), texts.stream().map(Text::getText).collect(Collectors.joining(", "))); }).filter(Objects::nonNull) .collect(Collectors.toList()); }
@Override public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException { String xpath = getProperties().get("Xpath"); Document xml = obj.createXML(); XPathFactory xpfac = XPathFactory.instance(); XPathExpression<Text> xp = xpfac.compile(xpath, Filters.text()); List<Text> evaluate = xp.evaluate(xml); if (evaluate.size() > 1) { throw new MCRPersistentIdentifierException( "Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + ""); } if (evaluate.size() == 0) { return Optional.empty(); } Text identifierText = evaluate.listIterator().next(); String identifierString = identifierText.getTextNormalize(); Optional<MCRDNBURN> parsedIdentifierOptional = PARSER.parse(identifierString); return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast); }
private void convertSuppressConsistency(Element repo) { Element type = repo.getChild("type", repo.getNamespace()); RepoType repoType = RepoType.fromType(type.getText()); if (repoType.isMavenGroup()) { Element suppress= repo.getChild("suppressPomConsistencyChecks", repo.getNamespace()); String pomConsistencyValue = repo.getChildText("suppressPomConsistencyChecks", repo.getNamespace()); if (suppress==null ){ suppress=new Element("suppressPomConsistencyChecks", repo.getNamespace()); int lastLocation = findSuppressLocation(repo); repo.addContent(lastLocation + 1, new Text("\n ")); repo.addContent(lastLocation + 2, suppress); } if(StringUtils.isBlank(suppress.getText())) { pomConsistencyValue = "false"; } suppress.setText(pomConsistencyValue); } }
private void replaceExcludedWithIncluded(Element rootElement, Namespace namespace, Element indexer, Element excludedRepositories) { List<String> excluded = excludedRepositories.getChildren() .stream() .map(Element::getText) .collect(Collectors.toList()); if (StringUtils.equals(indexer.getChildText("enabled", namespace), "true")) { Element includedRepositories = new Element("includedRepositories", namespace); collectRepositories(rootElement, namespace) .stream() .filter(repo -> !excluded.contains(repo)) .forEach(repo -> { Element repositoryRef = new Element("repositoryRef", namespace); repositoryRef.setText(repo); includedRepositories.addContent(repositoryRef); }); indexer.addContent(new Text("\n ")); indexer.addContent(includedRepositories); } indexer.removeContent(excludedRepositories); }
protected List<AnnotationFS> getAnnotations(JCas jCas) { int makeInstanceOffset = jCas.getDocumentText().length(); List<AnnotationFS> annotations = new ArrayList<AnnotationFS>(); FSIterator<Annotation> iterator = jCas.getAnnotationIndex().iterator(); while (iterator.isValid() && iterator.hasNext()) { Annotation annotation = iterator.next(); if (annotation instanceof DocumentAnnotation || annotation instanceof org.cleartk.timeml.type.Text || annotation instanceof Event || annotation instanceof Time || annotation instanceof TemporalLink) { annotations.add(annotation); if (annotation instanceof DocumentCreationTime) { annotations.add(new DCT((DocumentCreationTime) annotation)); } if (annotation instanceof Event) { annotations.add(new MakeInstance((Event) annotation, makeInstanceOffset)); } } } return annotations; }
private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException { LinkedList<String> result = new LinkedList<String>(); try { Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in)); XMLOutputter out = new XMLOutputter(); for (String xp : xpaths) { XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content()); for (Content node : xpath.evaluate(doc)) { if(node instanceof Element) { result.add(out.outputString((Element) node)); } else if(node instanceof Text) { result.add(out.outputString((Text) node)); } } } return result; } catch (JDOMException xpe) { throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe); } }
/** * @param element * {@link org.jdom2.Element} * @return {@link List}<{@link String}> * @author sholzer (05.05.2015) */ public List<Text> getTextNodes(Element element) { List<Text> result = new LinkedList<>(); for (Content content : element.getContent()) { if (content instanceof Text) { result.add((Text) content); } } return result; }
public MCRJDOMContent transform(MCRContent source) throws IOException { try { Element root = source.asXML().getRootElement().clone(); for (Text text : root.getDescendants(Filters.text())) { text.setText(MCRXMLFunctions.normalizeUnicode(text.getText())); } return new MCRJDOMContent(root); } catch (JDOMException | SAXException ex) { throw new IOException(ex); } }
public ProcessingInstruction getProcessingInstruction() { if (pi == null) { String data = RAW_OUTPUTTER.outputString(new Text(text)); this.pi = new ProcessingInstruction(type, data); } return pi; }
/** * Gets the list of mycore object identifiers from the given directory. * * @param targetDirectory directory where the *.tar was unpacked * @return list of object which lies within the directory */ public static List<String> getMCRObjects(Path targetDirectory) throws JDOMException, IOException { Path order = targetDirectory.resolve(MCRTransferPackage.IMPORT_CONFIG_FILENAME); Document xml = new SAXBuilder().build(order.toFile()); Element config = xml.getRootElement(); XPathExpression<Text> exp = MCRConstants.XPATH_FACTORY.compile("order/object/text()", Filters.text()); return exp.evaluate(config).stream().map(Text::getText).collect(Collectors.toList()); }
public static boolean equivalent(Text t1, Text t2) { String v1 = t1.getValue(); String v2 = t2.getValue(); boolean equals = v1.equals(v2); if (!equals && LOGGER.isDebugEnabled()) { LOGGER.debug("Text differs \"{}\"!=\"{}\"", t1, t2); } 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; }
/** * This method is capable of serializing Elements and Text nodes. * Return null otherwise. * * @param content the content to serialize * @return the serialized content, or null if the type is not supported */ public static JsonElement serialize(Content content) { if (content instanceof Element) { return serializeElement((Element) content); } if (content instanceof Text) { return serializeText((Text) content); } return null; }
@Test public void xmlRoundrip() throws IOException { // test 0.100 MCRMetaNumber meta_number = new MCRMetaNumber(); Element imported = new Element("number"); imported.setAttribute("inherited", "0"); imported.setAttribute("dimension", "width"); imported.setAttribute("measurement", "cm"); imported.addContent(new Text("0.100")); meta_number.setFromDOM(imported); Element exported = meta_number.createXML(); print_data(imported, exported); check_data(imported, exported); }
private static Element challengeToXml( final Challenge loopChallenge, final Answer answer, final String elementName ) throws ChaiOperationException { final Element responseElement = new Element( elementName ); responseElement.addContent( new Element( XML_NODE_CHALLENGE ).addContent( new Text( loopChallenge.getChallengeText() ) ) ); final Element answerElement = answer.toXml(); responseElement.addContent( answerElement ); responseElement.setAttribute( XML_ATTRIBUTE_ADMIN_DEFINED, String.valueOf( loopChallenge.isAdminDefined() ) ); responseElement.setAttribute( XML_ATTRIBUTE_REQUIRED, String.valueOf( loopChallenge.isRequired() ) ); responseElement.setAttribute( XNL_ATTRIBUTE_MIN_LENGTH, String.valueOf( loopChallenge.getMinLength() ) ); responseElement.setAttribute( XNL_ATTRIBUTE_MAX_LENGTH, String.valueOf( loopChallenge.getMaxLength() ) ); return responseElement; }
private static Text getWhitespace(Element parent, int i) { try { Content c = parent.getContent(i); if (isWhitespace(c)) { return (Text) c; } } catch (IndexOutOfBoundsException noSuchSibling) { // invalid whitepace directive } throw new PatchException(ErrorCondition.INVALID_WHITESPACE_DIRECTIVE, "sibling is not a whitespace node"); }
@Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { Element closedElement = getCurrentElement(); boolean twoPartTag = false; Locator loc = getDocumentLocator(); if (loc != null) { int endTagLine = loc.getLineNumber(); int endTagColumn = loc.getColumnNumber(); if (startTagLine != -1 && endTagLine != -1 && startTagLine != endTagLine) { twoPartTag = true; } else if (startTagColumn != -1 && endTagColumn != -1 && startTagColumn != endTagColumn) { twoPartTag = true; } } super.endElement(namespaceURI, localName, qName); if (emptyTag && twoPartTag && closedElement.getContent().isEmpty()) { // This is a separate closing tag after an empty value. // Add a blank text node. final Text text = getFactory().text(""); getFactory().addContent(closedElement, text); } }
@Override public Text filter(Object content) { if (content instanceof Text) { final Text txt = (Text) content; final String parentName = ((Element) txt.getParent()).getName(); if (elementsToFilter.contains(parentName)) { return null; } else { return txt; } } return null; }
@Override public void endElement( String namespaceURI, String localName, String qName ) throws SAXException { Element closedElement = getCurrentElement(); boolean twoPartTag = false; Locator loc = getDocumentLocator(); if ( loc != null ) { int endTagLine = loc.getLineNumber(); int endTagColumn = loc.getColumnNumber(); if ( startTagLine != -1 && endTagLine != -1 && startTagLine != endTagLine ) { twoPartTag = true; } else if ( startTagColumn != -1 && endTagColumn != -1 && startTagColumn != endTagColumn ) { twoPartTag = true; } } super.endElement( namespaceURI, localName, qName ); if ( emptyTag && twoPartTag && closedElement.getContent().isEmpty() ) { // This is a separate closing tag after an empty value. // Add a blank text node. final Text text = getFactory().text( "" ); getFactory().addContent( closedElement, text ); } }
static JsonPrimitive serializeText(Text text) { return new JsonPrimitive(text.getText()); }
public void testTrimContents() { Content whitespace = new Text(" \n\t\t"); Content cdata = new CDATA("Test"); List<Content> content; List newList; // Empty content list content = new ArrayList<>(); assertEquals(0, AtomParserUtils.trimContents(content).size()); // Whitespace content = new ArrayList<>(1); content.add(whitespace); assertEquals(0, AtomParserUtils.trimContents(content).size()); // Two whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(whitespace); assertEquals(0, AtomParserUtils.trimContents(content).size()); // CDATA alone content = new ArrayList<>(1); content.add(cdata); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // Whitespace + CDATA content = new ArrayList<>(2); content.add(whitespace); content.add(cdata); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // CDATA + whitespace content = new ArrayList<>(2); content.add(cdata); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // CDATA surrounded with whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(cdata); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // Two CDATA's surrounded with couples of whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(whitespace); content.add(cdata); content.add(cdata); content.add(whitespace); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(2, newList.size()); assertTrue(newList.get(0) == cdata); assertTrue(newList.get(1) == cdata); }
private static boolean isWhitespace(Content node) { // todo stricter interpretation of whitespace? return node instanceof Text && node.getValue().trim().length() == 0; }
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); } } } }
/** * Überprüft, ob die Anfrage erfolgreich war (StatusCode 200) und den * richtigen ContentType beinhaltet. Falls dies nicht der Fall ist, werden * entsprechende Exceptions geworfen. * * @param response * Antwort auf die HTTP-Anfrage * @param responseEntity * entsprechende Entity * @param expectedContentType * Content-Type, den die Antwort haben soll * @throws IOException * IOException * @throws NamiApiException * Fehler bei der Nami-Anfrage */ private void checkResponse(HttpResponse response, HttpEntity responseEntity, String expectedContentType) throws IOException, NamiApiException { // Teste, ob der Statuscode der Antwort 200 (OK) ist if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { // check if response is redirect to an error page Header locationHeader = response.getFirstHeader("Location"); if (locationHeader != null) { String redirectTarget = locationHeader.getValue(); // Extract error message from URL in location log.warning("Got redirect to: " + redirectTarget); // get query part (after '?') String redirectQuery = redirectTarget.substring(redirectTarget .indexOf('?') + 1); if (redirectTarget.contains("error.jsp")) { String message = URLDecoder.decode(redirectQuery, "UTF-8"); message = message.split("=", 2)[1]; throw new NamiApiException(message); } } else { // extract description from JBoss error page String error = ""; try { Document doc = getCleanedDom(response.getEntity() .getContent()); XPathFactory xpathFac = XPathFactory.instance(); // XPath describes content of description field String xpathStr = "/html/body/p[3]/u/text()"; XPathExpression<Text> xpath = xpathFac.compile(xpathStr, Filters.textOnly()); Text xpathResult = xpath.evaluateFirst(doc); if (xpathResult != null) { error = StringEscapeUtils.unescapeHtml4(xpathResult .getText()); } } catch (Exception e) { throw new NamiApiException(e); } throw new NamiApiException(error); } throw new NamiApiException("Statuscode of response is not 200 OK."); } // Teste, ob die Antwort den richtigen Content-Type liefert Header contentTypeHdr = responseEntity.getContentType(); if (contentTypeHdr == null) { throw new NamiApiException("Response has no Content-Type."); } else { String contentType = contentTypeHdr.getValue(); if (!contentType.equals(expectedContentType) && !contentType.contains(expectedContentType + ";")) { throw new NamiApiException("Content-Type of response is " + contentType + "; expected " + expectedContentType + "."); } } }