You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ja...@apache.org on 2014/01/08 16:25:56 UTC
[40/51] [abbrv] [partial] MARMOTTA-397: Reorganized and renamed
Marmotta Sesame Tools
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Atom10Parser.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Atom10Parser.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Atom10Parser.java
new file mode 100644
index 0000000..ccfc237
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Atom10Parser.java
@@ -0,0 +1,655 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.Namespace;
+import org.jdom2.output.XMLOutputter;
+
+import com.sun.syndication.feed.WireFeed;
+import com.sun.syndication.feed.atom.Category;
+import com.sun.syndication.feed.atom.Content;
+import com.sun.syndication.feed.atom.Entry;
+import com.sun.syndication.feed.atom.Feed;
+import com.sun.syndication.feed.atom.Generator;
+import com.sun.syndication.feed.atom.Link;
+import com.sun.syndication.feed.atom.Person;
+import com.sun.syndication.io.FeedException;
+import com.sun.syndication.io.WireFeedInput;
+import com.sun.syndication.io.WireFeedOutput;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.util.regex.Pattern;
+import org.jdom2.Attribute;
+import org.jdom2.JDOMException;
+import org.jdom2.Parent;
+import org.jdom2.input.SAXBuilder;
+
+/**
+ * Parser for Atom 1.0
+ * @author Dave Johnson
+ */
+public class Atom10Parser extends BaseWireFeedParser {
+ private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom";
+ private static final Namespace ATOM_10_NS = Namespace.getNamespace(ATOM_10_URI);
+
+ private static boolean resolveURIs = false;
+
+ public static void setResolveURIs(boolean resolveURIs) {
+ Atom10Parser.resolveURIs = resolveURIs;
+ }
+
+ public static boolean getResolveURIs() {
+ return resolveURIs;
+ }
+
+ public Atom10Parser() {
+ this("atom_1.0");
+ }
+
+ protected Atom10Parser(String type) {
+ super(type, ATOM_10_NS);
+ }
+
+ protected Namespace getAtomNamespace() {
+ return ATOM_10_NS;
+ }
+
+ public boolean isMyType(Document document) {
+ Element rssRoot = document.getRootElement();
+ Namespace defaultNS = rssRoot.getNamespace();
+ return (defaultNS!=null) && defaultNS.equals(getAtomNamespace());
+ }
+
+ public WireFeed parse(Document document, boolean validate)
+ throws IllegalArgumentException,FeedException {
+ if (validate) {
+ validateFeed(document);
+ }
+ Element rssRoot = document.getRootElement();
+ return parseFeed(rssRoot);
+ }
+
+ protected void validateFeed(Document document) throws FeedException {
+ // TBD
+ // here we have to validate the Feed against a schema or whatever
+ // not sure how to do it
+ // one posibility would be to produce an ouput and attempt to parse it again
+ // with validation turned on.
+ // otherwise will have to check the document elements by hand.
+ }
+
+ protected WireFeed parseFeed(Element eFeed) throws FeedException {
+
+ String baseURI = null;
+ 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;
+ }
+
+ private Feed parseFeedMetadata(String baseURI, Element eFeed) {
+ com.sun.syndication.feed.atom.Feed feed =
+ new com.sun.syndication.feed.atom.Feed(getType());
+
+ Element e = eFeed.getChild("title",getAtomNamespace());
+ if (e!=null) {
+ Content c = new Content();
+ c.setValue(parseTextConstructToString(e));
+ c.setType(getAttributeValue(e, "type"));
+ feed.setTitleEx(c);
+ }
+
+ List eList = eFeed.getChildren("link",getAtomNamespace());
+ feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, eList));
+ feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, eList));
+
+ List cList = eFeed.getChildren("category",getAtomNamespace());
+ feed.setCategories(parseCategories(baseURI, cList));
+
+ eList = eFeed.getChildren("author", getAtomNamespace());
+ if (eList.size()>0) {
+ feed.setAuthors(parsePersons(baseURI, eList));
+ }
+
+ eList = eFeed.getChildren("contributor",getAtomNamespace());
+ if (eList.size()>0) {
+ feed.setContributors(parsePersons(baseURI, eList));
+ }
+
+ e = eFeed.getChild("subtitle",getAtomNamespace());
+ if (e!=null) {
+ Content subtitle = new Content();
+ subtitle.setValue(parseTextConstructToString(e));
+ subtitle.setType(getAttributeValue(e, "type"));
+ feed.setSubtitle(subtitle);
+ }
+
+ e = eFeed.getChild("id",getAtomNamespace());
+ if (e!=null) {
+ feed.setId(e.getText());
+ }
+
+ e = eFeed.getChild("generator",getAtomNamespace());
+ if (e!=null) {
+ Generator gen = new Generator();
+ gen.setValue(e.getText());
+ String att = getAttributeValue(e, "uri");
+ if (att!=null) {
+ gen.setUrl(att);
+ }
+ att = getAttributeValue(e, "version");
+ if (att!=null) {
+ gen.setVersion(att);
+ }
+ feed.setGenerator(gen);
+ }
+
+ e = eFeed.getChild("rights",getAtomNamespace());
+ if (e!=null) {
+ feed.setRights(parseTextConstructToString(e));
+ }
+
+ e = eFeed.getChild("icon",getAtomNamespace());
+ if (e!=null) {
+ feed.setIcon(e.getText());
+ }
+
+ e = eFeed.getChild("logo",getAtomNamespace());
+ if (e!=null) {
+ feed.setLogo(e.getText());
+ }
+
+ e = eFeed.getChild("updated",getAtomNamespace());
+ if (e!=null) {
+ feed.setUpdated(DateParser.parseDate(e.getText()));
+ }
+
+ return feed;
+ }
+
+ private Link parseLink(Feed feed , Entry entry, String baseURI, Element eLink) {
+ Link link = new Link();
+ String att = getAttributeValue(eLink, "rel");
+ if (att!=null) {
+ link.setRel(att);
+ }
+ att = getAttributeValue(eLink, "type");
+ if (att!=null) {
+ link.setType(att);
+ }
+ att = getAttributeValue(eLink, "href");
+ if (att!=null) {
+ link.setHref(att);
+ if (isRelativeURI(att)) {
+ link.setHrefResolved(resolveURI(baseURI, eLink, att));
+ }
+ }
+ att = getAttributeValue(eLink, "title");
+ if (att!=null) {
+ link.setTitle(att);
+ }
+ att = getAttributeValue(eLink, "hreflang");
+ if (att!=null) {
+ link.setHreflang(att);
+ }
+ att = getAttributeValue(eLink, "length");
+ if (att!=null) {
+ Long val = NumberParser.parseLong(att);
+ if (val != null) {
+ link.setLength(val.longValue());
+ }
+ }
+ return link;
+ }
+
+ // List(Elements) -> List(Link)
+ private List parseAlternateLinks(Feed feed, Entry entry, String baseURI, List eLinks) {
+ List links = new ArrayList();
+ for (int i=0;i<eLinks.size();i++) {
+ Element eLink = (Element) eLinks.get(i);
+ Link link = parseLink(feed, entry, baseURI, eLink);
+ if (link.getRel() == null
+ || "".equals(link.getRel().trim())
+ || "alternate".equals(link.getRel())) {
+ links.add(link);
+ }
+ }
+ return (links.size()>0) ? links : null;
+ }
+
+ private List parseOtherLinks(Feed feed, Entry entry, String baseURI, List eLinks) {
+ List links = new ArrayList();
+ for (int i=0;i<eLinks.size();i++) {
+ Element eLink = (Element) eLinks.get(i);
+ Link link = parseLink(feed, entry, baseURI, eLink);
+ if (!"alternate".equals(link.getRel())) {
+ links.add(link);
+ }
+ }
+ return (links.size()>0) ? links : null;
+ }
+
+ private Person parsePerson(String baseURI, Element ePerson) {
+ Person person = new Person();
+ Element e = ePerson.getChild("name",getAtomNamespace());
+ if (e!=null) {
+ person.setName(e.getText());
+ }
+ e = ePerson.getChild("uri",getAtomNamespace());
+ if (e!=null) {
+ person.setUri(e.getText());
+ if (isRelativeURI(e.getText())) {
+ person.setUriResolved(resolveURI(baseURI, ePerson, e.getText()));
+ }
+ }
+ e = ePerson.getChild("email",getAtomNamespace());
+ if (e!=null) {
+ person.setEmail(e.getText());
+ }
+ person.setModules(parsePersonModules(ePerson));
+ return person;
+ }
+
+ // List(Elements) -> List(Persons)
+ private List parsePersons(String baseURI, List ePersons) {
+ List persons = new ArrayList();
+ for (int i=0;i<ePersons.size();i++) {
+ persons.add(parsePerson(baseURI, (Element)ePersons.get(i)));
+ }
+ return (persons.size()>0) ? persons : null;
+ }
+
+ private Content parseContent(Element e) {
+ String value = parseTextConstructToString(e);
+ String src = getAttributeValue(e, "src");
+ String type = getAttributeValue(e, "type");
+ Content content = new Content();
+ content.setSrc(src);
+ content.setType(type);
+ content.setValue(value);
+ return content;
+ }
+
+ private String parseTextConstructToString(Element e) {
+ String value = null;
+ String type = getAttributeValue(e, "type");
+ type = (type!=null) ? type : Content.TEXT;
+ if (type.equals(Content.XHTML) || (type.indexOf("/xml")) != -1 || (type.indexOf("+xml")) != -1) {
+ // XHTML content needs special handling
+ XMLOutputter outputter = new XMLOutputter();
+ List eContent = e.getContent();
+ Iterator i = eContent.iterator();
+ while (i.hasNext()) {
+ org.jdom2.Content c = (org.jdom2.Content) i.next();
+ if (c instanceof Element) {
+ Element eC = (Element) c;
+ if (eC.getNamespace().equals(getAtomNamespace())) {
+ ((Element)c).setNamespace(Namespace.NO_NAMESPACE);
+ }
+ }
+ }
+ value = outputter.outputString(eContent);
+ } else {
+ // Everything else comes in verbatim
+ value = e.getText();
+ }
+ return value;
+ }
+
+ // List(Elements) -> List(Entries)
+ protected List parseEntries(Feed feed, String baseURI, List eEntries) {
+ List entries = new ArrayList();
+ for (int i=0;i<eEntries.size();i++) {
+ entries.add(parseEntry(feed, (Element)eEntries.get(i), baseURI));
+ }
+ return (entries.size()>0) ? entries : null;
+ }
+
+ 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);
+ }
+
+ 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;
+ }
+
+ private List parseCategories(String baseURI, List eCategories) {
+ List cats = new ArrayList();
+ for (int i=0;i<eCategories.size();i++) {
+ Element eCategory = (Element) eCategories.get(i);
+ cats.add(parseCategory(baseURI, eCategory));
+ }
+ return (cats.size()>0) ? cats : null;
+ }
+
+ private Category parseCategory(String baseURI, Element eCategory) {
+ Category category = new Category();
+ String att = getAttributeValue(eCategory, "term");
+ if (att!=null) {
+ category.setTerm(att);
+ }
+ att = getAttributeValue(eCategory, "scheme");
+ if (att!=null) {
+ category.setScheme(att);
+ if (isRelativeURI(att)) {
+ category.setSchemeResolved(resolveURI(baseURI, eCategory, att));
+ }
+ }
+ att = getAttributeValue(eCategory, "label");
+ if (att!=null) {
+ category.setLabel(att);
+ }
+ return category;
+
+ }
+
+ // Once following relative URI methods are made public in the ROME
+ // Atom10Parser, then use them instead and delete these.
+
+
+ // Fix for issue #34 "valid IRI href attributes are stripped for atom:link"
+ // URI's that didn't start with http were being treated as relative URIs.
+ // So now consider an absolute URI to be any alpha-numeric string followed
+ // by a colon, followed by anything -- specified by this regex:
+ static Pattern absoluteURIPattern = Pattern.compile("^[a-z0-9]*:.*$");
+
+ public static boolean isAbsoluteURI(String uri) {
+ return absoluteURIPattern.matcher(uri).find();
+ }
+
+ /** Returns true if URI is relative. */
+ public static boolean isRelativeURI(String uri) {
+ return !isAbsoluteURI(uri);
+ }
+
+ /**
+ * 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
+ */
+ 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;
+ }
+
+ /**
+ * Find base URI of feed considering relative URIs.
+ * @param root Root element of feed.
+ */
+ private String findBaseURI(Element root) throws MalformedURLException {
+ String ret = null;
+ if (findAtomLink(root, "self") != null) {
+ ret = findAtomLink(root, "self");
+ if (".".equals(ret) || "./".equals(ret)) ret = "";
+ if (ret.indexOf("/") != -1) ret = ret.substring(0, ret.lastIndexOf("/"));
+ ret = resolveURI(null, root, ret);
+ }
+ return ret;
+ }
+
+ /**
+ * Return URL string of Atom link element under parent element.
+ * Link with no rel attribute is considered to be rel="alternate"
+ * @param parent Consider only children of this parent element
+ * @param rel Consider only links with this relationship
+ */
+ private String findAtomLink(Element parent, String rel) {
+ String ret = null;
+ List linksList = parent.getChildren("link", ATOM_10_NS);
+ if (linksList != null) {
+ for (Iterator links = linksList.iterator(); links.hasNext(); ) {
+ Element link = (Element)links.next();
+ Attribute relAtt = getAttribute(link, "rel");
+ Attribute hrefAtt = getAttribute(link, "href");
+ if ( (relAtt == null && "alternate".equals(rel))
+ || (relAtt != null && relAtt.getValue().equals(rel))) {
+ ret = hrefAtt.getValue();
+ break;
+ }
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * Form URI by combining base with append portion and giving
+ * special consideration to append portions that begin with ".."
+ * @param base Base of URI, may end with trailing slash
+ * @param append String to append, may begin with slash or ".."
+ */
+ private static String formURI(String base, String append) {
+ base = stripTrailingSlash(base);
+ append = stripStartingSlash(append);
+ if (append.startsWith("..")) {
+ String ret = null;
+ String[] parts = append.split("/");
+ for (int i=0; i<parts.length; i++) {
+ if ("..".equals(parts[i])) {
+ int last = base.lastIndexOf("/");
+ if (last != -1) {
+ base = base.substring(0, last);
+ append = append.substring(3, append.length());
+ }
+ else break;
+ }
+ }
+ }
+ return base + "/" + append;
+ }
+
+ /**
+ * Strip starting slash from beginning of string.
+ */
+ private static String stripStartingSlash(String s) {
+ if (s != null && s.startsWith("/")) {
+ s = s.substring(1, s.length());
+ }
+ return s;
+ }
+
+ /**
+ * Strip trailing slash from end of string.
+ */
+ private static String stripTrailingSlash(String s) {
+ if (s != null && s.endsWith("/")) {
+ s = s.substring(0, s.length() - 1);
+ }
+ return s;
+ }
+
+
+ /**
+ * Parse entry from reader.
+ */
+ 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);
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Base64.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Base64.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Base64.java
new file mode 100644
index 0000000..3c86ef7
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/Base64.java
@@ -0,0 +1,201 @@
+package com.sun.syndication.io.impl;
+
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * Encodes/decodes byte arrays and Strings into/from a base 64 String.
+ * <p>
+ * @author Alejandro Abdelnur
+ *
+ */
+public class Base64 {
+
+ /**
+ * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes.
+ * <p>
+ * @param s String to encode.
+ * @return encoded string.
+ *
+ */
+ public static String encode(String s) {
+ byte[] sBytes = s.getBytes();
+ sBytes = encode(sBytes);
+ s = new String(sBytes);
+ return s;
+ }
+
+ /**
+ * Decodes a base 64 String into a String.
+ * <p>
+ * @param s String to decode.
+ * @return encoded string.
+ * @throws java.lang.IllegalArgumentException thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding.
+ *
+ */
+ public static String decode(String s) throws IllegalArgumentException {
+ s = s.replaceAll("\n", "");
+ s = s.replaceAll("\r", "");
+ byte[] sBytes = s.getBytes();
+ sBytes = decode(sBytes);
+ s = new String(sBytes);
+ return s;
+ }
+
+
+ private static final byte[] ALPHASET =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".getBytes();
+
+ private static final int I6O2 = 255 - 3;
+ private static final int O6I2 = 3;
+ private static final int I4O4 = 255 - 15;
+ private static final int O4I4 = 15;
+ private static final int I2O6 = 255 - 63;
+ private static final int O2I6 = 63;
+
+ /**
+ * Encodes a byte array into a base 64 byte array.
+ * <p>
+ * @param dData byte array to encode.
+ * @return encoded byte array.
+ *
+ */
+ public static byte[] encode(byte[] dData) {
+ if (dData==null) {
+ throw new IllegalArgumentException("Cannot encode null");
+ }
+ byte[] eData = new byte[((dData.length+2)/3)*4];
+
+ int eIndex = 0;
+ for (int i = 0; i<dData.length; i += 3) {
+ int d1;
+ int d2=0;
+ int d3=0;
+ int e1;
+ int e2;
+ int e3;
+ int e4;
+ int pad=0;
+
+ d1 = dData[i];
+ if ((i+1)<dData.length) {
+ d2 = dData[i+1];
+ if ((i+2)<dData.length) {
+ d3 = dData[i+2];
+ }
+ else {
+ pad =1;
+ }
+ }
+ else {
+ pad =2;
+ }
+
+ e1 = ALPHASET[(d1&I6O2)>>2];
+ e2 = ALPHASET[(d1&O6I2)<<4 | (d2&I4O4)>>4];
+ e3 = ALPHASET[(d2&O4I4)<<2 | (d3&I2O6)>>6];
+ e4 = ALPHASET[(d3&O2I6)];
+
+ eData[eIndex++] = (byte)e1;
+ eData[eIndex++] = (byte)e2;
+ eData[eIndex++] = (pad<2) ?(byte)e3 : (byte)'=';
+ eData[eIndex++] = (pad<1) ?(byte)e4 : (byte)'=';
+
+ }
+ return eData;
+ }
+
+ private final static int[] CODES = new int[256];
+
+ static {
+ for (int i=0;i<CODES.length;i++) {
+ CODES[i] = 64;
+ }
+ for (int i=0;i<ALPHASET.length;i++) {
+ CODES[ALPHASET[i]] = i;
+ }
+ }
+
+ /**
+ * Dencodes a com.sun.syndication.io.impl.Base64 byte array.
+ * <p>
+ * @param eData byte array to decode.
+ * @return decoded byte array.
+ * @throws java.lang.IllegalArgumentException thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding.
+ *
+ */
+ public static byte[] decode(byte[] eData) {
+ if (eData==null) {
+ throw new IllegalArgumentException("Cannot decode null");
+ }
+ byte[] cleanEData = (byte[]) eData.clone();
+ int cleanELength = 0;
+ for (int i=0;i<eData.length;i++) {
+ if (eData[i]<256 && CODES[eData[i]]<64) {
+ cleanEData[cleanELength++] = eData[i];
+ }
+ }
+
+ int dLength = (cleanELength/4)*3;
+ switch (cleanELength%4) {
+ case 3:
+ dLength += 2;
+ break;
+ case 2:
+ dLength++;
+ break;
+ }
+
+ byte[] dData = new byte[dLength];
+ int dIndex = 0;
+ for (int i = 0; i < eData.length; i += 4) {
+ if ((i + 3) > eData.length) {
+ throw new IllegalArgumentException("byte array is not a valid com.sun.syndication.io.impl.Base64 encoding");
+ }
+ int e1 = CODES[cleanEData[i]];
+ int e2 = CODES[cleanEData[i+1]];
+ int e3 = CODES[cleanEData[i+2]];
+ int e4 = CODES[cleanEData[i+3]];
+ dData[dIndex++] = (byte) ((e1<<2)|(e2>>4));
+ if (dIndex<dData.length) {
+ dData[dIndex++] = (byte) ((e2<<4) | (e3>>2));
+ }
+ if (dIndex<dData.length) {
+ dData[dIndex++] = (byte) ((e3<<6) | (e4));
+ }
+ }
+ return dData;
+ }
+
+ public static void main(String[] args) throws Exception {
+ String s =
+ "\nPGRpdiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94aHRtbCI+V2UncmUgcHJvcG9zaW5nIDxhIGhy\n"+
+"ZWY9Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS9jb3Jwb3JhdGUvc29mdHdhcmVfcHJpbmNpcGxlcy5odG1sIj5z\n"+
+"b21lIGd1aWRlbGluZXMgPC9hPnRvIGhlbHAgY3VyYiB0aGUgcHJvYmxlbSBvZiBJbnRlcm5ldCBzb2Z0d2Fy\n"+
+"ZSB0aGF0IGluc3RhbGxzIGl0c2VsZiB3aXRob3V0IHRlbGxpbmcgeW91LCBvciBiZWhhdmVzIGJhZGx5IG9u\n"+
+"Y2UgaXQgZ2V0cyBvbiB5b3VyIGNvbXB1dGVyLiBXZSd2ZSBiZWVuIGhlYXJpbmcgYSBsb3Qgb2YgY29tcGxh\n"+
+"aW50cyBhYm91dCB0aGlzIGxhdGVseSBhbmQgaXQgc2VlbXMgdG8gYmUgZ2V0dGluZyB3b3JzZS4gV2UgdGhp\n"+
+"bmsgaXQncyBpbXBvcnRhbnQgdGhhdCB5b3UgcmV0YWluIGNvbnRyb2wgb2YgeW91ciBjb21wdXRlciBhbmQg\n"+
+"dGhhdCB0aGVyZSBiZSBzb21lIGNsZWFyIHN0YW5kYXJkcyBpbiBvdXIgaW5kdXN0cnkuIExldCB1cyBrbm93\n"+
+"IGlmIHlvdSB0aGluayB0aGVzZSBndWlkZWxpbmVzIGFyZSB1c2VmdWwgb3IgaWYgeW91IGhhdmUgc3VnZ2Vz\n"+
+"dGlvbnMgdG8gaW1wcm92ZSB0aGVtLgo8YnIgLz4KPGJyIC8+Sm9uYXRoYW4gUm9zZW5iZXJnCjxiciAvPgo8\n"+
+"L2Rpdj4K\n";
+
+ System.out.println(decode(s));
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java
new file mode 100644
index 0000000..72a2b70
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java
@@ -0,0 +1,136 @@
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.io.WireFeedGenerator;
+import org.jdom2.Element;
+import org.jdom2.Namespace;
+import org.jdom2.Parent;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author Alejandro Abdelnur
+ */
+public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
+
+ /**
+ * [TYPE].feed.ModuleParser.classes= [className] ...
+ */
+ private static final String FEED_MODULE_GENERATORS_POSFIX_KEY = ".feed.ModuleGenerator.classes";
+
+ /**
+ * [TYPE].item.ModuleParser.classes= [className] ...
+ */
+ private static final String ITEM_MODULE_GENERATORS_POSFIX_KEY = ".item.ModuleGenerator.classes";
+
+ /**
+ * [TYPE].person.ModuleParser.classes= [className] ...
+ */
+ private static final String PERSON_MODULE_GENERATORS_POSFIX_KEY = ".person.ModuleGenerator.classes";
+
+
+ private String _type;
+ private ModuleGenerators _feedModuleGenerators;
+ private ModuleGenerators _itemModuleGenerators;
+ private ModuleGenerators _personModuleGenerators;
+ private Namespace[] _allModuleNamespaces;
+
+ protected BaseWireFeedGenerator(String type) {
+ _type = type;
+ _feedModuleGenerators = new ModuleGenerators(type + FEED_MODULE_GENERATORS_POSFIX_KEY, this);
+ _itemModuleGenerators = new ModuleGenerators(type + ITEM_MODULE_GENERATORS_POSFIX_KEY, this);
+ _personModuleGenerators = new ModuleGenerators(type + PERSON_MODULE_GENERATORS_POSFIX_KEY, this);
+ Set allModuleNamespaces = new HashSet();
+ Iterator i = _feedModuleGenerators.getAllNamespaces().iterator();
+ while (i.hasNext()) {
+ allModuleNamespaces.add(i.next());
+ }
+ i = _itemModuleGenerators.getAllNamespaces().iterator();
+ while (i.hasNext()) {
+ allModuleNamespaces.add(i.next());
+ }
+ i = _personModuleGenerators.getAllNamespaces().iterator();
+ while (i.hasNext()) {
+ allModuleNamespaces.add(i.next());
+ }
+ _allModuleNamespaces = new Namespace[allModuleNamespaces.size()];
+ allModuleNamespaces.toArray(_allModuleNamespaces);
+ }
+
+ public String getType() {
+ return _type;
+ }
+
+ protected void generateModuleNamespaceDefs(Element root) {
+ for (int i = 0; i < _allModuleNamespaces.length; i++) {
+ root.addNamespaceDeclaration(_allModuleNamespaces[i]);
+ }
+ }
+
+ protected void generateFeedModules(List modules, Element feed) {
+ _feedModuleGenerators.generateModules(modules, feed);
+ }
+
+ public void generateItemModules(List modules, Element item) {
+ _itemModuleGenerators.generateModules(modules, item);
+ }
+
+ public void generatePersonModules(List modules, Element person) {
+ _personModuleGenerators.generateModules(modules, person);
+ }
+
+ protected void generateForeignMarkup(Element e, List foreignMarkup) {
+ if (foreignMarkup != null) {
+ Iterator elems = (Iterator) foreignMarkup.iterator();
+ while (elems.hasNext()) {
+ Element elem = (Element) elems.next();
+ Parent parent = elem.getParent();
+ if (parent != null) {
+ parent.removeContent(elem);
+ }
+ e.addContent(elem);
+ }
+ }
+ }
+
+ /**
+ * Purging unused declarations is less optimal, performance-wise, than never adding them in the first place. So, we
+ * should still ask the ROME guys to fix their code (not adding dozens of unnecessary module declarations). Having
+ * said that: purging them here, before XML generation, is more efficient than parsing and re-molding the XML after
+ * ROME generates it.
+ * <p/>
+ * Note that the calling app could still add declarations/modules to the Feed tree after this. Which is fine. But
+ * those modules are then responsible for crawling to the root of the tree, at generate() time, to make sure their
+ * namespace declarations are present.
+ */
+ protected static void purgeUnusedNamespaceDeclarations(Element root) {
+ java.util.Set usedPrefixes = new java.util.HashSet();
+ collectUsedPrefixes(root, usedPrefixes);
+
+ List list = root.getAdditionalNamespaces();
+ List additionalNamespaces = new java.util.ArrayList();
+ additionalNamespaces.addAll(list); // the duplication will prevent a ConcurrentModificationException below
+
+ for (int i = 0; i < additionalNamespaces.size(); i++) {
+ Namespace ns = (Namespace) additionalNamespaces.get(i);
+ String prefix = ns.getPrefix();
+ if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) {
+ root.removeNamespaceDeclaration(ns);
+ }
+ }
+ }
+
+ private static void collectUsedPrefixes(Element el, java.util.Set collector) {
+ String prefix = el.getNamespacePrefix();
+ if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) {
+ collector.add(prefix);
+ }
+ List kids = el.getChildren();
+ for (int i = 0; i < kids.size(); i++) {
+ collectUsedPrefixes((Element) kids.get(i), collector); // recursion - worth it
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedParser.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedParser.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedParser.java
new file mode 100644
index 0000000..ba48cef
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/BaseWireFeedParser.java
@@ -0,0 +1,114 @@
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.feed.WireFeed;
+import com.sun.syndication.feed.module.Extendable;
+import com.sun.syndication.io.WireFeedParser;
+import java.util.ArrayList;
+import java.util.Iterator;
+import org.jdom2.Element;
+
+import java.util.List;
+import org.jdom2.Namespace;
+import org.jdom2.Attribute;
+
+/**
+ * @author Alejandro Abdelnur
+ */
+public abstract class BaseWireFeedParser implements WireFeedParser {
+ /**
+ * [TYPE].feed.ModuleParser.classes= [className] ...
+ *
+ */
+ private static final String FEED_MODULE_PARSERS_POSFIX_KEY = ".feed.ModuleParser.classes";
+
+ /**
+ * [TYPE].item.ModuleParser.classes= [className] ...
+ *
+ */
+ private static final String ITEM_MODULE_PARSERS_POSFIX_KEY = ".item.ModuleParser.classes";
+
+ /**
+ * [TYPE].person.ModuleParser.classes= [className] ...
+ *
+ */
+ private static final String PERSON_MODULE_PARSERS_POSFIX_KEY = ".person.ModuleParser.classes";
+
+
+ private String _type;
+ private ModuleParsers _feedModuleParsers;
+ private ModuleParsers _itemModuleParsers;
+ private ModuleParsers _personModuleParsers;
+ private Namespace _namespace;
+
+ protected BaseWireFeedParser(String type, Namespace namespace) {
+ _type = type;
+ _namespace = namespace;
+ _feedModuleParsers = new ModuleParsers(type+FEED_MODULE_PARSERS_POSFIX_KEY, this);
+ _itemModuleParsers = new ModuleParsers(type+ITEM_MODULE_PARSERS_POSFIX_KEY, this);
+ _personModuleParsers = new ModuleParsers(type+PERSON_MODULE_PARSERS_POSFIX_KEY, this);
+ }
+
+ /**
+ * Returns the type of feed the parser handles.
+ * <p>
+ * @see WireFeed for details on the format of this string.
+ * <p>
+ * @return the type of feed the parser handles.
+ *
+ */
+ public String getType() {
+ return _type;
+ }
+
+ protected List parseFeedModules(Element feedElement) {
+ return _feedModuleParsers.parseModules(feedElement);
+ }
+
+ protected List parseItemModules(Element itemElement) {
+ return _itemModuleParsers.parseModules(itemElement);
+ }
+
+ protected List parsePersonModules(Element itemElement) {
+ return _personModuleParsers.parseModules(itemElement);
+ }
+
+ protected List extractForeignMarkup(Element e, Extendable ext, Namespace basens) {
+ ArrayList foreignMarkup = new ArrayList();
+ Iterator children = e.getChildren().iterator();
+ while (children.hasNext()) {
+ Element elem = (Element)children.next();
+ if (
+ // if elemet not in the RSS namespace
+ !basens.equals(elem.getNamespace())
+ // and elem was not handled by a module
+ && null == ext.getModule(elem.getNamespaceURI())) {
+
+ // save it as foreign markup,
+ // but we can't detach it while we're iterating
+ foreignMarkup.add(elem.clone());
+ }
+ }
+ // Now we can detach the foreign markup elements
+ Iterator fm = foreignMarkup.iterator();
+ while (fm.hasNext()) {
+ Element elem = (Element)fm.next();
+ elem.detach();
+ }
+ return foreignMarkup;
+ }
+
+ protected Attribute getAttribute(Element e, String attributeName) {
+ Attribute attribute = e.getAttribute(attributeName);
+ if (attribute == null) {
+ attribute = e.getAttribute(attributeName, _namespace);
+ }
+ return attribute;
+ }
+
+ protected String getAttributeValue(Element e, String attributeName) {
+ Attribute attr = getAttribute(e, attributeName);
+ return (attr != null) ? attr.getValue() : null;
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleGenerator.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleGenerator.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleGenerator.java
new file mode 100644
index 0000000..a7cdee3
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleGenerator.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.feed.module.Module;
+import com.sun.syndication.feed.module.DCModule;
+import com.sun.syndication.feed.module.DCSubject;
+import com.sun.syndication.io.ModuleGenerator;
+import org.jdom2.Attribute;
+import org.jdom2.Element;
+import org.jdom2.Namespace;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
+
+
+/**
+ * Feed Generator for DublinCore Module.
+ * <p/>
+ *
+ * @author Elaine Chien
+ *
+ */
+public class DCModuleGenerator implements ModuleGenerator {
+
+ private static final String DC_URI = "http://purl.org/dc/elements/1.1/";
+ private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
+ private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+
+ private static final Namespace DC_NS = Namespace.getNamespace("dc", DC_URI);
+ private static final Namespace TAXO_NS = Namespace.getNamespace("taxo", TAXO_URI);
+ private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
+
+ private static final Set NAMESPACES;
+
+ static {
+ Set nss = new HashSet();
+ nss.add(DC_NS);
+ nss.add(TAXO_NS);
+ nss.add(RDF_NS);
+ NAMESPACES = Collections.unmodifiableSet(nss);
+ }
+
+ public final String getNamespaceUri() {
+ return DC_URI;
+ }
+
+ private final Namespace getDCNamespace() {
+ return DC_NS;
+ }
+
+ private final Namespace getRDFNamespace() {
+ return RDF_NS;
+ }
+
+ private final Namespace getTaxonomyNamespace() {
+ return TAXO_NS;
+ }
+
+ /**
+ * Returns a set with all the URIs (JDOM Namespace elements) this module
+ * generator uses.
+ * <p/>
+ * It is used by the the feed generators to add their namespace definition
+ * in the root element of the generated document (forward-missing of
+ * Java 5.0 Generics).
+ * <p/>
+ *
+ * @return a set with all the URIs this module generator uses.
+ */
+ public final Set getNamespaces() {
+ return NAMESPACES;
+ }
+
+ /**
+ * Populate an element tree with elements for a module.
+ * <p>
+ * @param module the module to populate from.
+ * @param element the root element to attach child elements to.
+ */
+ public final void generate(Module module, Element element) {
+ DCModule dcModule = (DCModule) module;
+
+ if (dcModule.getTitle() != null) {
+ element.addContent(generateSimpleElementList("title", dcModule.getTitles()));
+ }
+ if (dcModule.getCreator() != null) {
+ element.addContent(generateSimpleElementList("creator", dcModule.getCreators()));
+ }
+ List subjects = dcModule.getSubjects();
+ for (int i = 0; i < subjects.size(); i++) {
+ element.addContent(generateSubjectElement((DCSubject) subjects.get(i)));
+ }
+ if (dcModule.getDescription() != null) {
+ element.addContent(generateSimpleElementList("description", dcModule.getDescriptions()));
+ }
+ if (dcModule.getPublisher() != null) {
+ element.addContent(generateSimpleElementList("publisher", dcModule.getPublishers()));
+ }
+ if (dcModule.getContributors() != null) {
+ element.addContent(generateSimpleElementList("contributor", dcModule.getContributors()));
+ }
+ if (dcModule.getDate() != null) {
+ for (Iterator i = dcModule.getDates().iterator(); i.hasNext();) {
+ element.addContent(generateSimpleElement("date",
+ DateParser.formatW3CDateTime((Date) i.next())));
+ }
+ }
+ if (dcModule.getType() != null) {
+ element.addContent(generateSimpleElementList("type", dcModule.getTypes()));
+ }
+ if (dcModule.getFormat() != null) {
+ element.addContent(generateSimpleElementList("format", dcModule.getFormats()));
+ }
+ if (dcModule.getIdentifier() != null) {
+ element.addContent(generateSimpleElementList("identifier", dcModule.getIdentifiers()));
+ }
+ if (dcModule.getSource() != null) {
+ element.addContent(generateSimpleElementList("source", dcModule.getSources()));
+ }
+ if (dcModule.getLanguage() != null) {
+ element.addContent(generateSimpleElementList("language", dcModule.getLanguages()));
+ }
+ if (dcModule.getRelation() != null) {
+ element.addContent(generateSimpleElementList("relation", dcModule.getRelations()));
+ }
+ if (dcModule.getCoverage() != null) {
+ element.addContent(generateSimpleElementList("coverage", dcModule.getCoverages()));
+ }
+ if (dcModule.getRights() != null) {
+ element.addContent(generateSimpleElementList("rights", dcModule.getRightsList()));
+ }
+ }
+
+ /**
+ * Utility method to generate an element for a subject.
+ * <p>
+ * @param subject the subject to generate an element for.
+ * @return the element for the subject.
+ */
+ protected final Element generateSubjectElement(DCSubject subject) {
+ Element subjectElement = new Element("subject", getDCNamespace());
+
+ if (subject.getTaxonomyUri() != null) {
+ Element descriptionElement = new Element("Description", getRDFNamespace());
+ Element topicElement = new Element("topic", getTaxonomyNamespace());
+ Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), getRDFNamespace());
+ topicElement.setAttribute(resourceAttribute);
+ descriptionElement.addContent(topicElement);
+
+ if (subject.getValue() != null) {
+ Element valueElement = new Element("value", getRDFNamespace());
+ valueElement.addContent(subject.getValue());
+ descriptionElement.addContent(valueElement);
+ }
+ subjectElement.addContent(descriptionElement);
+ } else {
+ subjectElement.addContent(subject.getValue());
+ }
+ return subjectElement;
+ }
+
+
+ /**
+ * Utility method to generate a single element containing a string.
+ * <p>
+ * @param name the name of the elment to generate.
+ * @param value the value of the text in the element.
+ * @return the element generated.
+ */
+ protected final Element generateSimpleElement(String name, String value) {
+ Element element = new Element(name, getDCNamespace());
+ element.addContent(value);
+
+ return element;
+ }
+
+ /**
+ * Utility method to generate a list of simple elements.
+ * <p>
+ * @param name the name of the element list to generate.
+ * @param value the list of values for the elements.
+ * @return a list of Elements created.
+ */
+ protected final List generateSimpleElementList(String name, List value) {
+ List elements = new ArrayList();
+ for (Iterator i = value.iterator(); i.hasNext();) {
+ elements.add(generateSimpleElement(name, (String) i.next()));
+ }
+
+ return elements;
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleParser.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleParser.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleParser.java
new file mode 100644
index 0000000..10173d5
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DCModuleParser.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.feed.module.DCModuleImpl;
+import com.sun.syndication.feed.module.DCSubjectImpl;
+import com.sun.syndication.feed.module.Module;
+import com.sun.syndication.feed.module.DCModule;
+import com.sun.syndication.feed.module.DCSubject;
+import com.sun.syndication.io.ModuleParser;
+import com.sun.syndication.io.WireFeedParser;
+import org.jdom2.Attribute;
+import org.jdom2.Element;
+import org.jdom2.Namespace;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Parser for the Dublin Core module.
+ */
+public class DCModuleParser implements ModuleParser {
+
+ private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+ private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
+
+ private static final Namespace DC_NS = Namespace.getNamespace(DCModule.URI);
+ private static final Namespace RDF_NS = Namespace.getNamespace(RDF_URI);
+ private static final Namespace TAXO_NS = Namespace.getNamespace(TAXO_URI);
+
+ public final String getNamespaceUri() {
+ return DCModule.URI;
+ }
+
+ private final Namespace getDCNamespace() {
+ return DC_NS;
+ }
+
+ private final Namespace getRDFNamespace() {
+ return RDF_NS;
+ }
+
+ private final Namespace getTaxonomyNamespace() {
+ return TAXO_NS;
+ }
+
+ /**
+ * Parse an element tree and return the module found in it.
+ * <p>
+ * @param dcRoot the root element containing the module elements.
+ * @return the module parsed from the element tree, <i>null</i> if none.
+ */
+ public Module parse(Element dcRoot) {
+ boolean foundSomething = false;
+ DCModule dcm = new DCModuleImpl();
+
+ List eList = dcRoot.getChildren("title", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setTitles(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("creator", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setCreators(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("subject", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setSubjects(parseSubjects(eList));
+ }
+ eList = dcRoot.getChildren("description", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setDescriptions(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("publisher", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setPublishers(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("contributor", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setContributors(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("date", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setDates(parseElementListDate(eList));
+ }
+ eList = dcRoot.getChildren("type", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setTypes(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("format", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setFormats(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("identifier", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setIdentifiers(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("source", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setSources(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("language", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setLanguages(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("relation", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setRelations(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("coverage", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setCoverages(parseElementList(eList));
+ }
+ eList = dcRoot.getChildren("rights", getDCNamespace());
+ if (eList.size() > 0) {
+ foundSomething = true;
+ dcm.setRightsList(parseElementList(eList));
+ }
+
+ return (foundSomething) ? dcm : null;
+ }
+
+ /**
+ * Utility method to parse a taxonomy from an element.
+ * <p>
+ * @param desc the taxonomy description element.
+ * @return the string contained in the resource of the element.
+ */
+ protected final String getTaxonomy(Element desc) {
+ String d = null;
+ Element taxo = desc.getChild("topic", getTaxonomyNamespace());
+ if (taxo!=null) {
+ Attribute a = taxo.getAttribute("resource", getRDFNamespace());
+ if (a!=null) {
+ d = a.getValue();
+ }
+ }
+ return d;
+ }
+
+ /**
+ * Utility method to parse a list of subjects out of a list of elements.
+ * <p>
+ * @param eList the element list to parse.
+ * @return a list of subjects parsed from the elements.
+ */
+ protected final List parseSubjects(List eList) {
+ List subjects = new ArrayList();
+ for (Iterator i = eList.iterator(); i.hasNext();) {
+ Element eSubject = (Element) i.next();
+ Element eDesc = eSubject.getChild("Description", getRDFNamespace());
+ if (eDesc != null) {
+ String taxonomy = getTaxonomy(eDesc);
+ List eValues = eDesc.getChildren("value", getRDFNamespace());
+ for (Iterator v = eValues.iterator(); v.hasNext();) {
+ Element eValue = (Element) v.next();
+ DCSubject subject = new DCSubjectImpl();
+ subject.setTaxonomyUri(taxonomy);
+ subject.setValue(eValue.getText());
+ subjects.add(subject);
+ }
+ } else {
+ DCSubject subject = new DCSubjectImpl();
+ subject.setValue(eSubject.getText());
+ subjects.add(subject);
+ }
+ }
+
+ return subjects;
+ }
+
+ /**
+ * Utility method to parse a list of strings out of a list of elements.
+ * <p>
+ * @param eList the list of elements to parse.
+ * @return the list of strings
+ */
+ protected final List parseElementList(List eList) {
+ List values= new ArrayList();
+ for (Iterator i = eList.iterator(); i.hasNext();) {
+ Element e = (Element) i.next();
+ values.add(e.getText());
+ }
+
+ return values;
+ }
+
+ /**
+ * Utility method to parse a list of dates out of a list of elements.
+ * <p>
+ * @param eList the list of elements to parse.
+ * @return the list of dates.
+ */
+ protected final List parseElementListDate(List eList) {
+ List values = new ArrayList();
+ for (Iterator i = eList.iterator(); i.hasNext();) {
+ Element e = (Element) i.next();
+ values.add(DateParser.parseDate(e.getText()));
+ }
+
+ return values;
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DateParser.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DateParser.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DateParser.java
new file mode 100644
index 0000000..a55e5e7
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/DateParser.java
@@ -0,0 +1,285 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.text.ParsePosition;
+import java.util.Date;
+import java.util.TimeZone;
+import java.util.Locale;
+
+/**
+ * A helper class that parses Dates out of Strings with date time in RFC822 and W3CDateTime
+ * formats plus the variants Atom (0.3) and RSS (0.9, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0)
+ * specificators added to those formats.
+ * <p/>
+ * It uses the JDK java.text.SimpleDateFormat class attemtping the parse using a mask for
+ * each one of the possible formats.
+ * <p/>
+ *
+ * @author Alejandro Abdelnur
+ *
+ */
+public class DateParser {
+
+ private static String[] ADDITIONAL_MASKS;
+
+ static {
+ ADDITIONAL_MASKS = PropertiesLoader.getPropertiesLoader().getTokenizedProperty("datetime.extra.masks","|");
+ }
+
+ // order is like this because the SimpleDateFormat.parse does not fail with exception
+ // if it can parse a valid date out of a substring of the full string given the mask
+ // so we have to check the most complete format first, then it fails with exception
+ private static final String[] RFC822_MASKS = {
+ "EEE, dd MMM yy HH:mm:ss z",
+ "EEE, dd MMM yy HH:mm z",
+ "dd MMM yy HH:mm:ss z",
+ "dd MMM yy HH:mm z"
+ };
+
+
+
+ // order is like this because the SimpleDateFormat.parse does not fail with exception
+ // if it can parse a valid date out of a substring of the full string given the mask
+ // so we have to check the most complete format first, then it fails with exception
+ private static final String[] W3CDATETIME_MASKS = {
+ "yyyy-MM-dd'T'HH:mm:ss.SSSz",
+ "yyyy-MM-dd't'HH:mm:ss.SSSz",
+ "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
+ "yyyy-MM-dd't'HH:mm:ss.SSS'z'",
+ "yyyy-MM-dd'T'HH:mm:ssz",
+ "yyyy-MM-dd't'HH:mm:ssz",
+ "yyyy-MM-dd'T'HH:mm:ssZ",
+ "yyyy-MM-dd't'HH:mm:ssZ",
+ "yyyy-MM-dd'T'HH:mm:ss'Z'",
+ "yyyy-MM-dd't'HH:mm:ss'z'",
+ "yyyy-MM-dd'T'HH:mmz", // together with logic in the parseW3CDateTime they
+ "yyyy-MM'T'HH:mmz", // handle W3C dates without time forcing them to be GMT
+ "yyyy'T'HH:mmz",
+ "yyyy-MM-dd't'HH:mmz",
+ "yyyy-MM-dd'T'HH:mm'Z'",
+ "yyyy-MM-dd't'HH:mm'z'",
+ "yyyy-MM-dd",
+ "yyyy-MM",
+ "yyyy"
+ };
+
+
+
+ /**
+ * The masks used to validate and parse the input to this Atom date.
+ * These are a lot more forgiving than what the Atom spec allows.
+ * The forms that are invalid according to the spec are indicated.
+ */
+ private static final String[] masks = {
+ "yyyy-MM-dd'T'HH:mm:ss.SSSz",
+ "yyyy-MM-dd't'HH:mm:ss.SSSz", // invalid
+ "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
+ "yyyy-MM-dd't'HH:mm:ss.SSS'z'", // invalid
+ "yyyy-MM-dd'T'HH:mm:ssz",
+ "yyyy-MM-dd't'HH:mm:ssz", // invalid
+ "yyyy-MM-dd'T'HH:mm:ss'Z'",
+ "yyyy-MM-dd't'HH:mm:ss'z'", // invalid
+ "yyyy-MM-dd'T'HH:mmz", // invalid
+ "yyyy-MM-dd't'HH:mmz", // invalid
+ "yyyy-MM-dd'T'HH:mm'Z'", // invalid
+ "yyyy-MM-dd't'HH:mm'z'", // invalid
+ "yyyy-MM-dd",
+ "yyyy-MM",
+ "yyyy"
+ };
+
+
+
+
+ /**
+ * Private constructor to avoid DateParser instances creation.
+ */
+ private DateParser() {
+ }
+
+ /**
+ * Parses a Date out of a string using an array of masks.
+ * <p/>
+ * It uses the masks in order until one of them succedes or all fail.
+ * <p/>
+ *
+ * @param masks array of masks to use for parsing the string
+ * @param sDate string to parse for a date.
+ * @return the Date represented by the given string using one of the given masks.
+ * It returns <b>null</b> if it was not possible to parse the the string with any of the masks.
+ *
+ */
+ private static Date parseUsingMask(String[] masks,String sDate) {
+ sDate = (sDate!=null) ? sDate.trim() : null;
+ ParsePosition pp = null;
+ Date d = null;
+ for (int i=0;d==null && i<masks.length;i++) {
+ DateFormat df = new SimpleDateFormat(masks[i],Locale.US);
+ //df.setLenient(false);
+ df.setLenient(true);
+ try {
+ pp = new ParsePosition(0);
+ d = df.parse(sDate,pp);
+ if (pp.getIndex()!=sDate.length()) {
+ d = null;
+ }
+ //System.out.println("pp["+pp.getIndex()+"] s["+sDate+" m["+masks[i]+"] d["+d+"]");
+ }
+ catch (Exception ex1) {
+ //System.out.println("s: "+sDate+" m: "+masks[i]+" d: "+null);
+ }
+ }
+ return d;
+ }
+
+ /**
+ * Parses a Date out of a String with a date in RFC822 format.
+ * <p/>
+ * It parsers the following formats:
+ * <ul>
+ * <li>"EEE, dd MMM yyyy HH:mm:ss z"</li>
+ * <li>"EEE, dd MMM yyyy HH:mm z"</li>
+ * <li>"EEE, dd MMM yy HH:mm:ss z"</li>
+ * <li>"EEE, dd MMM yy HH:mm z"</li>
+ * <li>"dd MMM yyyy HH:mm:ss z"</li>
+ * <li>"dd MMM yyyy HH:mm z"</li>
+ * <li>"dd MMM yy HH:mm:ss z"</li>
+ * <li>"dd MMM yy HH:mm z"</li>
+ * </ul>
+ * <p/>
+ * Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
+ * <p/>
+ * @param sDate string to parse for a date.
+ * @return the Date represented by the given RFC822 string.
+ * It returns <b>null</b> if it was not possible to parse the given string into a Date.
+ *
+ */
+ public static Date parseRFC822(String sDate) {
+ int utIndex = sDate.indexOf(" UT");
+ if (utIndex>-1) {
+ String pre = sDate.substring(0,utIndex);
+ String post = sDate.substring(utIndex+3);
+ sDate = pre + " GMT" + post;
+ }
+ return parseUsingMask(RFC822_MASKS,sDate);
+ }
+
+
+ /**
+ * Parses a Date out of a String with a date in W3C date-time format.
+ * <p/>
+ * It parsers the following formats:
+ * <ul>
+ * <li>"yyyy-MM-dd'T'HH:mm:ssz"</li>
+ * <li>"yyyy-MM-dd'T'HH:mmz"</li>
+ * <li>"yyyy-MM-dd"</li>
+ * <li>"yyyy-MM"</li>
+ * <li>"yyyy"</li>
+ * </ul>
+ * <p/>
+ * Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
+ * <p/>
+ * @param sDate string to parse for a date.
+ * @return the Date represented by the given W3C date-time string.
+ * It returns <b>null</b> if it was not possible to parse the given string into a Date.
+ *
+ */
+ public static Date parseW3CDateTime(String sDate) {
+ // if sDate has time on it, it injects 'GTM' before de TZ displacement to
+ // allow the SimpleDateFormat parser to parse it properly
+ int tIndex = sDate.indexOf("T");
+ if (tIndex>-1) {
+ if (sDate.endsWith("Z")) {
+ sDate = sDate.substring(0,sDate.length()-1)+"+00:00";
+ }
+ int tzdIndex = sDate.indexOf("+",tIndex);
+ if (tzdIndex==-1) {
+ tzdIndex = sDate.indexOf("-",tIndex);
+ }
+ if (tzdIndex>-1) {
+ String pre = sDate.substring(0,tzdIndex);
+ int secFraction = pre.indexOf(",");
+ if (secFraction>-1) {
+ pre = pre.substring(0,secFraction);
+ }
+ String post = sDate.substring(tzdIndex);
+ sDate = pre + "GMT" + post;
+ }
+ }
+ else {
+ sDate += "T00:00GMT";
+ }
+ return parseUsingMask(W3CDATETIME_MASKS,sDate);
+ }
+
+
+ /**
+ * Parses a Date out of a String with a date in W3C date-time format or
+ * in a RFC822 format.
+ * <p>
+ * @param sDate string to parse for a date.
+ * @return the Date represented by the given W3C date-time string.
+ * It returns <b>null</b> if it was not possible to parse the given string into a Date.
+ *
+ * */
+ public static Date parseDate(String sDate) {
+ Date d = parseW3CDateTime(sDate);
+ if (d==null) {
+ d = parseRFC822(sDate);
+ if (d==null && ADDITIONAL_MASKS.length>0) {
+ d = parseUsingMask(ADDITIONAL_MASKS,sDate);
+ }
+ }
+ return d;
+ }
+
+ /**
+ * create a RFC822 representation of a date.
+ * <p/>
+ * Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
+ * <p/>
+ * @param date Date to parse
+ * @return the RFC822 represented by the given Date
+ * It returns <b>null</b> if it was not possible to parse the date.
+ *
+ */
+ public static String formatRFC822(Date date) {
+ SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'",Locale.US);
+ dateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
+ return dateFormater.format(date);
+ }
+
+ /**
+ * create a W3C Date Time representation of a date.
+ * <p/>
+ * Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
+ * <p/>
+ * @param date Date to parse
+ * @return the W3C Date Time represented by the given Date
+ * It returns <b>null</b> if it was not possible to parse the date.
+ *
+ */
+ public static String formatW3CDateTime(Date date) {
+ SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",Locale.US);
+ dateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
+ return dateFormater.format(date);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedGenerators.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedGenerators.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedGenerators.java
new file mode 100644
index 0000000..f6e1a71
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedGenerators.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.io.WireFeedGenerator;
+
+import java.util.List;
+
+/**
+ * Generates an XML document (JDOM Document) out of a Feed.
+ * <p>
+ * It can generate all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
+ * Atom 0.3 feed.
+ * <p>
+ * WireFeedGenerator instances are thread safe.
+ * <p>
+ * Generators for a specific type must extend this class and register in the generator list.
+ * (Right now registration is hardcoded in the WireFeedGenerator constructor).
+ * <p>
+ * @author Alejandro Abdelnur
+ *
+ */
+public class FeedGenerators extends PluginManager {
+
+ /**
+ * WireFeedGenerator.classes= [className] ...
+ *
+ */
+ public static final String FEED_GENERATORS_KEY = "WireFeedGenerator.classes";
+
+
+ public FeedGenerators() {
+ super(FEED_GENERATORS_KEY);
+ }
+
+ public WireFeedGenerator getGenerator(String feedType) {
+ return (WireFeedGenerator) getPlugin(feedType);
+ }
+
+ protected String getKey(Object obj) {
+ return ((WireFeedGenerator)obj).getType();
+ }
+
+ public List getSupportedFeedTypes() {
+ return getKeys();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedParsers.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedParsers.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedParsers.java
new file mode 100644
index 0000000..2aaa1fe
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/FeedParsers.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.io.WireFeedParser;
+import org.jdom2.Document;
+import java.util.List;
+
+/**
+ * Parses an XML document (JDOM Document) into a Feed.
+ * <p>
+ * It accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
+ * Atom 0.3 feeds.
+ * <p>
+ * The WireFeedParser is a liberal parser.
+ * <p>
+ * WireFeedParser instances are thread safe.
+ * <p>
+ * Parsers for a specific type must extend this class and register in the parser list.
+ * (Right now registration is hardcoded in the WireFeedParser constructor).
+ * <p>
+ * @author Alejandro Abdelnur
+ *
+ */
+public class FeedParsers extends PluginManager {
+
+ /**
+ * WireFeedParser.classes= [className] ...
+ *
+ */
+ public static final String FEED_PARSERS_KEY = "WireFeedParser.classes";
+
+ /**
+ * Creates a parser instance.
+ * <p>
+ *
+ */
+ public FeedParsers() {
+ super(FEED_PARSERS_KEY);
+ }
+
+ public List getSupportedFeedTypes() {
+ return getKeys();
+ }
+
+ /**
+ * Finds the real parser type for the given document feed.
+ * <p>
+ * @param document document feed to find the parser for.
+ * @return the parser for the given document or <b>null</b> if there is no parser for that document.
+ *
+ */
+ public WireFeedParser getParserFor(Document document) {
+ List parsers = getPlugins();
+ WireFeedParser parser = null;
+ for (int i=0;parser==null && i<parsers.size();i++) {
+ parser = (WireFeedParser) parsers.get(i);
+ if (!parser.isMyType(document)) {
+ parser = null;
+ }
+ }
+ return parser;
+ }
+
+ protected String getKey(Object obj) {
+ return ((WireFeedParser)obj).getType();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleGenerators.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleGenerators.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleGenerators.java
new file mode 100644
index 0000000..9c493f9
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleGenerators.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.feed.module.Module;
+import com.sun.syndication.io.ModuleGenerator;
+import org.jdom2.Element;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ */
+public class ModuleGenerators extends PluginManager {
+ private Set _allNamespaces;
+
+ public ModuleGenerators(String propertyKey, BaseWireFeedGenerator parentGenerator) {
+ super(propertyKey, null, parentGenerator);
+ }
+
+ public ModuleGenerator getGenerator(String uri) {
+ return (ModuleGenerator) getPlugin(uri);
+ }
+
+ protected String getKey(Object obj) {
+ return ((ModuleGenerator)obj).getNamespaceUri();
+ }
+
+ public List getModuleNamespaces() {
+ return getKeys();
+ }
+
+ public void generateModules(List modules, Element element) {
+ Map generators = getPluginMap();
+ for (int i = 0; i < modules.size(); i++) {
+ Module module = (Module) modules.get(i);
+ String namespaceUri = module.getUri();
+ ModuleGenerator generator = (ModuleGenerator)generators.get(namespaceUri);
+ if (generator != null) {
+ generator.generate(module, element);
+ }
+ }
+ }
+
+ public Set getAllNamespaces() {
+ if (_allNamespaces==null) {
+ _allNamespaces = new HashSet();
+ List mUris = getModuleNamespaces();
+ for (int i=0;i<mUris.size();i++) {
+ ModuleGenerator mGen = getGenerator((String)mUris.get(i));
+ _allNamespaces.addAll(mGen.getNamespaces());
+ }
+ }
+ return _allNamespaces;
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleParsers.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleParsers.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleParsers.java
new file mode 100644
index 0000000..ac895fb
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/ModuleParsers.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package com.sun.syndication.io.impl;
+
+import com.sun.syndication.feed.module.Module;
+import com.sun.syndication.io.ModuleParser;
+import com.sun.syndication.io.WireFeedParser;
+import org.jdom2.Element;
+import org.jdom2.Namespace;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ */
+public class ModuleParsers extends PluginManager {
+ public ModuleParsers(String propertyKey, WireFeedParser parentParser) {
+ super(propertyKey, parentParser, null);
+ }
+
+ public String getKey(Object obj) {
+ return ((ModuleParser)obj).getNamespaceUri();
+ }
+
+ public List getModuleNamespaces() {
+ return getKeys();
+ }
+
+ public List parseModules(Element root) {
+ List parsers = getPlugins();
+ List modules = null;
+ for (int i=0;i<parsers.size();i++) {
+ ModuleParser parser = (ModuleParser) parsers.get(i);
+ String namespaceUri = parser.getNamespaceUri();
+ Namespace namespace = Namespace.getNamespace(namespaceUri);
+ if (hasElementsFrom(root, namespace)) {
+ Module module = parser.parse(root);
+ if (module != null) {
+ if (modules == null) {
+ modules = new ArrayList();
+ }
+ modules.add(module);
+ }
+ }
+ }
+ return modules;
+ }
+
+ private boolean hasElementsFrom(Element root, Namespace namespace) {
+ boolean hasElements = false;
+// boolean hasElements = namespace.equals(root.getNamespace());
+
+ if (!hasElements) {
+ List children = root.getChildren();
+ for (int i=0;!hasElements && i < children.size();i++) {
+ Element child = (Element) children.get(i);
+ hasElements = namespace.equals(child.getNamespace());
+ }
+ }
+ return hasElements;
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/00c22e7c/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/NumberParser.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/NumberParser.java b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/NumberParser.java
new file mode 100644
index 0000000..6076b7b
--- /dev/null
+++ b/commons/marmotta-sesame-tools/marmotta-rio-rss/src/ext/java/com/sun/syndication/io/impl/NumberParser.java
@@ -0,0 +1,104 @@
+
+package com.sun.syndication.io.impl;
+
+
+/**
+ * A helper class that parses Numbers out of Strings in a lenient manner.
+ *
+ * <p>
+ * No method will throw any sort of Exception when parsing a string.
+ * All methods accept any Java String or null as legal input, if the
+ * input is non null, whitespace will be trimmed first, and then parsing
+ * will be attempted.
+ * </p>
+ * <p>
+ * :TODO: Add Integer, Float, and Double methods as needed.
+ * </p>
+ */
+public class NumberParser {
+
+ /**
+ * Private constructor to avoid NumberParser instances creation.
+ */
+ private NumberParser() {
+ }
+
+ /**
+ * Parses a Long out of a string.
+ *
+ * @param str string to parse for a Long.
+ * @return the Long represented by the given string,
+ * It returns <b>null</b> if it was not possible to parse the the string.
+ */
+ public static Long parseLong(String str) {
+ if (null != str) {
+ try {
+ return new Long(Long.parseLong(str.trim()));
+ } catch (Exception e) {
+ // :IGNORE:
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Parse an Integer from a String. If the String is not an integer <b>null</b> is returned
+ * and no exception is thrown.
+ *
+ * @param str the String to parse
+ * @return The Integer represented by the String, or null if it could not be parsed.
+ */
+ public static Integer parseInt(String str) {
+ if (null != str) {
+ try {
+ return new Integer(Integer.parseInt(str.trim()));
+ } catch (Exception e) {
+ // :IGNORE:
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Parse a Float from a String without exceptions. If the String is not a Float then null is returned
+ *
+ * @param str the String to parse
+ * @return The Float represented by the String, or null if it could not be parsed.
+ */
+ public static Float parseFloat(String str) {
+ if (null != str) {
+ try {
+ return new Float(Float.parseFloat(str.trim()));
+ } catch (Exception e) {
+ // :IGNORE:
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Parse a float from a String, with a default value
+ *
+ * @param str
+ * @param def the value to return if the String cannot be parsed
+ * @return
+ */
+ public static float parseFloat(String str, float def) {
+ Float result = parseFloat(str);
+ return (result == null) ? def : result.floatValue();
+ }
+
+ /**
+ * Parses a long out of a string.
+ *
+ * @param str string to parse for a long.
+ * @param def default value to return if it is not possible to parse the the string.
+ * @return the long represented by the given string, or the default.
+ */
+ public static long parseLong(String str, long def) {
+ Long ret = parseLong(str);
+ return (null == ret) ? def : ret.longValue();
+ }
+
+
+}