Example usage for org.jdom2 Namespace XML_NAMESPACE

List of usage examples for org.jdom2 Namespace XML_NAMESPACE

Introduction

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

Prototype

Namespace XML_NAMESPACE

To view the source code for org.jdom2 Namespace XML_NAMESPACE.

Click Source Link

Document

Define a Namespace for the standard xml prefix.

Usage

From source file:com.sun.syndication.io.impl.Atom10Generator.java

License:Open Source License

protected Element createRootElement(Feed feed) {
    Element root = new Element("feed", getFeedNamespace());
    root.addNamespaceDeclaration(getFeedNamespace());
    //Attribute version = new Attribute("version", getVersion());
    //root.setAttribute(version);
    if (feed.getXmlBase() != null) {
        root.setAttribute("base", feed.getXmlBase(), Namespace.XML_NAMESPACE);
    }// w w w .  ja va2 s .  c  o m
    generateModuleNamespaceDefs(root);
    return root;
}

From source file:com.sun.syndication.io.impl.Atom10Generator.java

License:Open Source License

protected void addEntry(Entry entry, Element parent) throws FeedException {
    Element eEntry = new Element("entry", getFeedNamespace());
    if (entry.getXmlBase() != null) {
        eEntry.setAttribute("base", entry.getXmlBase(), Namespace.XML_NAMESPACE);
    }//w  ww .j  ava2s.  c  o m
    populateEntry(entry, eEntry);
    generateForeignMarkup(eEntry, (List) entry.getForeignMarkup());
    checkEntryConstraints(eEntry);
    generateItemModules(entry.getModules(), eEntry);
    parent.addContent(eEntry);
}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

protected WireFeed parseFeed(Element eFeed) throws FeedException {

    String baseURI = null;/*from  ww w . jav  a 2s.  c o m*/
    try {
        baseURI = findBaseURI(eFeed);
    } catch (Exception e) {
        throw new FeedException("ERROR while finding base URI of feed", e);
    }

    Feed feed = parseFeedMetadata(baseURI, eFeed);

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

    feed.setModules(parseFeedModules(eFeed));

    List eList = eFeed.getChildren("entry", getAtomNamespace());
    if (eList.size() > 0) {
        feed.setEntries(parseEntries(feed, baseURI, eList));
    }

    List foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
    if (foreignMarkup.size() > 0) {
        feed.setForeignMarkup(foreignMarkup);
    }
    return feed;
}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

protected Entry parseEntry(Feed feed, Element eEntry, String baseURI) {
    Entry entry = new Entry();

    String xmlBase = eEntry.getAttributeValue("base", Namespace.XML_NAMESPACE);
    if (xmlBase != null) {
        entry.setXmlBase(xmlBase);/*from w  w w . jav  a  2s. com*/
    }

    Element e = eEntry.getChild("title", getAtomNamespace());
    if (e != null) {
        Content c = new Content();
        c.setValue(parseTextConstructToString(e));
        c.setType(getAttributeValue(e, "type"));
        entry.setTitleEx(c);
    }

    List eList = eEntry.getChildren("link", getAtomNamespace());
    entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, eList));
    entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, eList));

    eList = eEntry.getChildren("author", getAtomNamespace());
    if (eList.size() > 0) {
        entry.setAuthors(parsePersons(baseURI, eList));
    }

    eList = eEntry.getChildren("contributor", getAtomNamespace());
    if (eList.size() > 0) {
        entry.setContributors(parsePersons(baseURI, eList));
    }

    e = eEntry.getChild("id", getAtomNamespace());
    if (e != null) {
        entry.setId(e.getText());
    }

    e = eEntry.getChild("updated", getAtomNamespace());
    if (e != null) {
        entry.setUpdated(DateParser.parseDate(e.getText()));
    }

    e = eEntry.getChild("published", getAtomNamespace());
    if (e != null) {
        entry.setPublished(DateParser.parseDate(e.getText()));
    }

    e = eEntry.getChild("summary", getAtomNamespace());
    if (e != null) {
        entry.setSummary(parseContent(e));
    }

    e = eEntry.getChild("content", getAtomNamespace());
    if (e != null) {
        List contents = new ArrayList();
        contents.add(parseContent(e));
        entry.setContents(contents);
    }

    e = eEntry.getChild("rights", getAtomNamespace());
    if (e != null) {
        entry.setRights(e.getText());
    }

    List cList = eEntry.getChildren("category", getAtomNamespace());
    entry.setCategories(parseCategories(baseURI, cList));

    // TODO: SHOULD handle Atom entry source element
    e = eEntry.getChild("source", getAtomNamespace());
    if (e != null) {
        entry.setSource(parseFeedMetadata(baseURI, e));
    }

    entry.setModules(parseItemModules(eEntry));

    List foreignMarkup = extractForeignMarkup(eEntry, entry, getAtomNamespace());
    if (foreignMarkup.size() > 0) {
        entry.setForeignMarkup(foreignMarkup);
    }
    return entry;
}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

/**
 * Resolve URI via base URL and parent element.
 * Resolve URI based considering xml:base and baseURI.
 * @param baseURI Base URI used to fetch the XML document
 * @param parent  Parent element from which to consider xml:base
 * @param url     URL to be resolved//from  w  w  w  .ja v  a2 s.  co m
 */
public static String resolveURI(String baseURI, Parent parent, String url) {
    if (!resolveURIs) {
        return url;
    }
    if (isRelativeURI(url)) {
        url = (!".".equals(url) && !"./".equals(url)) ? url : "";

        if (url.startsWith("/") && baseURI != null) {
            String base = null;
            int slashslash = baseURI.indexOf("//");
            int nextslash = baseURI.indexOf("/", slashslash + 2);
            if (nextslash != -1)
                base = baseURI.substring(0, nextslash);
            return formURI(base, url);
        }

        // Relative URI with parent
        if (parent != null && parent instanceof Element) {

            // Do we have an xml:base?         
            String xmlbase = ((Element) parent).getAttributeValue("base", Namespace.XML_NAMESPACE);
            if (xmlbase != null && xmlbase.trim().length() > 0) {
                if (isAbsoluteURI(xmlbase)) {
                    // Absolute xml:base, so form URI right now 
                    if (url.startsWith("/")) {
                        // Host relative URI
                        int slashslash = xmlbase.indexOf("//");
                        int nextslash = xmlbase.indexOf("/", slashslash + 2);
                        if (nextslash != -1)
                            xmlbase = xmlbase.substring(0, nextslash);
                        return formURI(xmlbase, url);
                    }
                    if (!xmlbase.endsWith("/")) {
                        // Base URI is filename, strip it off 
                        xmlbase = xmlbase.substring(0, xmlbase.lastIndexOf("/"));
                    }
                    return formURI(xmlbase, url);
                } else {
                    // Relative xml:base, so walk up tree
                    return resolveURI(baseURI, parent.getParent(),
                            stripTrailingSlash(xmlbase) + "/" + stripStartingSlash(url));
                }
            }
            // No xml:base so walk up tree
            return resolveURI(baseURI, parent.getParent(), url);

            // Relative URI with no parent (i.e. top of tree), so form URI right now
        } else if (parent == null || parent instanceof Document) {
            return formURI(baseURI, url);
        }
    }
    return url;
}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

/**
 * Parse entry from reader.//  w ww .  j a v a2 s .com
 */
public static Entry parseEntry(Reader rd, String baseURI)
        throws JDOMException, IOException, IllegalArgumentException, FeedException {
    // Parse entry into JDOM tree
    SAXBuilder builder = new SAXBuilder();
    Document entryDoc = builder.build(rd);
    Element fetchedEntryElement = entryDoc.getRootElement();
    fetchedEntryElement.detach();

    // Put entry into a JDOM document with 'feed' root so that Rome can handle it
    Feed feed = new Feed();
    feed.setFeedType("atom_1.0");
    WireFeedOutput wireFeedOutput = new WireFeedOutput();
    Document feedDoc = wireFeedOutput.outputJDom(feed);
    feedDoc.getRootElement().addContent(fetchedEntryElement);

    if (baseURI != null) {
        feedDoc.getRootElement().setAttribute("base", baseURI, Namespace.XML_NAMESPACE);
    }

    WireFeedInput input = new WireFeedInput();
    Feed parsedFeed = (Feed) input.build(feedDoc);
    return (Entry) parsedFeed.getEntries().get(0);
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractDocumentHeaders(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = documentSchemeURL == null ? new TEIDocumentValidator()
            : new FromURLValidator(documentSchemeURL);

    File documentDir = new File(outputDirectory, "DocumentHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }/*from   ww  w.ja  va  2  s  .c  o m*/

    Element documentRoot = Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));

    for (Element docHeader : documentRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("DocumentHeader".equals(docHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(docHeader.clone());
        Document newDoc = new Document(tei);

        if (documentSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIDocumentValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + documentSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        String id = fileDesc.getAttributeValue("id", Namespace.XML_NAMESPACE);
        if (id != null) {
            outName = id;
        } else {
            Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));

            String title = titleStmt.getChildText("title", null);
            if (title != null) {
                outName = title;
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN DOCUMENT HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:de.nava.informa.parsers.Atom_0_3_Parser.java

License:Open Source License

/**
 * @see de.nava.informa.core.ChannelParserIF#parse(de.nava.informa.core.ChannelBuilderIF, org.jdom2.Element)
 *///from  w ww . jav  a  2  s . co m
public ChannelIF parse(ChannelBuilderIF cBuilder, Element channel) throws ParseException {
    if (cBuilder == null) {
        throw new RuntimeException("Without builder no channel can " + "be created.");
    }

    Date dateParsed = new Date();
    Namespace defNS = ParserUtils.getDefaultNS(channel);

    if (defNS == null) {
        defNS = Namespace.NO_NAMESPACE;
        LOGGER.info("No default namespace found.");
    }

    // RSS 1.0 Dublin Core Module namespace
    Namespace dcNS = ParserUtils.getNamespace(channel, "dc");

    if (dcNS == null) {
        LOGGER.debug("No namespace for dublin core found");
        dcNS = defNS;
    }

    LOGGER.debug("start parsing.");

    // get version attribute
    String formatVersion = "0.3";

    if (channel.getAttribute("version") != null) {
        formatVersion = channel.getAttribute("version").getValue().trim();
        LOGGER.debug("Atom version " + formatVersion + " specified in document.");
    } else {
        LOGGER.info("No format version specified, using default.");
    }

    // --- read in channel information

    // Lower the case of these tags to simulate case-insensitive parsing
    ParserUtils.matchCaseOfChildren(channel, new String[] { "title", "description", "tagline", "ttl",
            "modified", "author", "generator", "copyright", "link", "entry" });

    // title element
    ChannelIF chnl = cBuilder.createChannel(channel, channel.getChildTextTrim("title", defNS));

    // TODO: support attributes: type, mode
    chnl.setFormat(ChannelFormat.ATOM_0_3);

    // language
    String language = channel.getAttributeValue("lang", Namespace.XML_NAMESPACE);

    if (language != null) {
        chnl.setLanguage(language);
    }

    // description element
    if (channel.getChild("description") != null) {
        chnl.setDescription(channel.getChildTextTrim("description", defNS));
    } else {
        // fallback
        chnl.setDescription(channel.getChildTextTrim("tagline", defNS));
    }

    // ttl in dc namespace
    Element ttl = channel.getChild("ttl", dcNS);

    if (ttl != null) {
        String ttlString = ttl.getTextTrim();

        if (ttlString != null) {
            chnl.setTtl(Integer.parseInt(ttlString));
        }
    }

    //  lastbuild element : modified ?
    Element modified = channel.getChild("modified", defNS);

    if (modified != null) {
        chnl.setPubDate(ParserUtils.getDate(modified.getTextTrim()));
    }

    // TODO : issued value
    /*
    if (modified != null) {
      modified = channel.getChild("issued", defNS);
      chnl.setLastBuildDate (ParserUtils.getDate(modified.getTextTrim()));
    }
    */

    // author element
    Element author = channel.getChild("author", defNS);

    if (author != null) {
        ParserUtils.matchCaseOfChildren(author, "name");
        chnl.setCreator(author.getChildTextTrim("name", defNS));
    }

    // generator element
    Element generator = channel.getChild("generator", defNS);

    if (generator != null) {
        chnl.setGenerator(generator.getTextTrim());
    }

    // copyright element
    Element copyright = channel.getChild("copyright", defNS);

    if (copyright != null) {
        chnl.setCopyright(getCopyright(copyright));
    }

    // n link elements
    // TODO : type attribut of link (text, application...)
    List links = channel.getChildren("link", defNS);
    Iterator i = links.iterator();

    while (i.hasNext()) {
        Element linkElement = (Element) i.next();

        // use first 'alternate' link
        String rel = linkElement.getAttributeValue("rel");
        String href = linkElement.getAttributeValue("href");

        if ((rel != null) && (href != null) && rel.equals("alternate")) {
            URL linkURL = ParserUtils.getURL(href);

            chnl.setSite(linkURL);

            break;
        }

        // TODO: further extraction of link information
    }

    // 1..n entry elements
    List items = channel.getChildren("entry", defNS);

    i = items.iterator();

    while (i.hasNext()) {
        Element item = (Element) i.next();

        // Lower the case of these tags to simulate case-insensitive parsing
        ParserUtils.matchCaseOfChildren(item,
                new String[] { "title", "link", "content", "summary", "issued", "subject" });

        // get title element
        // TODO : deal with type attribut
        Element elTitle = item.getChild("title", defNS);
        String strTitle = "<No Title>";

        if (elTitle != null) {
            strTitle = getTitle(elTitle);
            LOGGER.debug("Parsing title " + elTitle.getTextTrim() + "->" + strTitle);
        }

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Entry element found (" + strTitle + ").");
        }

        // get link element
        String strLink = AtomParserUtils.getItemLink(item, defNS);

        // get description element
        String strDesc = getDescription(item, defNS);

        // generate new news item (link to article)
        ItemIF curItem = cBuilder.createItem(item, chnl, strTitle, strDesc, ParserUtils.getURL(strLink));

        curItem.setFound(dateParsed);

        // get issued element (required)
        Element elIssued = item.getChild("issued", defNS);

        if (elIssued == null) {
            // [adewale@gmail.com, 01-May-2005] Fix for blogs which have
            // 'created' dates, but not 'issued' dates -- in clear contravention
            // of the Atom 0.3 spec.
            Element elCreated = item.getChild("created", defNS);

            if (elCreated != null) {
                curItem.setDate(ParserUtils.getDate(elCreated.getTextTrim()));
            }
        } else {
            curItem.setDate(ParserUtils.getDate(elIssued.getTextTrim()));
        }

        // get subject element
        Element elSubject = item.getChild("subject", dcNS);

        if (elSubject != null) {
            // TODO: Mulitple subject elements not handled currently
            curItem.setSubject(elSubject.getTextTrim());
        }
    }

    // set to current date
    chnl.setLastUpdated(dateParsed);

    return chnl;
}

From source file:ditatools.translate.DitaTranslator.java

License:Apache License

protected void translateTopicXML(Document doc, File outputXmlFile) throws JDOMException, IOException {
    // caller should have verified that root element 
    // is a DITA topic: concept, task, or reference
    Element rootElement = doc.getRootElement();

    // XXX handle case where xml:lang attr is not present
    rootElement.getAttribute("lang", Namespace.XML_NAMESPACE).setValue(language);

    process(rootElement);//from www .  j  a  v  a 2  s.c om

    XMLOutputter xmlOutput = new XMLOutputter();

    // force use of UTF-8 in output
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    try {
        fos = new FileOutputStream(outputXmlFile);
        osw = new OutputStreamWriter(fos, "UTF-8");

        xmlOutput.output(doc, osw);
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (Exception x) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception x) {
            }
        }
    }
}

From source file:Enrichissement.Jaccard.java

private static boolean comparerObjet(Element triplet1, Element triplet2) {
    Element uri1 = triplet1.getChild("uri");
    Element uri2 = triplet2.getChild("uri");
    Element literal1 = triplet1.getChild("literal");
    Element literal2 = triplet1.getChild("literal");
    if ((uri1 == null && uri2 != null) || (uri2 == null && uri1 != null)) {
        return false;
    } else if (uri1 != null && uri2 != null) {
        String uri1text = uri1.getText();
        String uri2text = uri2.getText();
        if (!(uri1text.equals(uri2text))) {
            return false;
        }//from ww w.ja  v  a2s .c  o  m
    } else if (literal1 != null && literal2 != null) {
        String lang1 = literal1.getAttributeValue("lang", Namespace.XML_NAMESPACE);
        String lang2 = literal2.getAttributeValue("lang", Namespace.XML_NAMESPACE);
        String datatype1 = literal1.getAttributeValue("datatype");
        String datatype2 = literal2.getAttributeValue("datatype");
        String literal1Text = literal1.getText();
        String literal2Text = literal2.getText();

        if (lang1 != null && lang2 != null) {

            if (!lang1.equals(lang2)) {
                return false;
            } else if (!literal1Text.equals(literal2Text)) {

                return false;
            }
        } else if (datatype1 != null && datatype2 != null) {
            if (!datatype1.equals(datatype2)) {
                return false;
            } else if (!literal1Text.equals(literal2Text)) {
                return false;
            }
        }

    }
    return true;
}