Java Code Examples for org.jdom2.Document#addContent()

The following examples show how to use org.jdom2.Document#addContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetadataPersister.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends N2oMetadata> void persist(T n2o, String directory) {
    boolean isCreate = metadataRegister.contains(n2o.getId(), n2o.getClass());
    checkLock();
    Element element = persisterFactory.produce(n2o).persist(n2o, n2o.getNamespace());
    Document doc = new Document();
    doc.addContent(element);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(XmlUtil.N2O_FORMAT);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        xmlOutput.output(doc, outputStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    InfoConstructor info = findOrCreateXmlInfo(n2o, directory);
    String path = PathUtil.convertUrlToAbsolutePath(info.getURI());
    if (path == null)
        throw new IllegalStateException();
    watchDir.skipOn(path);
    try {
        saveContentToFile(new ByteArrayInputStream(outputStream.toByteArray()), new File(path));
        metadataRegister.update(info);//if exists
        metadataRegister.add(info);
        eventBus.publish(new ConfigPersistEvent(this, info, isCreate));
    } finally {
        watchDir.takeOn(path);
    }
}
 
Example 2
Source File: PersistOperation.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
private ByteArrayOutputStream writeDocument(S source) {
    Element element = persisterFactory.produce(source).persist(source, source.getNamespace());
    Document doc = new Document();
    doc.addContent(element);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(format);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        xmlOutput.output(doc, outputStream);
    } catch (IOException e) {
        throw new N2oException("Error during reading metadata " + source.getId(), e);
    }
    return outputStream;
}
 
Example 3
Source File: MCROAIDataProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add link to XSL stylesheet for displaying OAI response in web browser.
 */
private Document addXSLStyle(Document doc) {
    String styleSheet = MCROAIAdapter.PREFIX + getServletName() + ".ResponseStylesheet";
    String xsl = MCRConfiguration2.getString(styleSheet).orElse("oai/oai2.xsl");
    if (!xsl.isEmpty()) {
        Map<String, String> pairs = new HashMap<>();
        pairs.put("type", "text/xsl");
        pairs.put("href", MCRFrontendUtil.getBaseURL() + xsl);
        doc.addContent(0, new ProcessingInstruction("xml-stylesheet", pairs));
    }
    return doc;
}
 
Example 4
Source File: MCRDataSourceCall.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
protected Boolean validate(String id) {
    final Document doc = new Document();
    doc.addContent(this.result.detach());

    try {
        MCRXMLHelper.validate(doc, MCRMODSCommands.MODS_V3_XSD_URI);
    } catch (SAXException | IOException e) {
        LOGGER.warn(ds.getID() + " with " + id + " returned invalid mods!", e);
        return false;
    }

    return true;
}
 
Example 5
Source File: OPML10Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an XML document (JDOM) for the given feed bean.
 *
 * @param feed the feed bean to generate the XML document from.
 * @return the generated XML document (JDOM).
 * @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the
 *             WireFeedGenerator.
 * @throws FeedException thrown if the XML Document could not be created.
 */
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {

    if (!(feed instanceof Opml)) {
        throw new IllegalArgumentException("Not an OPML file");
    }

    final Opml opml = (Opml) feed;
    final Document doc = new Document();
    final Element root = new Element("opml");
    root.setAttribute("version", "1.0");
    doc.addContent(root);

    final Element head = generateHead(opml);

    if (head != null) {
        root.addContent(head);
    }

    final Element body = new Element("body");
    root.addContent(body);
    super.generateFeedModules(opml.getModules(), root);
    body.addContent(generateOutlines(opml.getOutlines()));

    return doc;
}
 
Example 6
Source File: MCRRestAPIObjectsHelper.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private static Document listDerivateContentAsXML(MCRDerivate derObj, String path, int depth, UriInfo info,
    Application app)
    throws IOException {
    Document doc = new Document();

    MCRPath root = MCRPath.getPath(derObj.getId().toString(), "/");
    root = MCRPath.toMCRPath(root.resolve(path));
    if (depth == -1) {
        depth = Integer.MAX_VALUE;
    }
    if (root != null) {
        Element eContents = new Element("contents");
        eContents.setAttribute("mycoreobject", derObj.getOwnerID().toString());
        eContents.setAttribute("mycorederivate", derObj.getId().toString());
        doc.addContent(eContents);
        if (!path.endsWith("/")) {
            path += "/";
        }
        MCRPath p = MCRPath.getPath(derObj.getId().toString(), path);
        if (p != null && Files.exists(p)) {
            Element eRoot = MCRPathXML.getDirectoryXML(p).getRootElement();
            eContents.addContent(eRoot.detach());
            createXMLForSubdirectories(p, eRoot, 1, depth);
        }

        //add href Attributes
        String baseURL = MCRJerseyUtil.getBaseURL(info, app)
            + MCRConfiguration2.getStringOrThrow("MCR.RestAPI.v1.Files.URL.path");
        baseURL = baseURL.replace("${mcrid}", derObj.getOwnerID().toString()).replace("${derid}",
            derObj.getId().toString());
        XPathExpression<Element> xp = XPathFactory.instance().compile(".//child[@type='file']", Filters.element());
        for (Element e : xp.evaluate(eContents)) {
            String uri = e.getChildText("uri");
            if (uri != null) {
                int pos = uri.lastIndexOf(":/");
                String subPath = uri.substring(pos + 2);
                while (subPath.startsWith("/")) {
                    subPath = path.substring(1);
                }
                e.setAttribute("href", baseURL + subPath);
            }
        }
    }
    return doc;
}