Example usage for org.jdom2 Element getDocument

List of usage examples for org.jdom2 Element getDocument

Introduction

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

Prototype

public Document getDocument() 

Source Link

Document

Return this child's owning document or null if the branch containing this child is currently not attached to a document.

Usage

From source file:com.izforge.izpack.util.xmlmerge.action.DtdInsertAction.java

License:Open Source License

/**
 * Gets the DTD declared in the doctype of the element's owning document.
 *
 * @param element The element for which the DTD will be retrieved
 * @return The DTD declared in the doctype of the element's owning document
 * @throws DocumentException If an error occurred during DTD retrieval
 *///  ww w  .java  2 s .c  om
public DTD getDTD(Element element) throws DocumentException {

    if (element.getDocument().getDocType() != null) {

        String systemId = element.getDocument().getDocType().getSystemID();

        DTD dtd = s_dtdMap.get(systemId);

        // if not in cache, create the DTD and put it in cache
        if (dtd == null) {
            Reader reader;
            URL url;

            // lookup URL of DTD
            try {
                url = new URL(systemId);
                reader = new InputStreamReader(url.openStream());
            } catch (MalformedURLException e) {
                throw new DocumentException(element.getDocument(), e);
            } catch (IOException ioe) {
                throw new DocumentException(element.getDocument(), ioe);
            }

            try {
                dtd = new DTDParser(reader).parse();
            } catch (IOException ioe) {
                throw new DocumentException(element.getDocument(), ioe);
            }

            s_dtdMap.put(systemId, dtd);
        }

        return dtd;

    } else {
        throw new DocumentException(element.getDocument(),
                "No DTD specified in document " + element.getDocument());
    }
}

From source file:com.rometools.modules.sle.io.ItemParser.java

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>/*from w w  w  .ja va  2  s  . co m*/
 *
 * @param element the XML node (JDOM element) to extract module information from.
 * @return a module instance, <b>null</b> if the element did not have module information.
 */
@Override
public Module parse(final Element element, final Locale locale) {
    final SleEntryImpl sle = new SleEntryImpl();
    ArrayList<EntryValue> values = new ArrayList<EntryValue>();
    final List<Element> groups = element.getChildren("group", ModuleParser.TEMP);

    for (final Element group : groups) {
        final StringValue value = new StringValue();
        value.setElement(group.getAttributeValue("element"));
        value.setLabel(group.getAttributeValue("label"));
        value.setValue(group.getAttributeValue("value"));
        if (group.getAttributeValue("ns") != null) {
            value.setNamespace(Namespace.getNamespace(group.getAttributeValue("ns")));
        } else {
            value.setNamespace(element.getDocument().getRootElement().getNamespace());
        }
        values.add(value);
        element.removeContent(group);
    }

    sle.setGroupValues(values.toArray(new EntryValue[values.size()]));
    values = values.size() == 0 ? values : new ArrayList<EntryValue>();

    final List<Element> sorts = new ArrayList<Element>(element.getChildren("sort", ModuleParser.TEMP));
    // LOG.debug("]]] sorts on element"+sorts.size());
    for (final Element sort : sorts) {
        final String dataType = sort.getAttributeValue("data-type");
        // LOG.debug("Doing datatype "+dataType +" :: "+sorts.size());
        if (dataType == null || dataType.equals(Sort.TEXT_TYPE)) {
            final StringValue value = new StringValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            value.setValue(sort.getAttributeValue("value"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }
            values.add(value);

            element.removeContent(sort);

        } else if (dataType.equals(Sort.DATE_TYPE)) {
            final DateValue value = new DateValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }
            Date dateValue = null;

            try {
                dateValue = DateParser.parseRFC822(sort.getAttributeValue("value"), locale);
                if (dateValue == null) {
                    dateValue = DateParser.parseW3CDateTime(sort.getAttributeValue("value"), locale);
                }
            } catch (final Exception e) {
                ; // ignore parse exceptions
            }

            value.setValue(dateValue);
            values.add(value);
            element.removeContent(sort);
        } else if (dataType.equals(Sort.NUMBER_TYPE)) {
            final NumberValue value = new NumberValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }

            try {
                value.setValue(new BigDecimal(sort.getAttributeValue("value")));
            } catch (final NumberFormatException nfe) {
                ; // ignore
                values.add(value);
                element.removeContent(sort);
            }
        } else {
            throw new RuntimeException("Unknown datatype");
        }
    }
    // LOG.debug("Values created "+values.size()+" from sorts" +sorts.size());
    sle.setSortValues(values.toArray(new EntryValue[values.size()]));

    return sle;
}

From source file:com.rometools.rome.io.impl.Atom03Parser.java

License:Open Source License

protected WireFeed parseFeed(final Element eFeed, final Locale locale) {

    final String type = getType();
    final Document document = eFeed.getDocument();
    final String styleSheet = getStyleSheet(document);

    final Feed feed = new Feed(type);
    feed.setStyleSheet(styleSheet);//from  www .ja va 2  s .  c  o m

    final Element title = eFeed.getChild("title", getAtomNamespace());
    if (title != null) {
        feed.setTitleEx(parseContent(title));
    }

    final List<Element> links = eFeed.getChildren("link", getAtomNamespace());
    feed.setAlternateLinks(parseAlternateLinks(links));
    feed.setOtherLinks(parseOtherLinks(links));

    final Element author = eFeed.getChild("author", getAtomNamespace());
    if (author != null) {
        final List<SyndPerson> authors = new ArrayList<SyndPerson>();
        authors.add(parsePerson(author));
        feed.setAuthors(authors);
    }

    final List<Element> contributors = eFeed.getChildren("contributor", getAtomNamespace());
    if (!contributors.isEmpty()) {
        feed.setContributors(parsePersons(contributors));
    }

    final Element tagline = eFeed.getChild("tagline", getAtomNamespace());
    if (tagline != null) {
        feed.setTagline(parseContent(tagline));
    }

    final Element id = eFeed.getChild("id", getAtomNamespace());
    if (id != null) {
        feed.setId(id.getText());
    }

    final Element generator = eFeed.getChild("generator", getAtomNamespace());
    if (generator != null) {
        final Generator gen = new Generator();
        gen.setValue(generator.getText());
        String att = getAttributeValue(generator, "url");
        if (att != null) {
            gen.setUrl(att);
        }
        att = getAttributeValue(generator, "version");
        if (att != null) {
            gen.setVersion(att);
        }
        feed.setGenerator(gen);
    }

    final Element copyright = eFeed.getChild("copyright", getAtomNamespace());
    if (copyright != null) {
        feed.setCopyright(copyright.getText());
    }

    final Element info = eFeed.getChild("info", getAtomNamespace());
    if (info != null) {
        feed.setInfo(parseContent(info));
    }

    final Element modified = eFeed.getChild("modified", getAtomNamespace());
    if (modified != null) {
        feed.setModified(DateParser.parseDate(modified.getText(), locale));
    }

    feed.setModules(parseFeedModules(eFeed, locale));

    final List<Element> entries = eFeed.getChildren("entry", getAtomNamespace());
    if (!entries.isEmpty()) {
        feed.setEntries(parseEntries(entries, locale));
    }

    final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
    if (!foreignMarkup.isEmpty()) {
        feed.setForeignMarkup(foreignMarkup);
    }

    return feed;

}

From source file:com.rometools.rome.io.impl.Atom10Parser.java

License:Open Source License

protected WireFeed parseFeed(final Element eFeed, final Locale locale) throws FeedException {

    String baseURI = null;//  www . j a  v a2  s . c  om
    try {
        baseURI = findBaseURI(eFeed);
    } catch (final Exception e) {
        throw new FeedException("ERROR while finding base URI of feed", e);
    }

    final Feed feed = parseFeedMetadata(baseURI, eFeed, locale);
    feed.setStyleSheet(getStyleSheet(eFeed.getDocument()));

    final String xmlBase = eFeed.getAttributeValue("base", Namespace.XML_NAMESPACE);
    if (xmlBase != null) {
        feed.setXmlBase(xmlBase);
    }

    feed.setModules(parseFeedModules(eFeed, locale));

    final List<Element> eList = eFeed.getChildren("entry", getAtomNamespace());
    if (!eList.isEmpty()) {
        feed.setEntries(parseEntries(feed, baseURI, eList, locale));
    }

    final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
    if (!foreignMarkup.isEmpty()) {
        feed.setForeignMarkup(foreignMarkup);
    }
    return feed;
}

From source file:com.rometools.rome.io.impl.RSS090Parser.java

License:Open Source License

/**
 * Parses the root element of an RSS document into a Channel bean.
 * <p/>/*from   ww w.  j a  v a  2s. c om*/
 * It reads title, link and description and delegates to parseImage, parseItems and
 * parseTextInput. This delegation always passes the root element of the RSS document as
 * different RSS version may have this information in different parts of the XML tree (no
 * assumptions made thanks to the specs variaty)
 * <p/>
 *
 * @param rssRoot the root element of the RSS document to parse.
 * @return the parsed Channel bean.
 */
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {

    final Channel channel = new Channel(getType());
    channel.setStyleSheet(getStyleSheet(rssRoot.getDocument()));

    final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());

    final Element title = eChannel.getChild("title", getRSSNamespace());
    if (title != null) {
        channel.setTitle(title.getText());
    }

    final Element link = eChannel.getChild("link", getRSSNamespace());
    if (link != null) {
        channel.setLink(link.getText());
    }

    final Element description = eChannel.getChild("description", getRSSNamespace());
    if (description != null) {
        channel.setDescription(description.getText());
    }

    channel.setImage(parseImage(rssRoot));

    channel.setTextInput(parseTextInput(rssRoot));

    // Unfortunately Microsoft's SSE extension has a special case of effectively putting the
    // sharing channel module inside the RSS tag and not inside the channel itself. So we also
    // need to look for channel modules from the root RSS element.
    final List<Module> allFeedModules = new ArrayList<Module>();
    final List<Module> rootModules = parseFeedModules(rssRoot, locale);
    final List<Module> channelModules = parseFeedModules(eChannel, locale);

    if (rootModules != null) {
        allFeedModules.addAll(rootModules);
    }

    if (channelModules != null) {
        allFeedModules.addAll(channelModules);
    }

    channel.setModules(allFeedModules);
    channel.setItems(parseItems(rssRoot, locale));

    final List<Element> foreignMarkup = extractForeignMarkup(eChannel, channel, getRSSNamespace());
    if (!foreignMarkup.isEmpty()) {
        channel.setForeignMarkup(foreignMarkup);
    }

    return channel;

}

From source file:cz.cesnet.shongo.connector.device.AdobeConnectConnector.java

@Override
public String exportRoomSettings(String roomId) throws CommandException {
    Element scoInfo = getScoInfo(roomId);
    Document document = scoInfo.getDocument();
    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
    return xmlOutput.outputString(document);
}

From source file:org.jahia.utils.osgi.parsers.AbstractXmlFileParser.java

License:Open Source License

/**
 * Utility method to retrieve an XML element using an XPath expression. Note that this method is
 * namespace aware and will require you to use the "xp" prefix in your XPath queries. For example, an XPath query
 * for a Spring XML configuration will look like this :
 * /xp:beans/xp:bean[@id="FileListSync"]/xp:property[@name="syncUrl"]
 * Currently there is no way to rename the prefix.
 *
 * @param scopeElement    the scope in which to execute the XPath query
 * @param xPathExpression the XPath query to select the element we wish to retrieve. In the case where multiple
 *                        elements match, only the first one will be returned.
 * @return the first element that matches the XPath expression, or null if no element matches.
 * @throws JDOMException raised if there was a problem navigating the JDOM structure.
 *///from w w  w. j  av  a2 s.c o  m
public static Element getElement(Element scopeElement, String xPathExpression) throws JDOMException {
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace("xp", namespaceURI);
    }
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    return (Element) xPath.selectSingleNode(scopeElement);
}

From source file:org.jahia.utils.osgi.parsers.AbstractXmlFileParser.java

License:Open Source License

public List<?> selectNodes(Element scopeElement, String xPathExpression) throws JDOMException {
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace("xp", namespaceURI);
    }//from w  ww. j  av  a2  s.co m
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }

    return xPath.selectNodes(scopeElement);
}

From source file:org.jahia.utils.osgi.parsers.AbstractXmlFileParser.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Attribute> getAttributes(Element scopeElement, String xPathExpression) throws JDOMException {
    List<Attribute> elems = new LinkedList<Attribute>();
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace("xp", namespaceURI);
    }//from w  w w  . ja  v a  2 s  .c om
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    for (Object obj : xPath.selectNodes(scopeElement)) {
        if (obj instanceof Attribute) {
            elems.add((Attribute) obj);
        }
    }

    return elems;
}

From source file:org.jahia.utils.osgi.parsers.AbstractXmlFileParser.java

License:Open Source License

public List<Object> getNodes(Element scopeElement, String xPathExpression, String defaultPrefix)
        throws JDOMException {
    List<Object> nodes = new LinkedList<Object>();
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace(defaultPrefix, namespaceURI);
    }/*  w w w .j av  a 2  s.c om*/
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    for (Object obj : xPath.selectNodes(scopeElement)) {
        nodes.add(obj);
    }
    return nodes;
}