/** * Merges a given XML file with a patch. This API is designed for CobiGen * @see com.github.maybeec.lexeme.LeXeMerger#merge(Element, Element, ConflictHandlingType) * @param base * File * @param patch * String * @param charSet * target charset of the file to be read and write * @param conflictHandling * {@link ConflictHandlingType} specifying how conflicts will be handled during the merge * process. If null the default for this LeXeMerger will be used * @return Document the merged result of base and patch * @throws XMLMergeException * when the Documents can't be properly merged * @author sholzer (23.04.2015) */ public Document merge(File base, String patch, String charSet, ConflictHandlingType conflictHandling) throws XMLMergeException { if (conflictHandling == null) { conflictHandling = conflictHandlingType; } try { SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING); builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); String baseString = JDom2Util.getInstance().readFile(base.getPath(), charSet); Document baseDoc = builder.build(new InputSource(new BufferedReader(new StringReader(baseString)))); Document patchDoc = builder.build(new InputSource(new BufferedReader(new StringReader(patch)))); return merge(baseDoc, patchDoc, conflictHandling); } catch (IOException | JDOMException e) { logger.error("Caught unexcpected {}:{}", e.getClass().getName(), e.getMessage()); throw new XMLMergeException(e.getMessage()); } }
public static Document parseXHTMLDocument(String xhtml, JDOMFactory factory) throws IOException, JDOMException { //DTD ersetzen, da die originale nicht erreichbar bzw. nur sehr langsam ist, xhtml = xhtml.replace("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd", "http://localhost:8777/dtd/www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"); ByteArrayInputStream bais = new ByteArrayInputStream(xhtml.getBytes("UTF-8")); SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/external-general-entities", false); builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false); builder.setFeature("http://xml.org/sax/features/resolve-dtd-uris", false); builder.setFeature("http://xml.org/sax/features/validation", false); builder.setExpandEntities(false); if (factory != null) { builder.setJDOMFactory(factory); } Document document = builder.build(bais); return document; }
protected void importXml(String text) { if (isNotBlank(text)) { SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); try { Document document = builder.build(new StringReader(text)); String rootName = document.getRootElement().getName(); if (rootName.equals("definitions")) { importFromWsdl(text); } else if (rootName.equals("schema")) { importFromXsd(text); } else { Notification note = new Notification("Unrecognized Content", "The XML file has a root element of " + rootName + ", but expected \"definitions\" for WSDL or \"schema\" for XSD."); note.show(Page.getCurrent()); } } catch (Exception e) { throw new RuntimeException(e); } } }
protected void buildXpathChoices() { SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); Setting setting = component.findSetting(XmlFormatter.XML_FORMATTER_TEMPLATE); xpathChoices = new TreeSet<String>(); if (StringUtils.isNotBlank(setting.getValue())) { try { Document document = builder.build(new StringReader(setting.getValue())); buildXpathChoicesFromElement("/" + document.getRootElement().getName(), document.getRootElement()); } catch (Exception e) { throw new RuntimeException(e); } } ComboBox combo = (ComboBox) grid.getColumn("xpath").getEditorField(); combo.removeAllItems(); combo.addItems(xpathChoices); }
private VisualTreeNode parseVisualTree(ComponentContainer parent, Reader inputStream, Object eventHandlerTarget) throws ParserException { SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING); try { Document doc = builder.build(inputStream); //FIXME: I think this is not needed with XSD schema. Please use schema validation instead! if (!doc.hasRootElement() && (doc.getRootElement().getName().equalsIgnoreCase(Constants.XAADIN_ROOT_ELEMENT_NAME))) { throw new ParserException("could not find root rootElement with name xaadin"); } Element rootElement = doc.getRootElement(); xaadinNamespace = getDefaultXaadinNamespace(rootElement); List<Element> childrenList = rootElement.getChildren(); if (childrenList.isEmpty()) { throw new ParserException("xaadin root element is empty"); } return parseVisualTreeInt(childrenList.get(0), null, eventHandlerTarget, parent); } catch (Exception e) { throw new ParserException(e); } }
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); } }
protected void parseLauncherTemplate(JnlpTemplate launcher) throws ServletErrorException, IOException { // Parse JNLP launcher as JDOM Element rootElt = null; BufferedReader reader = null; try { // Assume the template has UTF-8 encoding reader = new BufferedReader( new InputStreamReader(launcher.realPathURL.toURL().openConnection().getInputStream(), "UTF-8")); rootElt = new SAXBuilder(XMLReaders.NONVALIDATING, null, null).build(reader).getRootElement(); } catch (JDOMException e) { throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Can't parse launcher template", e); } finally { FileUtil.safeClose(reader); } if (!rootElt.getName().equals(JNLP_TAG_ELT_ROOT)) { throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Invalid JNLP launcher template"); } launcher.rootElt = rootElt; }
@MCRCommand(syntax = "load mods document from file {0} for project {1}", help = "Load MODS document {0} as MyCoRe Object for project {1}", order = 20) public static void loadFromFile(String modsFileName, String projectID) throws JDOMException, IOException, MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException { File modsFile = new File(modsFileName); if (!modsFile.isFile()) { throw new MCRException(MessageFormat.format("File {0} is not a file.", modsFile.getAbsolutePath())); } SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null); Document modsDoc = s.build(modsFile); //force validation against MODS XSD MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI); Element modsRoot = modsDoc.getRootElement(); if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) { throw new MCRException( MessageFormat.format("File {0} is not a MODS document.", modsFile.getAbsolutePath())); } if (modsRoot.getName().equals("modsCollection")) { List<Element> modsElements = modsRoot.getChildren("mods", MCRConstants.MODS_NAMESPACE); for (Element mods : modsElements) { saveAsMyCoReObject(projectID, mods); } } else { saveAsMyCoReObject(projectID, modsRoot); } }
@MCRCommand(syntax = "load mods document from file {0} with files from directory {1} for project {2}", help = "Load MODS document {0} as MyCoRe Object with files from direcory {1} for project {2}", order = 10) public static void loadFromFileWithFiles(String modsFileName, String fileDirName, String projectID) throws JDOMException, IOException, MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException { File modsFile = new File(modsFileName); if (!modsFile.isFile()) { throw new MCRException(MessageFormat.format("File {0} is not a file.", modsFile.getAbsolutePath())); } File fileDir = new File(fileDirName); if (!fileDir.isDirectory()) { throw new MCRException( MessageFormat.format("Directory {0} is not a directory.", fileDir.getAbsolutePath())); } SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null); Document modsDoc = s.build(modsFile); //force validation against MODS XSD MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI); Element modsRoot = modsDoc.getRootElement(); if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) { throw new MCRException( MessageFormat.format("File {0} is not a MODS document.", modsFile.getAbsolutePath())); } if (modsRoot.getName().equals("modsCollection")) { throw new MCRException( MessageFormat.format("File {0} contains a mods collection witch not supported by this command.", modsFile.getAbsolutePath())); } else { createDerivate(saveAsMyCoReObject(projectID, modsRoot), fileDir); } }
public static SAXBuilder createSaxBuilder() { SAXBuilder sb = new SAXBuilder(); // don't validate and don't load dtd sb.setXMLReaderFactory(XMLReaders.NONVALIDATING); sb.setFeature("http://xml.org/sax/features/validation", false); sb.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); sb.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); // JNLP needs DOCTYPE //sb.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); sb.setFeature("http://xml.org/sax/features/external-general-entities", false); sb.setFeature("http://xml.org/sax/features/external-parameter-entities", false); return sb; }
public static String getTransformedXml(String inputXml, String stylesheetXml, String xmlFormat, boolean omitXmlDeclaration) { StringWriter writer = new StringWriter(); SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); try { Document inputDoc = builder.build(new StringReader(inputXml)); StringReader reader = new StringReader(stylesheetXml); XSLTransformer transformer = new XSLTransformer(reader); Document outputDoc = transformer.transform(inputDoc); XMLOutputter xmlOutput = new XMLOutputter(); Format format = null; if (xmlFormat.equals(COMPACT_FORMAT)) { format = Format.getCompactFormat(); } else if (xmlFormat.equals(RAW_FORMAT)) { format = Format.getRawFormat(); } else { format = Format.getPrettyFormat(); } format.setOmitDeclaration(omitXmlDeclaration); xmlOutput.setFormat(format); xmlOutput.output(outputDoc, writer); writer.close(); } catch (Exception e) { throw new RuntimeException(e); } return writer.toString(); }
private void parseDocument() throws JDOMException, IOException { lastModified = getLastModified(); LOGGER.info("Parsing: {}", docURL); parsedDocument = new SAXBuilder(XMLReaders.NONVALIDATING).build(docURL); }
@SuppressWarnings("unchecked") protected void handleUsingXPath(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) { ArrayList<String> inputRows = ((TextMessage) inputMessage).getPayload(); ArrayList<EntityData> payload = new ArrayList<EntityData>(); if (inputRows != null) { for (String xml : inputRows) { SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); try { Document document = builder.build(new StringReader(xml)); removeNamespaces(document); for (XmlFormatterEntitySetting entitySetting : entitySettings) { List<XmlFormatterAttributeSetting> attributeSettings = entitySetting.getAttributeSettings(); List<Element> entityMatches = (List<Element>) entitySetting.getExpression().evaluate(document.getRootElement()); for (Element element : entityMatches) { String text = toXML(element); Document childDocument = builder.build(new ByteArrayInputStream(text.getBytes(Charset.forName("utf-8")))); getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber); EntityData data = new EntityData(); for (XmlFormatterAttributeSetting attributeSetting : attributeSettings) { boolean resultsFound = false; Element targetElement = element; Document targetDocument = childDocument; do { List<Object> attributeMatches = (List<Object>) attributeSetting.getExpression().evaluate(targetDocument); for (Object object : attributeMatches) { resultsFound = true; if (object instanceof Attribute) { data.put(attributeSetting.getSetting().getAttributeId(), ((Attribute) object).getValue()); } else if (object instanceof Content) { data.put(attributeSetting.getSetting().getAttributeId(), ((Content) object).getValue()); } else if (object instanceof Element) { data.put(attributeSetting.getSetting().getAttributeId(), ((Element) object).getTextTrim()); } else { data.put(attributeSetting.getSetting().getAttributeId(), object); } } if (!resultsFound && !attributeSetting.getExpression().getExpression().startsWith("/" + element.getName()) && targetElement.getParentElement() != null) { targetElement = targetElement.getParentElement(); targetDocument = builder .build(new ByteArrayInputStream(toXML(targetElement).getBytes(Charset.forName("utf-8")))); } else if (!resultsFound) { info("Did not find a match for: %s\n in:\n %s", attributeSetting.getExpression().getExpression(), text); targetDocument = null; targetElement = null; } } while (!resultsFound && targetElement != null); } if (data.size() > 0) { payload.add(data); } else { log(LogLevel.WARN, "Found entity element: <%s/> with no matching attributes. Please make sure your xpath expressions match", element.getName()); } } } if (payload.size() > rowsPerMessage) { callback.sendEntityDataMessage(null, payload); payload = new ArrayList<>(); } } catch (Exception e) { throw new RuntimeException(e); } } } if (payload.size() > 0) { callback.sendEntityDataMessage(null, payload); } }
@SuppressWarnings("unchecked") protected void handleUsingXPath(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) { ArrayList<String> inputRows = ((TextMessage) inputMessage).getPayload(); ArrayList<EntityData> payload = new ArrayList<EntityData>(); if (inputRows != null) { for (String xml : inputRows) { SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); try { Document document = builder.build(new StringReader(xml)); removeNamespaces(document); for (XmlFormatterEntitySetting entitySetting : entitySettings) { List<XmlFormatterAttributeSetting> attributeSettings = entitySetting.getAttributeSettings(); List<Element> entityMatches = (List<Element>) entitySetting.getExpression().evaluate(document.getRootElement()); for (Element element : entityMatches) { getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber); EntityData data = new EntityData(); for (XmlFormatterAttributeSetting attributeSetting : attributeSettings) { Element targetElement = element; List<Object> attributeMatches = (List<Object>) attributeSetting.getExpression().evaluate(targetElement); for (Object object : attributeMatches) { if (object instanceof Attribute) { data.put(attributeSetting.getSetting().getAttributeId(), ((Attribute) object).getValue()); } else if (object instanceof Content) { data.put(attributeSetting.getSetting().getAttributeId(), ((Content) object).getValue()); } else if (object instanceof Element) { data.put(attributeSetting.getSetting().getAttributeId(), ((Element) object).getTextTrim()); } } if (attributeMatches.size() == 0) { info("Did not find a match for: %s\n in:\n %s", attributeSetting.getExpression().getExpression(), toXML(element)); } } if (data.size() > 0) { payload.add(data); } else { log(LogLevel.WARN, "Found entity element: <%s/> with no matching attributes. Please make sure your xpath expressions match", element.getName()); } } } if (payload.size() > rowsPerMessage) { callback.sendEntityDataMessage(null, payload); payload = new ArrayList<>(); } } catch (Exception e) { throw new RuntimeException(e); } } } if (payload.size() > 0) { callback.sendEntityDataMessage(null, payload); } }
/** * Creates and sets up a org.jdom2.input.SAXBuilder for parsing. * * @return a new org.jdom2.input.SAXBuilder object */ protected SAXBuilder createSAXBuilder() { SAXBuilder saxBuilder; if (validate) { saxBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING); } else { saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING); } saxBuilder.setEntityResolver(RESOLVER); // // This code is needed to fix the security problem outlined in // http://www.securityfocus.com/archive/1/297714 // // Unfortunately there isn't an easy way to check if an XML parser // supports a particular feature, so // we need to set it and catch the exception if it fails. We also need // to subclass the JDom SAXBuilder // class in order to get access to the underlying SAX parser - otherwise // the features don't get set until // we are already building the document, by which time it's too late to // fix the problem. // // Crimson is one parser which is known not to support these features. try { final XMLReader parser = saxBuilder.createParser(); setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-general-entities", false); setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-parameter-entities", false); setFeature(saxBuilder, parser, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); if(!allowDoctypes) { setFeature(saxBuilder, parser, "http://apache.org/xml/features/disallow-doctype-decl", true); } } catch (final JDOMException e) { throw new IllegalStateException("JDOM could not create a SAX parser", e); } saxBuilder.setExpandEntities(false); return saxBuilder; }
protected Document getJDomDoc() throws Exception { final SAXBuilder saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING); return saxBuilder.build(getFeedReader()); }
private static SAXBuilder createBuilder() { SAXBuilder sax = new SAXBuilder(XMLReaders.NONVALIDATING); sax.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); return sax; }
/** * Reads xml from an InputStream and returns the parsed root element. * * @param in * the InputStream that contains the XML document * @return the root element of the parsed input stream */ protected Element parseStream(InputStream in) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING); builder.setEntityResolver(MCREntityResolver.instance()); return builder.build(in).getRootElement(); }
/** * * @deprecated use SAXBuilder(XMLReaderJDOMFactory) with either XMLReaders.DTDVALIDATING or * XMLReaders.NONVALIDATING */ @Deprecated public SAXBuilder(final boolean validate) { super(validate ? XMLReaders.DTDVALIDATING : XMLReaders.NONVALIDATING); }