Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name, final String uri) 

Source Link

Document

Creates a new element with the supplied (local) name and a namespace given by a URI.

Usage

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createOther(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "other");
    Document doc = new Document();
    Element root = new Element("otherdata", OTHERNAMESPACE);
    doc.addContent(root);//from   w w w. j  av a 2s.c  om
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", OTHERNAMESPACE);
        packAge.setAttribute("pkgid", HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.setAttribute("name", rpmMetadata.name);
        packAge.setAttribute("arch", rpmMetadata.architecture);

        root.addContent(packAge);

        Element version = new Element("version", OTHERNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.setContent(version);

        for (ChangeLog log : rpmMetadata.changeLogs) {
            Element fileElement = new Element("changelog", OTHERNAMESPACE);
            fileElement.setAttribute("author", log.author);
            fileElement.setAttribute("date", log.date + "");
            fileElement.setText(log.text);
            packAge.addContent(fileElement);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createPrimary(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "primary");
    Document doc = new Document();
    Element root = new Element("metadata", COMMONNAMESPACE);
    doc.addContent(root);/*from   w  ww .  j  a v  a 2 s  . c om*/
    root.addNamespaceDeclaration(RPMNAMESPACE);
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", COMMONNAMESPACE);
        packAge.setAttribute("type", "rpm");
        root.addContent(packAge);

        Element name = new Element("name", COMMONNAMESPACE);
        name.setText(rpmMetadata.name);
        packAge.addContent(name);

        Element arch = new Element("arch", COMMONNAMESPACE);
        arch.setText(rpmMetadata.architecture);
        packAge.addContent(arch);

        Element version = new Element("version", COMMONNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.addContent(version);

        Element checksum = new Element("checksum", COMMONNAMESPACE);
        checksum.setAttribute("type", ALGORITHM);
        checksum.setAttribute("pkgid", "YES");
        checksum.setText(HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.addContent(checksum);

        Element summary = new Element("summary", COMMONNAMESPACE);
        summary.setText(rpmMetadata.summary);
        packAge.addContent(summary);

        Element description = new Element("description", COMMONNAMESPACE);
        description.setText(rpmMetadata.description);
        packAge.addContent(description);

        Element packager = new Element("packager", COMMONNAMESPACE);
        packager.setText(rpmMetadata.packager);
        packAge.addContent(packager);

        Element url = new Element("url", COMMONNAMESPACE);
        url.setText(rpmMetadata.url);
        packAge.addContent(url);

        Element time = new Element("time", COMMONNAMESPACE);
        time.setAttribute("file", rpmdata.rpm.lastModified() / 1000 + "");
        time.setAttribute("build", rpmMetadata.buildTime + "");
        packAge.addContent(time);

        Element size = new Element("size", COMMONNAMESPACE);
        size.setAttribute("package", rpmdata.rpm.length() + "");
        size.setAttribute("installed", rpmMetadata.installedSize + "");
        size.setAttribute("archive", rpmMetadata.archiveSize + "");
        packAge.addContent(size);

        Element location = new Element("location", COMMONNAMESPACE);
        location.setAttribute("href", replacePath(FileUtils.getFileRelativePath(rootPath, rpmdata.rpm)));
        packAge.addContent(location);

        Element format = new Element("format", COMMONNAMESPACE);
        packAge.addContent(format);

        Element license = new Element("license", RPMNAMESPACE);
        license.setText(rpmMetadata.license);
        format.addContent(license);

        Element vendor = new Element("vendor", RPMNAMESPACE);
        vendor.setText(rpmMetadata.vendor);
        format.addContent(vendor);

        Element group = new Element("group", RPMNAMESPACE);
        group.setText(rpmMetadata.group);
        format.addContent(group);

        Element buildhost = new Element("buildhost", RPMNAMESPACE);
        buildhost.setText(rpmMetadata.buildHost);
        format.addContent(buildhost);

        Element sourcerpm = new Element("sourcerpm", RPMNAMESPACE);
        sourcerpm.setText(rpmMetadata.sourceRpm);
        format.addContent(sourcerpm);

        Element headerRange = new Element("header-range", RPMNAMESPACE);
        headerRange.setAttribute("start", rpmMetadata.headerStart + "");
        headerRange.setAttribute("end", rpmMetadata.headerEnd + "");
        format.addContent(headerRange);

        Element provides = new Element("provides", RPMNAMESPACE);
        format.addContent(provides);
        addEntry(provides, rpmMetadata.provide, null);

        Element requires = new Element("requires", RPMNAMESPACE);
        format.addContent(requires);
        addEntry(requires, rpmMetadata.require, new PrivateRequireFilter());

        Element conflicts = new Element("conflicts", RPMNAMESPACE);
        format.addContent(conflicts);
        addEntry(conflicts, rpmMetadata.conflict, null);

        Element obsoletes = new Element("obsoletes", RPMNAMESPACE);
        format.addContent(obsoletes);
        addEntry(obsoletes, rpmMetadata.obsolete, null);

        YumFileter fileflter = new PrivateFileFilter();
        YumFileter fileDirflter = new PrivateFileDirFilter();
        for (com.github.cat.yum.store.model.File file : rpmMetadata.files) {
            if (StringUtils.isBlank(file.type)) {
                if (fileflter.filter(file.path)) {
                    continue;
                }
            } else if ("dir".equals(file.type)) {
                if (fileDirflter.filter(file.path)) {
                    continue;
                }
            }

            Element fileElemenrt = new Element("file", COMMONNAMESPACE);
            fileElemenrt.setText(file.path);
            if (!StringUtils.isBlank(file.type)) {
                fileElemenrt.setAttribute("type", file.type);
            }
            format.addContent(fileElemenrt);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createFilelitsts(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "filelists");
    Document doc = new Document();
    Element root = new Element("filelists", FILELISTSNAMESPACE);
    doc.addContent(root);/*from   www .ja v a 2s  .  c o  m*/
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", FILELISTSNAMESPACE);
        packAge.setAttribute("pkgid", HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.setAttribute("name", rpmMetadata.name);
        packAge.setAttribute("arch", rpmMetadata.architecture);
        root.addContent(packAge);

        Element version = new Element("version", FILELISTSNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.setContent(version);

        for (com.github.cat.yum.store.model.File file : rpmMetadata.files) {
            Element fileElement = new Element("file", FILELISTSNAMESPACE);
            fileElement.setText(file.path);
            if (file.type != null) {
                fileElement.setAttribute("type", file.type);
            }
            packAge.addContent(fileElement);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void addEntry(Element parent, List<Entry> entrys, YumFileter filter) {
    for (Entry entry : entrys) {
        Element entryElement = new Element("entry", RPMNAMESPACE);
        String name = entry.name;
        if (null != filter && filter.filter(name)) {
            continue;
        }//from w w w  .  j  av  a 2 s.  co m
        entryElement.setAttribute("name", name);
        if (null != entry.flags) {
            entryElement.setAttribute("flags", entry.flags);
        }
        if (null != entry.epoch) {
            entryElement.setAttribute("epoch", entry.epoch);
        }
        if (null != entry.version) {
            entryElement.setAttribute("ver", entry.version);
        }
        if (null != entry.release) {
            entryElement.setAttribute("rel", entry.release);
        }
        if (null != entry.pre) {
            entryElement.setAttribute("pre", entry.pre);
        }
        parent.addContent(entryElement);
    }
}

From source file:com.globalsight.dispatcher.bo.JobTask.java

License:Apache License

private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException {
    OutputStream writer = null;/* w  w  w  . j  ava2 s  . c om*/
    File fileStorage = CommonDAO.getFileStorage();
    File srcFile = p_job.getSrcFile();
    Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId());
    File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID()
            + File.separator + AppConstants.XLF_TARGET_FOLDER);
    File trgFile = new File(trgDir, srcFile.getName());
    FileUtils.copyFile(srcFile, trgFile);
    String encoding = FileUtil.getEncodingOfXml(trgFile);

    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(p_job.getSrcFile());
        Element root = doc.getRootElement(); // Get root element
        Namespace namespace = root.getNamespace();
        Element fileElem = root.getChild("file", namespace);
        XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace);
        List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace));
        for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size()
                && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) {
            if (p_targetSegments[trgIndex] == null) {
                continue;
            }

            Element elem = (Element) tuList.get(tuIndex);
            Element srcElem = elem.getChild("source", namespace);
            Element trgElem = elem.getChild("target", namespace);
            if (srcElem == null || srcElem.getContentSize() == 0) {
                trgIndex--;
                continue;
            }

            if (trgElem != null) {
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
            } else {
                trgElem = new Element("target", namespace);
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
                elem.addContent(trgElem);
            }

        }

        XMLOutputter xmlOutput = new XMLOutputter();
        Format format = Format.getRawFormat();
        format.setEncoding(encoding);
        writer = new FileOutputStream(trgFile);
        xmlOutput.setFormat(format);
        writeBOM(writer, format.getEncoding());
        xmlOutput.output(doc, writer);
        p_job.setTrgFile(trgFile);
        logger.info("Create Target File: " + trgFile);
    } catch (JDOMException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } catch (IOException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:com.googlecode.mipnp.mediaserver.cds.DidlLiteDocument.java

License:Open Source License

public DidlLiteDocument(URL mediaLocation, String filter) {
    this.mediaLocation = mediaLocation;
    this.filter = filter;
    Element root = new Element("DIDL-Lite", NS_DEFAULT);
    root.addNamespaceDeclaration(NS_DC);
    root.addNamespaceDeclaration(NS_UPNP);
    this.document = new Document(root);
}

From source file:com.googlecode.mipnp.mediaserver.cds.DidlLiteDocument.java

License:Open Source License

public void addCdsObject(CdsObject obj) {
    Element element = null;//  w  ww .  ja  va 2  s.  c  o  m
    if (obj.isContainer()) {
        element = new Element("container", NS_DEFAULT);
        addProperty(element, obj, CdsConstants.PROPERTY_SEARCHABLE, "true");
    } else {
        element = new Element("item", NS_DEFAULT);
    }

    for (String reqProp : REQUIRED_PROPERTIES) {
        addProperty(element, obj, reqProp);
    }

    // TODO: temp fix, res location should be added to CdsObject as a property
    Resource res = obj.getResource();
    if (res != null && (filter.equals("*") || filter.contains("res"))) {
        Element resEl = new Element(CdsConstants.PROPERTY_RES, NS_DEFAULT);
        resEl.setAttribute("protocolInfo", "http-get:*:" + res.getMimeType() + ":*");
        resEl.setText(mediaLocation.toString() + "/" + obj.getId());
        element.addContent(resEl);
    }
    // END temp fix

    if (filter.equals("*")) {
        for (String property : obj.getProperties()) {
            if (!REQUIRED_PROPERTIES.contains(property)) {
                addProperty(element, obj, property);
            }
        }
    } else {
        String[] filterParts = filter.split("(?<!\\\\),");
        for (int i = 0; i < filterParts.length; i++) {
            filterParts[i] = filterParts[i].replaceAll("\\\\,", ",");
            if (!REQUIRED_PROPERTIES.contains(filterParts[i])) {
                addProperty(element, obj, filterParts[i]);
            }
        }
    }

    document.getRootElement().addContent(element);
}

From source file:com.googlecode.mipnp.mediaserver.cds.DidlLiteDocument.java

License:Open Source License

private void addProperty(Element element, CdsObject obj, String property, String propertyValue) {

    if (property.contains("@")) {
        Element parentElement = null;
        String strParentElement = property.substring(0, property.indexOf('@'));
        if (strParentElement.equals("")) {
            parentElement = element;//from w w w.jav a  2 s . c  om
        } else {
            parentElement = element.getChild(removePrefix(strParentElement), getNamespace(strParentElement));
            if (parentElement == null) {
                parentElement = new Element(removePrefix(strParentElement), getNamespace(strParentElement));
                parentElement.setText(obj.getProperty(strParentElement));
                element.addContent(parentElement);
            }
        }
        parentElement.setAttribute(property.substring(property.indexOf('@') + 1), propertyValue);
    } else {
        Element newElement = new Element(removePrefix(property), getNamespace(property));
        newElement.setText(propertyValue);
        element.addContent(newElement);
    }
}

From source file:com.kixeye.kixmpp.client.KixmppClient.java

License:Apache License

/**
 * Performs auth.//from w ww.j  av a2 s  .com
 */
private void performAuth() {
    byte[] authToken = ("\0" + jid.getNode() + "\0" + password).getBytes(StandardCharsets.UTF_8);

    Element auth = new Element("auth", "urn:ietf:params:xml:ns:xmpp-sasl");
    auth.setAttribute("mechanism", "PLAIN");

    ByteBuf rawCredentials = channel.get().alloc().buffer().writeBytes(authToken);
    ByteBuf encodedCredentials = Base64.encode(rawCredentials);
    String encodedCredentialsString = encodedCredentials.toString(StandardCharsets.UTF_8);
    encodedCredentials.release();
    rawCredentials.release();

    auth.setText(encodedCredentialsString);

    channel.get().writeAndFlush(auth);
}

From source file:com.rometools.modules.atom.io.AtomModuleGenerator.java

License:Apache License

private Element generateLink(Link link) {
    Element linkElement = new Element("link", NS);

    if (link.getHref() != null) {
        Attribute href = new Attribute(AtomLinkAttribute.HREF, link.getHref());
        linkElement.setAttribute(href);/*from  ww  w .j a va2s.c om*/
    }
    if (link.getType() != null) {
        Attribute type = new Attribute(AtomLinkAttribute.TYPE, link.getType());
        linkElement.setAttribute(type);
    }
    if (link.getRel() != null) {
        Attribute rel = new Attribute(AtomLinkAttribute.REL, link.getRel());
        linkElement.setAttribute(rel);
    }

    if (link.getHreflang() != null) {
        final Attribute hreflangAttribute = new Attribute(AtomLinkAttribute.HREF_LANG, link.getHreflang());
        linkElement.setAttribute(hreflangAttribute);
    }

    if (link.getTitle() != null) {
        final Attribute title = new Attribute(AtomLinkAttribute.TITLE, link.getTitle());
        linkElement.setAttribute(title);
    }

    if (link.getLength() != 0) {
        final Attribute length = new Attribute(AtomLinkAttribute.LENGTH, Long.toString(link.getLength()));
        linkElement.setAttribute(length);
    }

    return linkElement;
}