Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

In this page you can find the example usage for org.jdom2 Document Document.

Prototype

public Document(List<? extends Content> content) 

Source Link

Document

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Usage

From source file:org.mycore.datamodel.ifs.MCRFile.java

License:Open Source License

/**
 * Build a XML representation of all technical metadata of this MCRFile and its MCRAudioVideoExtender, if present. That xml can be used for indexing this
 * data./*  ww w.  j av  a  2 s  .c  o m*/
 */
public Document createXML() {
    Element root = new Element("file");
    root.setAttribute("id", getID());
    root.setAttribute("owner", getOwnerID());
    root.setAttribute("name", getName());
    String absolutePath = getAbsolutePath();
    root.setAttribute("path", absolutePath);
    root.setAttribute("size", Long.toString(getSize()));
    root.setAttribute("extension", getExtension());
    root.setAttribute("contentTypeID", getContentTypeID());
    root.setAttribute("contentType", getContentType().getLabel());
    root.setAttribute("returnId", getMCRObjectID().toString());
    Collection<MCRCategoryID> linksFromReference = MCRCategLinkServiceFactory.getInstance()
            .getLinksFromReference(getCategLinkReference(MCRObjectID.getInstance(getOwnerID()), absolutePath));
    for (MCRCategoryID category : linksFromReference) {
        Element catEl = new Element("category");
        catEl.setAttribute("id", category.toString());
        root.addContent(catEl);
    }

    MCRISO8601Date iDate = new MCRISO8601Date();
    iDate.setDate(getLastModified().getTime());
    root.setAttribute("modified", iDate.getISOString());

    if (hasAudioVideoExtender()) {
        MCRAudioVideoExtender ext = getAudioVideoExtender();
        root.setAttribute("bitRate", String.valueOf(ext.getBitRate()));
        root.setAttribute("frameRate", String.valueOf(ext.getFrameRate()));
        root.setAttribute("duration", ext.getDurationTimecode());
        root.setAttribute("mediaType", (ext.hasVideoStream() ? "video" : "audio"));
    }

    return new Document(root);
}

From source file:org.mycore.datamodel.ifs.MCRFilesystemNode.java

License:Open Source License

/**
 * Stores additional XML data for this node. The name of the data element is
 * used as unique key for storing data. If data with this name already
 * exists, it is overwritten./*from  w  w  w.j av a 2 s  . c om*/
 * 
 * @param data
 *            the additional XML data to be saved
 * @throws IOException
 *             if the XML data can not be retrieved
 * @throws JDOMException
 *             if the XML data can not be parsed
 */
public void setAdditionalData(Element data) throws IOException, JDOMException {
    MCRFile dataFile = MCRFile.getRootFile(ID);
    Document doc;
    if (dataFile == null) {
        String name = "MCRFilesystemNode.additionalData";
        dataFile = new MCRFile(name, ID);
        doc = new Document(new Element("additionalData"));
    } else {
        doc = dataFile.getContentAsJDOM();
    }

    Element child = doc.getRootElement().getChild(data.getName());
    if (child != null) {
        child.detach();
    }
    doc.getRootElement().addContent((Element) data.clone());
    dataFile.setContentFrom(doc);
}

From source file:org.mycore.datamodel.ifs2.MCRFileCollection.java

License:Open Source License

/**
 * Creates a new file collection in the given store, or retrieves an
 * existing one./* ww  w.  jav a2  s  . c o  m*/
 * 
 * @see MCRFileStore
 * 
 * @param store
 *            the store this file collection is stored in
 * @param id
 *            the ID of this file collection
 */
protected MCRFileCollection(MCRStore store, int id) throws IOException {
    super(null, store.getSlot(id), new Element("collection"));
    this.store = store;
    this.id = id;
    if (fo.exists()) {
        readAdditionalData();
    } else {
        fo.createFolder();
        new Document(data);
        saveAdditionalData();
    }
}

From source file:org.mycore.datamodel.ifs2.MCRFileCollection.java

License:Open Source License

private void readAdditionalData() throws IOException {
    FileObject src = VFS.getManager().resolveFile(fo, dataFile);
    if (!src.exists()) {
        LOGGER.warn("Metadata file is missing, repairing metadata...");
        data = new Element("collection");
        new Document(data);
        repairMetadata();//www.  j  a  v a  2 s  .co  m
    }
    try {
        data = new MCRVFSContent(src).asXML().getRootElement();
    } catch (JDOMException | SAXException e) {
        throw new IOException(e);
    }
}

From source file:org.mycore.datamodel.ifs2.MCRStoreBrowserServlet.java

License:Open Source License

/** Builds the xml output to be rendered as response */
Document buildResponseXML() throws Exception {
    File dir = getRequestedDirectory();

    String[] children = dir.list();
    Element xml = new Element("storeBrowser");
    if (children != null)
        for (String child : children) {
            xml.addContent(buildXML(child));
        }//ww  w.j  a v a  2s . co  m

    return new Document(xml);
}

From source file:org.mycore.datamodel.language.MCRLanguageResolver.java

License:Open Source License

public Source resolve(String href, String base) throws TransformerException {
    try {// ww w.  ja  v  a  2 s  .  c o m
        String code = href.split(":")[1];
        MCRLanguage language = MCRLanguageFactory.instance().getLanguage(code);
        Document doc = new Document(buildXML(language));
        return new JDOMSource(doc);
    } catch (Exception ex) {
        throw new TransformerException(ex);
    }
}

From source file:org.mycore.datamodel.metadata.MCRBase.java

License:Open Source License

/**
 * This method create a XML stream for all object data.
 * //from  ww w  .j  a  va 2  s  . com
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Document with the XML data of the object as byte array
 */
public Document createXML() throws MCRException {
    validate();
    Element elm = new Element(getRootTagName());
    Document doc = new Document(elm);
    elm.addNamespaceDeclaration(XSI_NAMESPACE);
    elm.addNamespaceDeclaration(XLINK_NAMESPACE);
    elm.setAttribute("noNamespaceSchemaLocation", mcr_schema, XSI_NAMESPACE);
    elm.setAttribute("ID", mcr_id.toString());
    if (mcr_label != null) {
        elm.setAttribute("label", mcr_label);
    }
    elm.setAttribute("version", mcr_version);
    return doc;
}

From source file:org.mycore.datamodel.niofs.MCRPathXML.java

License:Open Source License

/**
 * Returns metadata of the file retrievable by 'path' in XML form.
 * //from  www.j  a v  a  2s.  c o m
 * @param path
 *            Path to File
 * @param attrs
 *            file attributes of given file
 */
public static Document getFileXML(MCRPath path, BasicFileAttributes attrs) throws IOException {
    Element root = new Element("file");
    root.setAttribute("uri", path.toUri().toString());
    root.setAttribute("ownerID", path.getOwner());
    String fileName = path.getFileName().toString();
    root.setAttribute("name", fileName);
    String absolutePath = path.getOwnerRelativePath();
    root.setAttribute("path", absolutePath);
    root.setAttribute("extension", getFileExtension(fileName));
    root.setAttribute("returnId", MCRMetadataManager
            .getObjectId(MCRObjectID.getInstance(path.getOwner()), 10, TimeUnit.SECONDS).toString());
    Collection<MCRCategoryID> linksFromReference = MCRCategLinkServiceFactory.getInstance()
            .getLinksFromReference(new MCRCategLinkReference(path));
    for (MCRCategoryID category : linksFromReference) {
        Element catEl = new Element("category");
        catEl.setAttribute("id", category.toString());
        root.addContent(catEl);
    }
    if (!attrs.isDirectory() && attrs instanceof MCRFileAttributes<?>) {
        addAttributes(root, (MCRFileAttributes<?>) attrs, path);
    } else {
        addBasicAttributes(root, attrs, path);
    }
    return new Document(root);
}

From source file:org.mycore.frontend.basket.MCRBasketResolver.java

License:Open Source License

public Source resolve(String href, String base) throws TransformerException {
    try {/*from www .  j a va2  s .  co m*/
        String[] tokens = href.split(":");
        String type = tokens[1];
        MCRBasket basket = MCRBasketManager.getOrCreateBasketInSession(type);
        if (tokens.length > 2) {
            String id = tokens[2];

            MCRBasketEntry entry = basket.get(id);
            entry.resolveContent();
            Element xml = new MCRBasketXMLBuilder(true).buildXML(entry);
            Document doc = new Document(xml);
            return new JDOMSource(doc);
        } else {
            //resolve entire basket
            MCRBasketXMLBuilder basketXMLBuilder = new MCRBasketXMLBuilder(false);
            Document doc = basketXMLBuilder.buildXML(basket);
            return new JDOMSource(doc);
        }
    } catch (Exception ex) {
        throw new TransformerException(ex);
    }
}

From source file:org.mycore.frontend.basket.MCRBasketXMLBuilder.java

License:Open Source License

/**
 * Builds an XML representation of a basket and its entries.
 *//*from ww w .j a  v  a  2  s . c  o  m*/
public Document buildXML(MCRBasket basket) {
    Element xml = new Element("basket");
    xml.setAttribute("type", basket.getType());

    String derivateID = basket.getDerivateID();
    if (derivateID != null)
        xml.setAttribute("id", derivateID);

    for (MCRBasketEntry entry : basket)
        xml.addContent(buildXML(entry));
    return new Document(xml);
}