You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2011/09/20 17:57:20 UTC
svn commit: r1173209 [23/49] - in /abdera/abdera2: ./ .settings/ activities/
activities/src/ activities/src/main/ activities/src/main/java/
activities/src/main/java/org/ activities/src/main/java/org/apache/
activities/src/main/java/org/apache/abdera2/ ...
Added: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java?rev=1173209&view=auto
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java (added)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java Tue Sep 20 15:56:46 2011
@@ -0,0 +1,848 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera2.parser.axiom;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.activation.DataHandler;
+import javax.activation.MimeType;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.abdera2.common.Constants;
+import org.apache.abdera2.common.iri.IRI;
+import org.apache.abdera2.common.lang.Lang;
+import org.apache.abdera2.model.Base;
+import org.apache.abdera2.model.Category;
+import org.apache.abdera2.model.Content;
+import org.apache.abdera2.model.Div;
+import org.apache.abdera2.model.Document;
+import org.apache.abdera2.model.Element;
+import org.apache.abdera2.model.ElementIteratorWrapper;
+import org.apache.abdera2.model.ElementWrapper;
+import org.apache.abdera2.model.ElementList;
+import org.apache.abdera2.model.Link;
+import org.apache.abdera2.model.Text;
+import org.apache.abdera2.model.selector.CategorySchemeSelector;
+import org.apache.abdera2.model.selector.LinkRelSelector;
+import org.apache.abdera2.parser.ParseException;
+import org.apache.abdera2.parser.Parser;
+import org.apache.abdera2.parser.ParserOptions;
+import org.apache.abdera2.writer.Writer;
+import org.apache.abdera2.writer.WriterOptions;
+import org.apache.abdera2.common.mediatype.MimeTypeHelper;
+import org.apache.abdera2.common.selector.Selector;
+import org.apache.abdera2.factory.Factory;
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMComment;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMProcessingInstruction;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.impl.llom.OMElementImpl;
+
+@SuppressWarnings("unchecked")
+public class FOMElement extends OMElementImpl implements Element, OMElement, Constants {
+
+ private static final long serialVersionUID = 8024257594220911953L;
+
+ public FOMElement(QName qname) {
+ super(qname, null, null);
+ }
+
+ public FOMElement(String name, OMNamespace namespace, OMContainer parent, OMFactory factory) throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMElement(QName qname, OMContainer parent, OMFactory factory) throws OMException {
+ super(qname.getLocalPart(), getOrCreateNamespace(qname, parent, factory), parent, factory);
+ }
+
+ public FOMElement(QName qname, OMContainer parent, OMFactory factory, OMXMLParserWrapper builder) {
+ super(qname.getLocalPart(), factory.createOMNamespace(qname.getNamespaceURI(), qname.getPrefix()), parent,
+ builder, factory);
+ }
+
+ private static OMNamespace getOrCreateNamespace(QName qname, OMContainer parent, OMFactory factory) {
+ String namespace = qname.getNamespaceURI();
+ String prefix = qname.getPrefix();
+ if (parent != null && parent instanceof OMElement) {
+ OMNamespace ns = ((OMElement)parent).findNamespace(namespace, prefix);
+ if (ns != null)
+ return ns;
+ }
+ return factory.createOMNamespace(qname.getNamespaceURI(), qname.getPrefix());
+ }
+
+ protected Element getWrapped(Element internal) {
+ if (internal == null)
+ return null;
+ FOMFactory factory = (FOMFactory)getFactory();
+ return factory.getElementWrapper(internal);
+ }
+
+ public <T extends Base> T getParentElement() {
+ T parent = (T)super.getParent();
+ return (T)((parent instanceof Element) ? getWrapped((Element)parent) : parent);
+ }
+
+ protected void setParentDocument(Document<?> parent) {
+ super.setParent((OMContainer)parent);
+ }
+
+ public <T extends Element> T setParentElement(Element parent) {
+ if (parent instanceof ElementWrapper) {
+ parent = ((ElementWrapper)parent).getInternal();
+ }
+ super.setParent((FOMElement)parent);
+ return (T)this;
+ }
+
+ public <T extends Element> T getPreviousSibling() {
+ OMNode el = this.getPreviousOMSibling();
+ while (el != null) {
+ if (el instanceof Element)
+ return (T)getWrapped((Element)el);
+ else
+ el = el.getPreviousOMSibling();
+ }
+ return null;
+ }
+
+ public <T extends Element> T getNextSibling() {
+ OMNode el = this.getNextOMSibling();
+ while (el != null) {
+ if (el instanceof Element)
+ return (T)getWrapped((Element)el);
+ else
+ el = el.getNextOMSibling();
+ }
+ return null;
+ }
+
+ public <T extends Element> T getFirstChild() {
+ return (T)getWrapped((Element)this.getFirstElement());
+ }
+
+ public <T extends Element> T getPreviousSibling(QName qname) {
+ Element el = getPreviousSibling();
+ while (el != null) {
+ OMElement omel = (OMElement)el;
+ if (omel.getQName().equals(qname))
+ return (T)getWrapped((Element)omel);
+ el = el.getPreviousSibling();
+ }
+ return null;
+ }
+
+ public <T extends Element> T getNextSibling(QName qname) {
+ Element el = getNextSibling();
+ while (el != null) {
+ OMElement omel = (OMElement)el;
+ if (omel.getQName().equals(qname))
+ return (T)getWrapped((Element)omel);
+ el = el.getNextSibling();
+ }
+ return null;
+ }
+
+ public <T extends Element> T getFirstChild(QName qname) {
+ return (T)getWrapped((Element)this.getFirstChildWithName(qname));
+ }
+
+ public Lang getLanguageTag() {
+ String lang = getLanguage();
+ return (lang != null) ? new Lang(lang) : null;
+ }
+
+ public String getLanguage() {
+ String lang = getAttributeValue(LANG);
+ Base parent = this.getParentElement();
+ return (lang != null) ? lang : (parent != null && parent instanceof Element) ? ((Element)parent).getLanguage()
+ : (parent != null && parent instanceof Document) ? ((Document<?>)parent).getLanguage() : null;
+ }
+
+ public <T extends Element> T setLanguage(String language) {
+ complete();
+ return setAttributeValue(LANG, language);
+ }
+
+ public IRI getBaseUri() {
+ IRI uri = _getUriValue(getAttributeValue(BASE));
+ if (uri == null) {
+ if (parent instanceof Element) {
+ uri = ((Element)parent).getBaseUri();
+ } else if (parent instanceof Document) {
+ uri = ((Document<?>)parent).getBaseUri();
+ }
+ }
+ return uri;
+ }
+
+ public IRI getResolvedBaseUri() {
+ IRI baseUri = null;
+ IRI uri = _getUriValue(getAttributeValue(BASE));
+ if (parent instanceof Element)
+ baseUri = ((Element)parent).getResolvedBaseUri();
+ else if (parent instanceof Document)
+ baseUri = ((Document<?>)parent).getBaseUri();
+ if (uri != null && baseUri != null) {
+ uri = baseUri.resolve(uri);
+ } else if (uri == null) {
+ uri = baseUri;
+ }
+ return uri;
+ }
+
+ public <T extends Element> T setBaseUri(IRI base) {
+ complete();
+ return setAttributeValue(BASE, _getStringValue(base));
+ }
+
+ public <T extends Element> T setBaseUri(String base) {
+ setBaseUri((base != null) ? new IRI(base) : null);
+ return (T)this;
+ }
+
+ public String getAttributeValue(QName qname) {
+ OMAttribute attr = getAttribute(qname);
+ String value = (attr != null) ? attr.getAttributeValue() : null;
+ return getMustPreserveWhitespace() || value == null ? value : value.trim();
+ }
+
+ public <T extends Element> T setAttributeValue(QName qname, String value) {
+ OMAttribute attr = this.getAttribute(qname);
+ if (attr != null && value != null) {
+ attr.setAttributeValue(value);
+ } else {
+ if (value != null) {
+ String uri = qname.getNamespaceURI();
+ String prefix = qname.getPrefix();
+ if (uri != null) {
+ OMNamespace ns = findNamespace(uri, prefix);
+ if (ns == null)
+ ns = factory.createOMNamespace(uri, prefix);
+ attr = factory.createOMAttribute(qname.getLocalPart(), ns, value);
+ } else {
+ attr = factory.createOMAttribute(qname.getLocalPart(), null, value);
+ }
+ if (attr != null)
+ addAttribute(attr);
+ } else if (attr != null){
+ removeAttribute(attr);
+ }
+ }
+ return (T)this;
+ }
+
+ protected <E extends Element> List<E> _getChildrenAsSet(QName qname) {
+ Iterator<E> i = new ElementIteratorWrapper<E>(getFactory(),getChildrenWithName(qname));
+ return new ElementList<E>(i);
+ }
+
+ protected <E extends Element> List<E> _getChildrenAsSet(QName qname, Selector selector) {
+ Iterator<E> i = new ElementIteratorWrapper<E>(getFactory(),getChildrenWithName(qname), selector);
+ return new ElementList<E>(i);
+ }
+
+ protected void _setChild(QName qname, OMElement element) {
+ OMElement e = getFirstChildWithName(qname);
+ if (e == null && element != null) {
+ addChild(element);
+ } else if (e != null && element != null) {
+ e.insertSiblingBefore(element);
+ e.discard();
+ } else if (e != null && element == null) {
+ e.discard();
+ }
+ }
+
+ protected IRI _getUriValue(String v) {
+ return (v != null) ? new IRI(v) : null;
+ }
+
+ protected String _getStringValue(IRI uri) {
+ return (uri != null) ? uri.toString() : null;
+ }
+
+ protected IRI _resolve(IRI base, IRI value) {
+ return base != null ? base.resolve(value) : value;
+ }
+
+ public void writeTo(OutputStream out, WriterOptions options) throws IOException {
+ Writer writer = this.getFactory().getAbdera().getWriter();
+ writer.writeTo(this, out, options);
+ }
+
+ public void writeTo(java.io.Writer out, WriterOptions options) throws IOException {
+ Writer writer = this.getFactory().getAbdera().getWriter();
+ writer.writeTo(this, out, options);
+ }
+
+ public void writeTo(Writer writer, OutputStream out) throws IOException {
+ writer.writeTo(this, out);
+ }
+
+ public void writeTo(Writer writer, java.io.Writer out) throws IOException {
+ writer.writeTo(this, out);
+ }
+
+ public void writeTo(Writer writer, OutputStream out, WriterOptions options) throws IOException {
+ writer.writeTo(this, out, options);
+ }
+
+ public void writeTo(Writer writer, java.io.Writer out, WriterOptions options) throws IOException {
+ writer.writeTo(this, out, options);
+ }
+
+ public void writeTo(OutputStream out) throws IOException {
+ Document<?> doc = getDocument();
+ String charset = doc != null ? doc.getCharset() : "UTF-8";
+ Writer writer = this.getFactory().getAbdera().getWriter();
+ writeTo(writer, new OutputStreamWriter(out, charset));
+ }
+
+ public void writeTo(java.io.Writer writer) throws IOException {
+ Writer out = getFactory().getAbdera().getWriter();
+ if (!(out instanceof FOMWriter)) {
+ out.writeTo(this, writer);
+ } else {
+ try {
+ OMOutputFormat outputFormat = new OMOutputFormat();
+ if (getDocument() != null && getDocument().getCharset() != null)
+ outputFormat.setCharSetEncoding(getDocument().getCharset());
+ serialize(writer, outputFormat);
+ } catch (XMLStreamException e) {
+ throw new FOMException(e);
+ }
+ }
+ }
+
+ public <T extends Element> Document<T> getDocument() {
+ Document<T> document = null;
+ if (parent != null) {
+ if (parent instanceof Element) {
+ document = ((Element)parent).getDocument();
+ } else if (parent instanceof Document) {
+ document = (Document<T>)parent;
+ }
+ }
+ return document;
+ }
+
+ public String getAttributeValue(String name) {
+ return getAttributeValue(new QName(name));
+ }
+
+ public <T extends Element> T setAttributeValue(String name, String value) {
+ return setAttributeValue(new QName(name), value);
+ }
+
+ protected void _setElementValue(QName qname, String value) {
+ complete();
+ OMElement element = this.getFirstChildWithName(qname);
+ if (element != null && value != null) {
+ element.setText(value);
+ } else if (element != null && value == null) {
+ for (Iterator<OMNode> i = element.getChildren(); i.hasNext();) {
+ OMNode node = i.next();
+ node.discard();
+ }
+ } else if (element == null && value != null) {
+ element = factory.createOMElement(qname, this);
+ element.setText(value);
+ this.addChild(element);
+ }
+ }
+
+ protected String _getElementValue(QName qname) {
+ String value = null;
+ OMElement element = this.getFirstChildWithName(qname);
+ if (element != null)
+ value = element.getText();
+ return getMustPreserveWhitespace() || value == null ? value : value.trim();
+ }
+
+ protected <T extends Text> T getTextElement(QName qname) {
+ return (T)getFirstChildWithName(qname);
+ }
+
+ protected <T extends Text> void setTextElement(QName qname, T text, boolean many) {
+ complete();
+ if (text != null) {
+ _setChild(qname, (OMElement)text);
+ } else
+ _removeChildren(qname, false);
+ }
+
+ protected Text setTextText(QName qname, String value) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Text text = fomfactory.newText(qname, Text.Type.TEXT);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setHtmlText(QName qname, String value, IRI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Text text = fomfactory.newText(qname, Text.Type.HTML);
+ if (baseUri != null)
+ text.setBaseUri(baseUri);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setXhtmlText(QName qname, String value, IRI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Text text = fomfactory.newText(qname, Text.Type.XHTML);
+ if (baseUri != null)
+ text.setBaseUri(baseUri);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setXhtmlText(QName qname, Div value, IRI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Text text = fomfactory.newText(qname, Text.Type.XHTML);
+ if (baseUri != null)
+ text.setBaseUri(baseUri);
+ text.setValueElement(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ public void setText(String text) {
+ complete();
+ if (text != null) {
+ OMNode child = this.getFirstOMChild();
+ while (child != null) {
+ if (child.getType() == OMNode.TEXT_NODE) {
+ child.detach();
+ }
+ child = child.getNextOMSibling();
+ }
+ getOMFactory().createOMText(this, text);
+ } else
+ _removeAllChildren();
+ }
+
+ public String getText() {
+ StringBuilder buf = new StringBuilder();
+ Iterator<OMNode> i = getChildren();
+ while (i.hasNext()) {
+ OMNode node = i.next();
+ if (node instanceof OMText) {
+ buf.append(((OMText)node).getText());
+ } else {
+ // for now, let's ignore other elements. eventually, we
+ // should make this work like innerHTML in browsers... stripping
+ // out all markup but leaving all text, even in child nodes
+ }
+ }
+ String value = buf.toString();
+ return getMustPreserveWhitespace() || value == null ? value : value.trim();
+ }
+
+ protected String getText(QName qname) {
+ Text text = getTextElement(qname);
+ return (text != null) ? text.getValue() : null;
+ }
+
+ public List<QName> getAttributes() {
+ List<QName> list = new ArrayList<QName>();
+ for (Iterator<OMAttribute> i = getAllAttributes(); i.hasNext();) {
+ OMAttribute attr = i.next();
+ list.add(attr.getQName());
+ }
+ return Collections.unmodifiableList(list);
+ }
+
+ public List<QName> getExtensionAttributes() {
+ List<QName> list = new ArrayList<QName>();
+ for (Iterator<OMAttribute> i = getAllAttributes(); i.hasNext();) {
+ OMAttribute attr = i.next();
+ String namespace = (attr.getNamespace() != null) ? attr.getNamespace().getNamespaceURI() : "";
+ if (!namespace.equals(getNamespace().getNamespaceURI()) && !namespace.equals(""))
+ list.add(attr.getQName());
+ }
+ return Collections.unmodifiableList(list);
+ }
+
+ protected Element _parse(String value, IRI baseUri) throws ParseException, UnsupportedEncodingException {
+ if (value == null)
+ return null;
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Parser parser = fomfactory.newParser();
+ Document<?> thisdoc = getDocument();
+ String charset = thisdoc != null ? thisdoc.getCharset() : "UTF-8";
+ charset = charset != null ? charset : "UTF-8";
+ ByteArrayInputStream bais =
+ new ByteArrayInputStream(value.getBytes(charset));
+ ParserOptions options = parser.getDefaultParserOptions();
+ options.setCharset(getXMLStreamReader().getCharacterEncodingScheme());
+ options.setFactory(fomfactory);
+ Document<?> doc = parser.parse(bais, (baseUri != null) ? baseUri.toString() : null, options);
+ return doc.getRoot();
+ }
+
+ public <T extends Element> T removeAttribute(QName qname) {
+ OMAttribute attr = getAttribute(qname);
+ if (attr != null)
+ removeAttribute(attr);
+ return (T)this;
+ }
+
+ public <T extends Element> T removeAttribute(String name) {
+ OMAttribute attr = getAttribute(new QName(name));
+ if (attr != null)
+ removeAttribute(attr);
+ return (T)this;
+ }
+
+ protected void _removeChildren(QName qname, boolean many) {
+ complete();
+ if (many) {
+ for (Iterator<OMElement> i = getChildrenWithName(qname); i.hasNext();) {
+ OMElement element = i.next();
+ element.discard();
+ }
+ } else {
+ OMElement element = getFirstChildWithName(qname);
+ if (element != null)
+ element.discard();
+ }
+ }
+
+ protected void _removeAllChildren() {
+ complete();
+ for (Iterator<OMNode> i = getChildren(); i.hasNext();) {
+ OMNode node = i.next();
+ node.discard();
+ }
+ }
+
+ public Object clone() {
+ OMElement el = _create(this);
+ _copyElement(this, el);
+ return el;
+
+ }
+
+ protected OMElement _copyElement(OMElement src, OMElement dest) {
+ for (Iterator<OMAttribute> i = src.getAllAttributes(); i.hasNext();) {
+ OMAttribute attr = i.next();
+ dest.addAttribute(attr);
+ dest.addAttribute(factory.createOMAttribute(attr.getLocalName(), attr.getNamespace(), attr
+ .getAttributeValue()));
+ }
+ for (Iterator<OMNode> i = src.getChildren(); i.hasNext();) {
+ OMNode node = i.next();
+ if (node.getType() == OMNode.ELEMENT_NODE) {
+ OMElement element = (OMElement)node;
+ OMElement child = _create(element);
+ if (child != null) {
+ _copyElement(element, child);
+ dest.addChild(child);
+ }
+ } else if (node.getType() == OMNode.CDATA_SECTION_NODE) {
+ OMText text = (OMText)node;
+ factory.createOMText(dest, text.getText(), OMNode.CDATA_SECTION_NODE);
+ } else if (node.getType() == OMNode.TEXT_NODE) {
+ OMText text = (OMText)node;
+ factory.createOMText(dest, text.getText());
+ } else if (node.getType() == OMNode.COMMENT_NODE) {
+ OMComment comment = (OMComment)node;
+ factory.createOMComment(dest, comment.getValue());
+ } else if (node.getType() == OMNode.PI_NODE) {
+ OMProcessingInstruction pi = (OMProcessingInstruction)node;
+ factory.createOMProcessingInstruction(dest, pi.getTarget(), pi.getValue());
+ } else if (node.getType() == OMNode.SPACE_NODE) {
+ OMText text = (OMText)node;
+ factory.createOMText(dest, text.getText(), OMNode.SPACE_NODE);
+ } else if (node.getType() == OMNode.ENTITY_REFERENCE_NODE) {
+ OMText text = (OMText)node;
+ factory.createOMText(dest, text.getText(), OMNode.ENTITY_REFERENCE_NODE);
+ }
+ }
+ return dest;
+ }
+
+ protected OMElement _create(OMElement src) {
+ OMElement el = null;
+
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Object obj = null;
+ if (src instanceof Content)
+ obj = ((Content)src).getContentType();
+ if (src instanceof Text)
+ obj = ((Text)src).getTextType();
+ el = fomfactory.createElement(src.getQName(), (OMContainer)fomfactory.newDocument(), factory, obj);
+
+ return el;
+ }
+
+ public Factory getFactory() {
+ return (Factory)this.factory;
+ }
+
+ public <T extends Base> T addComment(String value) {
+ factory.createOMComment(this, value);
+ return (T)this;
+ }
+
+ public Locale getLocale() {
+ String tag = getLanguage();
+ if (tag == null || tag.length() == 0)
+ return null;
+ String[] tokens = tag.split("-");
+ Locale locale = null;
+ switch (tokens.length) {
+ case 0:
+ break;
+ case 1:
+ locale = new Locale(tokens[0]);
+ break;
+ case 2:
+ locale = new Locale(tokens[0], tokens[1]);
+ break;
+ default:
+ locale = new Locale(tokens[0], tokens[1], tokens[2]);
+ break;
+ }
+ return locale;
+ }
+
+ protected Link selectLink(List<Link> links, String type, String hreflang) {
+ for (Link link : links) {
+ MimeType mt = link.getMimeType();
+ boolean typematch = MimeTypeHelper.isMatch((mt != null) ? mt.toString() : null, type);
+ boolean langmatch =
+ "*".equals(hreflang) || ((hreflang != null) ? hreflang.equals(link.getHrefLang())
+ : link.getHrefLang() == null);
+ if (typematch && langmatch)
+ return link;
+ }
+ return null;
+ }
+
+ public <T extends Element> T declareNS(String uri, String prefix) {
+ if (!isDeclared(uri, prefix)) {
+ super.declareNamespace(uri, prefix);
+ }
+ return (T)this;
+ }
+
+ protected boolean isDeclared(String ns, String prefix) {
+ for (Iterator<OMNamespace> i = this.getAllDeclaredNamespaces(); i.hasNext();) {
+ OMNamespace omn = i.next();
+ if (omn.getNamespaceURI().equals(ns) && (omn.getPrefix() != null && omn.getPrefix().equals(prefix)))
+ return true;
+ }
+ Base parent = this.getParentElement();
+ if (parent != null && parent instanceof FOMElement) {
+ return ((FOMElement)parent).isDeclared(ns, prefix);
+ } else
+ return false;
+ }
+
+ protected void declareIfNecessary(String ns, String prefix) {
+ if (prefix != null && !prefix.equals("") && !isDeclared(ns, prefix)) {
+ declareNS(ns, prefix);
+ }
+ }
+
+ public Map<String, String> getNamespaces() {
+ Map<String, String> namespaces = new HashMap<String, String>();
+ OMElement current = this;
+ while (current != null) {
+ Iterator<OMNamespace> i = current.getAllDeclaredNamespaces();
+ while (i.hasNext()) {
+ OMNamespace ns = i.next();
+ String prefix = ns.getPrefix();
+ String uri = ns.getNamespaceURI();
+ if (!namespaces.containsKey(prefix))
+ namespaces.put(prefix, uri);
+ }
+ OMContainer parent = current.getParent();
+ current = (OMElement)((parent != null && parent instanceof OMElement) ? parent : null);
+ }
+ return Collections.unmodifiableMap(namespaces);
+ }
+
+ public <T extends Element> List<T> getElements() {
+ return new ElementList<T>(new ElementIteratorWrapper<T>(getFactory(),getChildElements()));
+ }
+
+ public <T extends Element> List<T> getElements(Selector selector) {
+ return new ElementList<T>(new ElementIteratorWrapper<T>(getFactory(),getChildElements(),selector));
+ }
+
+ public boolean getMustPreserveWhitespace() {
+ OMAttribute attr = getAttribute(SPACE);
+ String space = (attr != null) ? attr.getAttributeValue() : null;
+ Base parent = this.getParentElement();
+
+ return space != null && space.equalsIgnoreCase("preserve") ? true
+ : (parent != null && parent instanceof Element) ? ((Element)parent).getMustPreserveWhitespace()
+ : (parent != null && parent instanceof Document) ? ((Document<?>)parent).getMustPreserveWhitespace()
+ : true;
+ }
+
+ public <T extends Element> T setMustPreserveWhitespace(boolean preserve) {
+ complete();
+ if (preserve && !getMustPreserveWhitespace()) {
+ setAttributeValue(SPACE, "preserve");
+ } else if (!preserve && getMustPreserveWhitespace()) {
+ setAttributeValue(SPACE, "default");
+ }
+ return (T)this;
+ }
+
+ public <T extends Element> T setText(DataHandler handler) {
+ _removeAllChildren();
+ addChild(factory.createOMText(handler, true));
+ return (T)this;
+ }
+
+ public WriterOptions getDefaultWriterOptions() {
+ return new FOMWriter().getDefaultWriterOptions();
+ }
+
+ /**
+ * Ensure that the underlying streams are fully parsed. We might eventually need to find a more efficient way of
+ * doing this, but for now, calling toString() will ensure that this particular object is fully parsed and ready to
+ * be modified. Calling complete on an Element does not necessarily mean that the underlying stream is fully
+ * consumed, only that that particular element has been completely parsed.
+ */
+ public <T extends Base> T complete() {
+ try {
+ if (!isComplete() && builder != null)
+ super.build();
+ } catch (Throwable t) {}
+ return (T)this;
+ }
+
+ /**
+ * Iterate over all child elements
+ */
+ public Iterator<Element> iterator() {
+ return getElements().iterator();
+ }
+
+ public void writeTo(String writer, OutputStream out) throws IOException {
+ writeTo(getFactory().getAbdera().getWriterFactory().getWriter(writer), out);
+ }
+
+ public void writeTo(String writer, java.io.Writer out) throws IOException {
+ writeTo(getFactory().getAbdera().getWriterFactory().getWriter(writer), out);
+ }
+
+ public void writeTo(String writer, OutputStream out, WriterOptions options) throws IOException {
+ writeTo(getFactory().getAbdera().getWriterFactory().getWriter(writer), out, options);
+ }
+
+ public void writeTo(String writer, java.io.Writer out, WriterOptions options) throws IOException {
+ writeTo(getFactory().getAbdera().getWriterFactory().getWriter(writer), out, options);
+ }
+
+ public String toFormattedString() {
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ writeTo("prettyxml", out);
+ return new String(out.toByteArray(), "UTF-8");
+ } catch (Exception e) {
+ return toString();
+ }
+ }
+
+ public static String generateUuid() {
+ return UUID.randomUUID().toString();
+ }
+
+ public static List<Link> getLinks(Element element, String... rels) {
+ LinkRelSelector sel =
+ new LinkRelSelector(rels);
+ return element.getElements(sel);
+ }
+
+ public static List<Link> getLinks(Element element, String rel) {
+ LinkRelSelector sel =
+ new LinkRelSelector(rel);
+ return element.getElements(sel);
+ }
+
+ public static List<Category> getCategories(Element element, String scheme) {
+ CategorySchemeSelector sel =
+ new CategorySchemeSelector(scheme);
+ return element.getElements(sel);
+ }
+
+ /**
+ * Returns true if this element has a child element or attribute
+ * with the given QName
+ */
+ public boolean has(QName qname) {
+ if (this.getFirstChild(qname) != null) return true;
+ if (this.getAttribute(qname) != null) return true;
+ return false;
+ }
+
+ /**
+ * Returns true if this element has an attribute with the given name
+ */
+ public boolean hasAttribute(String name) {
+ return this.getAttributeValue(name) != null;
+ }
+}
Propchange: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMEntry.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMEntry.java?rev=1173209&view=auto
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMEntry.java (added)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMEntry.java Tue Sep 20 15:56:46 2011
@@ -0,0 +1,998 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera2.parser.axiom;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Date;
+import java.util.List;
+
+import javax.activation.DataHandler;
+import javax.activation.MimeType;
+import javax.xml.namespace.QName;
+
+import org.apache.abdera2.common.Constants;
+import org.apache.abdera2.common.mediatype.MimeTypeHelper;
+import org.apache.abdera2.common.selector.Selector;
+import org.apache.abdera2.common.iri.IRI;
+import org.apache.abdera2.extra.InputStreamDataSource;
+import org.apache.abdera2.model.Categories;
+import org.apache.abdera2.model.Category;
+import org.apache.abdera2.model.Content;
+import org.apache.abdera2.model.Control;
+import org.apache.abdera2.model.DateTime;
+import org.apache.abdera2.model.Div;
+import org.apache.abdera2.model.Document;
+import org.apache.abdera2.model.Element;
+import org.apache.abdera2.model.Entry;
+import org.apache.abdera2.model.Feed;
+import org.apache.abdera2.model.IRIElement;
+import org.apache.abdera2.model.Link;
+import org.apache.abdera2.model.Person;
+import org.apache.abdera2.model.Source;
+import org.apache.abdera2.model.Text;
+import org.apache.abdera2.model.Content.Type;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+@SuppressWarnings("deprecation")
+public class FOMEntry extends FOMExtensibleElement implements Entry {
+
+ private static final long serialVersionUID = 1L;
+
+ public FOMEntry() {
+ super(Constants.ENTRY, new FOMDocument<Entry>(), new FOMFactory());
+ }
+
+ public FOMEntry(String name, OMNamespace namespace, OMContainer parent, OMFactory factory) throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMEntry(QName qname, OMContainer parent, OMFactory factory) {
+ super(qname, parent, factory);
+ }
+
+ public FOMEntry(QName qname, OMContainer parent, OMFactory factory, OMXMLParserWrapper builder) {
+ super(qname, parent, factory, builder);
+ }
+
+ public FOMEntry(OMContainer parent, OMFactory factory) throws OMException {
+ super(ENTRY, parent, factory);
+ }
+
+ public FOMEntry(OMContainer parent, OMFactory factory, OMXMLParserWrapper builder) throws OMException {
+ super(ENTRY, parent, factory, builder);
+ }
+
+ public Person getAuthorInherited() {
+ Person person = getAuthor();
+ if (person == null) {
+ Source source = getSource();
+ if (source == null) {
+ Element parent = getParentElement();
+ if (parent != null && parent instanceof Source)
+ source = (Source) parent;
+ }
+ if (source != null)
+ person = source.getAuthor();
+ }
+ return person;
+ }
+
+ @SuppressWarnings("unused")
+ private boolean is_empty(List<?> list) {
+ if (list == null) return true;
+ for (Object o : list)
+ return false;
+ return true;
+ }
+
+ public List<Person> getAuthorsInherited() {
+ List<Person>list = getAuthors();
+ if (is_empty(list)) {
+ Source source = getSource();
+ if (source == null) {
+ Element parent = getParentElement();
+ if (parent != null && parent instanceof Source)
+ source = (Source) parent;
+ }
+ if (source != null)
+ list = source.getAuthors();
+ }
+ return list;
+ }
+
+ public List<Person> getAuthorsInherited(Selector selector) {
+ List<Person>list = getAuthors(selector);
+ if (is_empty(list)) {
+ Source source = getSource();
+ if (source == null) {
+ Element parent = getParentElement();
+ if (parent != null && parent instanceof Source)
+ source = (Source) parent;
+ }
+ if (source != null)
+ list = source.getAuthors(selector);
+ }
+ return list;
+ }
+
+ public Person getAuthor() {
+ return (Person)getFirstChildWithName(AUTHOR);
+ }
+
+ public List<Person> getAuthors() {
+ return _getChildrenAsSet(AUTHOR);
+ }
+
+ public Entry addAuthor(Person person) {
+ complete();
+ addChild((OMElement)person);
+ return this;
+ }
+
+ public Person addAuthor(String name) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)this.factory;
+ Person person = fomfactory.newAuthor(this);
+ person.setName(name);
+ return person;
+ }
+
+ public Person addAuthor(String name, String email, String uri) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)this.factory;
+ Person person = fomfactory.newAuthor(this);
+ person.setName(name);
+ person.setEmail(email);
+ person.setUri(uri);
+ return person;
+ }
+
+ public List<Category> getCategories() {
+ return _getChildrenAsSet(CATEGORY);
+ }
+
+ public List<Category> getCategories(String scheme) {
+ return FOMElement.getCategories(this, scheme);
+ }
+
+ public Entry addCategory(Category category) {
+ complete();
+ Element el = category.getParentElement();
+ if (el != null && el instanceof Categories) {
+ Categories cats = category.getParentElement();
+ category = (Category)category.clone();
+ try {
+ if (category.getScheme() == null && cats.getScheme() != null)
+ category.setScheme(cats.getScheme().toString());
+ } catch (Exception e) {
+ // Do nothing, shouldn't happen
+ }
+ }
+ addChild((OMElement)category);
+ return this;
+ }
+
+ public Category addCategory(String term) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Category category = factory.newCategory(this);
+ category.setTerm(term);
+ return category;
+ }
+
+ public Category addCategory(String scheme, String term, String label) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Category category = factory.newCategory(this);
+ category.setTerm(term);
+ category.setScheme(scheme);
+ category.setLabel(label);
+ return category;
+ }
+
+ public Content getContentElement() {
+ return (Content)getFirstChildWithName(CONTENT);
+ }
+
+ public Entry setContentElement(Content content) {
+ complete();
+ if (content != null) {
+ _setChild(CONTENT, (OMElement)content);
+ } else {
+ _removeChildren(CONTENT, false);
+ }
+ return this;
+ }
+
+ /**
+ * Sets the content for this entry as @type="text"
+ */
+ public Content setContent(String value) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent();
+ content.setValue(value);
+ setContentElement(content);
+ return content;
+ }
+
+ public Content setContentAsHtml(String value) {
+ return setContent(value, Content.Type.HTML);
+ }
+
+ public Content setContentAsXhtml(String value) {
+ return setContent(value, Content.Type.XHTML);
+ }
+
+ /**
+ * Sets the content for this entry
+ */
+ public Content setContent(String value, Content.Type type) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent(type);
+ content.setValue(value);
+ setContentElement(content);
+ return content;
+ }
+
+ /**
+ * Sets the content for this entry
+ */
+ public Content setContent(Element value) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent();
+ content.setValueElement(value);
+ setContentElement(content);
+ return content;
+ }
+
+ /**
+ * Sets the content for this entry
+ *
+ * @throws MimeTypeParseException
+ */
+ public Content setContent(Element element, String mediaType) {
+ try {
+ if (MimeTypeHelper.isText(mediaType))
+ throw new IllegalArgumentException();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent(new MimeType(mediaType));
+ content.setValueElement(element);
+ setContentElement(content);
+ return content;
+ } catch (javax.activation.MimeTypeParseException e) {
+ throw new org.apache.abdera2.common.mediatype.MimeTypeParseException(e);
+ }
+ }
+
+ /**
+ * Sets the content for this entry
+ *
+ * @throws MimeTypeParseException
+ */
+ public Content setContent(DataHandler dataHandler) {
+ return setContent(dataHandler, dataHandler.getContentType());
+ }
+
+ /**
+ * Sets the content for this entry
+ *
+ * @throws MimeTypeParseException
+ */
+ public Content setContent(DataHandler dataHandler, String mediatype) {
+ if (MimeTypeHelper.isText(mediatype)) {
+ try {
+ return setContent(dataHandler.getInputStream(), mediatype);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent(Content.Type.MEDIA);
+ content.setDataHandler(dataHandler);
+ if (mediatype != null)
+ content.setMimeType(mediatype);
+ setContentElement(content);
+ return content;
+ }
+ }
+
+ /**
+ * Sets the content for this entry
+ */
+ public Content setContent(InputStream in) {
+ InputStreamDataSource ds = new InputStreamDataSource(in);
+ DataHandler dh = new DataHandler(ds);
+ Content content = setContent(dh);
+ return content;
+ }
+
+ /**
+ * Sets the content for this entry
+ */
+ public Content setContent(InputStream in, String mediatype) {
+ if (MimeTypeHelper.isText(mediatype)) {
+ try {
+ StringBuilder buf = new StringBuilder();
+ String charset = MimeTypeHelper.getCharset(mediatype);
+ Document<?> doc = this.getDocument();
+ charset = charset != null ? charset : doc != null ? doc.getCharset() : null;
+ charset = charset != null ? charset : "UTF-8";
+ InputStreamReader isr = new InputStreamReader(in, charset);
+ char[] data = new char[500];
+ int r = -1;
+ while ((r = isr.read(data)) != -1) {
+ buf.append(data, 0, r);
+ }
+ return setContent(buf.toString(), mediatype);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ InputStreamDataSource ds = new InputStreamDataSource(in, mediatype);
+ DataHandler dh = new DataHandler(ds);
+ return setContent(dh, mediatype);
+ }
+ }
+
+ /**
+ * Sets the content for this entry
+ *
+ * @throws MimeTypeParseException
+ */
+ public Content setContent(String value, String mediatype) {
+ try {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent(new MimeType(mediatype));
+ content.setValue(value);
+ content.setMimeType(mediatype);
+ setContentElement(content);
+ return content;
+ } catch (javax.activation.MimeTypeParseException e) {
+ throw new org.apache.abdera2.common.mediatype.MimeTypeParseException(e);
+ }
+ }
+
+ /**
+ * Sets the content for this entry
+ *
+ * @throws MimeTypeParseException
+ * @throws IRISyntaxException
+ */
+ public Content setContent(IRI uri, String mediatype) {
+ try {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Content content = factory.newContent(new MimeType(mediatype));
+ content.setSrc(uri.toString());
+ setContentElement(content);
+ return content;
+ } catch (javax.activation.MimeTypeParseException e) {
+ throw new org.apache.abdera2.common.mediatype.MimeTypeParseException(e);
+ }
+ }
+
+ public List<Person> getContributors() {
+ return _getChildrenAsSet(CONTRIBUTOR);
+ }
+
+ public Entry addContributor(Person person) {
+ complete();
+ addChild((OMElement)person);
+ return this;
+ }
+
+ public Person addContributor(String name) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)this.factory;
+ Person person = fomfactory.newContributor(this);
+ person.setName(name);
+ return person;
+ }
+
+ public Person addContributor(String name, String email, String uri) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)this.factory;
+ Person person = fomfactory.newContributor(this);
+ person.setName(name);
+ person.setEmail(email);
+ person.setUri(uri);
+ return person;
+ }
+
+ public IRIElement getIdElement() {
+ return (IRIElement)getFirstChildWithName(ID);
+ }
+
+ public Entry setIdElement(IRIElement id) {
+ complete();
+ if (id != null)
+ _setChild(ID, (OMElement)id);
+ else
+ _removeChildren(ID, false);
+ return this;
+ }
+
+ public IRI getId() {
+ IRIElement id = getIdElement();
+ return (id != null) ? id.getValue() : null;
+ }
+
+ public IRIElement setId(String value) {
+ return setId(value, false);
+ }
+
+ public IRIElement newId() {
+ return setId(this.getFactory().newUuidUri(), false);
+ }
+
+ public IRIElement setId(String value, boolean normalize) {
+ complete();
+ if (value == null) {
+ _removeChildren(ID, false);
+ return null;
+ }
+ IRIElement id = getIdElement();
+ if (id != null) {
+ if (normalize)
+ id.setNormalizedValue(value);
+ else
+ id.setValue(value);
+ return id;
+ } else {
+ FOMFactory fomfactory = (FOMFactory)factory;
+ IRIElement iri = fomfactory.newID(this);
+ iri.setValue((normalize) ? IRI.normalizeString(value) : value);
+ return iri;
+ }
+ }
+
+ public List<Link> getLinks() {
+ return _getChildrenAsSet(LINK);
+ }
+
+ public List<Link> getLinks(String rel) {
+ return getLinks(this, rel);
+ }
+
+ public List<Link> getLinks(String... rels) {
+ return FOMElement.getLinks(this, rels);
+ }
+
+ public Entry addLink(Link link) {
+ complete();
+ addChild((OMElement)link);
+ return this;
+ }
+
+ public Link addLink(String href) {
+ complete();
+ return addLink(href, null);
+ }
+
+ public Link addLink(String href, String rel) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Link link = fomfactory.newLink(this);
+ link.setHref(href);
+ if (rel != null)
+ link.setRel(rel);
+ return link;
+ }
+
+ public Link addLink(String href, String rel, String type, String title, String hreflang, long length) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Link link = fomfactory.newLink(this);
+ link.setHref(href);
+ link.setRel(rel);
+ link.setMimeType(type);
+ link.setTitle(title);
+ link.setHrefLang(hreflang);
+ link.setLength(length);
+ return link;
+ }
+
+ public DateTime getPublishedElement() {
+ return (DateTime)getFirstChildWithName(PUBLISHED);
+ }
+
+ public Entry setPublishedElement(DateTime dateTime) {
+ complete();
+ if (dateTime != null)
+ _setChild(PUBLISHED, (OMElement)dateTime);
+ else
+ _removeChildren(PUBLISHED, false);
+ return this;
+ }
+
+ public Date getPublished() {
+ DateTime dte = getPublishedElement();
+ return (dte != null) ? dte.getDate() : null;
+ }
+
+ private DateTime setPublished(org.apache.abdera2.common.date.DateTime value) {
+ complete();
+ if (value == null) {
+ _removeChildren(PUBLISHED, false);
+ return null;
+ }
+ DateTime dte = getPublishedElement();
+ if (dte != null) {
+ dte.setValue(value);
+ return dte;
+ } else {
+ FOMFactory fomfactory = (FOMFactory)factory;
+ DateTime dt = fomfactory.newPublished(this);
+ dt.setValue(value);
+ return dt;
+ }
+ }
+
+ public DateTime setPublished(Date value) {
+ return setPublished((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public DateTime setPublished(String value) {
+ return setPublished((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public Text getRightsElement() {
+ return getTextElement(RIGHTS);
+ }
+
+ public Entry setRightsElement(Text text) {
+ complete();
+ setTextElement(RIGHTS, text, false);
+ return this;
+ }
+
+ public Text setRights(String value) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newRights();
+ text.setValue(value);
+ setRightsElement(text);
+ return text;
+ }
+
+ public Text setRightsAsHtml(String value) {
+ return setRights(value, Text.Type.HTML);
+ }
+
+ public Text setRightsAsXhtml(String value) {
+ return setRights(value, Text.Type.XHTML);
+ }
+
+ public Text setRights(String value, Text.Type type) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newRights(type);
+ text.setValue(value);
+ setRightsElement(text);
+ return text;
+ }
+
+ public Text setRights(Div value) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newRights(Text.Type.XHTML);
+ text.setValueElement(value);
+ setRightsElement(text);
+ return text;
+ }
+
+ public String getRights() {
+ return getText(RIGHTS);
+ }
+
+ public Source getSource() {
+ return (Source)getFirstChildWithName(SOURCE);
+ }
+
+ public Entry setSource(Source source) {
+ complete();
+ if (source != null) {
+ if (source instanceof Feed)
+ source = ((Feed)source).getAsSource();
+ _setChild(SOURCE, (OMElement)source);
+ } else {
+ _removeChildren(SOURCE, false);
+ }
+ return this;
+ }
+
+ public Text getSummaryElement() {
+ return getTextElement(SUMMARY);
+ }
+
+ public Entry setSummaryElement(Text text) {
+ complete();
+ setTextElement(SUMMARY, text, false);
+ return this;
+ }
+
+ public Text setSummary(String value) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newSummary();
+ text.setValue(value);
+ setSummaryElement(text);
+ return text;
+ }
+
+ public Text setSummaryAsHtml(String value) {
+ return setSummary(value, Text.Type.HTML);
+ }
+
+ public Text setSummaryAsXhtml(String value) {
+ return setSummary(value, Text.Type.XHTML);
+ }
+
+ public Text setSummary(String value, Text.Type type) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newSummary(type);
+ text.setValue(value);
+ setSummaryElement(text);
+ return text;
+ }
+
+ public Text setSummary(Div value) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newSummary(Text.Type.XHTML);
+ text.setValueElement(value);
+ setSummaryElement(text);
+ return text;
+ }
+
+ public String getSummary() {
+ return getText(SUMMARY);
+ }
+
+ public Text getTitleElement() {
+ return getTextElement(TITLE);
+ }
+
+ public Entry setTitleElement(Text title) {
+ complete();
+ setTextElement(TITLE, title, false);
+ return this;
+ }
+
+ public Text setTitle(String value) {
+ complete();
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newTitle();
+ text.setValue(value);
+ setTitleElement(text);
+ return text;
+ }
+
+ public Text setTitleAsHtml(String value) {
+ return setTitle(value, Text.Type.HTML);
+ }
+
+ public Text setTitleAsXhtml(String value) {
+ return setTitle(value, Text.Type.XHTML);
+ }
+
+ public Text setTitle(String value, Text.Type type) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newTitle(type);
+ text.setValue(value);
+ setTitleElement(text);
+ return text;
+ }
+
+ public Text setTitle(Div value) {
+ FOMFactory factory = (FOMFactory)this.factory;
+ Text text = factory.newTitle(Text.Type.XHTML);
+ text.setValueElement(value);
+ setTitleElement(text);
+ return text;
+ }
+
+ public String getTitle() {
+ return getText(TITLE);
+ }
+
+ public DateTime getUpdatedElement() {
+ return (DateTime)getFirstChildWithName(UPDATED);
+ }
+
+ public Entry setUpdatedElement(DateTime updated) {
+ complete();
+ if (updated != null)
+ _setChild(UPDATED, (OMElement)updated);
+ else
+ _removeChildren(UPDATED, false);
+ return this;
+ }
+
+ public Date getUpdated() {
+ DateTime dte = getUpdatedElement();
+ return (dte != null) ? dte.getDate() : null;
+ }
+
+ private DateTime setUpdated(org.apache.abdera2.common.date.DateTime value) {
+ complete();
+ if (value == null) {
+ _removeChildren(UPDATED, false);
+ return null;
+ }
+ DateTime dte = getUpdatedElement();
+ if (dte != null) {
+ dte.setValue(value);
+ return dte;
+ } else {
+ FOMFactory fomfactory = (FOMFactory)factory;
+ DateTime dt = fomfactory.newUpdated(this);
+ dt.setValue(value);
+ return dt;
+ }
+ }
+
+ public DateTime setUpdated(Date value) {
+ return setUpdated((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public DateTime setUpdated(String value) {
+ return setUpdated((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public DateTime getEditedElement() {
+ DateTime dt = (DateTime)getFirstChildWithName(EDITED);
+ if (dt == null)
+ dt = (DateTime)getFirstChildWithName(PRE_RFC_EDITED);
+ return dt;
+ }
+
+ public void setEditedElement(DateTime updated) {
+ complete();
+ declareNamespace(APP_NS, "app");
+ _removeChildren(PRE_RFC_EDITED, false);
+ if (updated != null)
+ _setChild(EDITED, (OMElement)updated);
+ else
+ _removeChildren(EDITED, false);
+ }
+
+ public Date getEdited() {
+ DateTime dte = getEditedElement();
+ return (dte != null) ? dte.getDate() : null;
+ }
+
+ private DateTime setEdited(org.apache.abdera2.common.date.DateTime value) {
+ complete();
+ declareNamespace(APP_NS, "app");
+ if (value == null) {
+ _removeChildren(PRE_RFC_EDITED, false);
+ _removeChildren(EDITED, false);
+ return null;
+ }
+ DateTime dte = getEditedElement();
+ if (dte != null) {
+ dte.setValue(value);
+ return dte;
+ } else {
+ FOMFactory fomfactory = (FOMFactory)factory;
+ DateTime dt = fomfactory.newEdited(this);
+ dt.setValue(value);
+ return dt;
+ }
+ }
+
+ public DateTime setEdited(Date value) {
+ return setEdited((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public DateTime setEdited(String value) {
+ return setUpdated((value != null) ? org.apache.abdera2.common.date.DateTime.valueOf(value) : null);
+ }
+
+ public Control getControl(boolean create) {
+ Control control = getControl();
+ if (control == null && create) {
+ control = getFactory().newControl();
+ setControl(control);
+ }
+ return control;
+ }
+
+ public Control getControl() {
+ Control control = (Control)getFirstChildWithName(CONTROL);
+ if (control == null)
+ control = (Control)getFirstChildWithName(PRE_RFC_CONTROL);
+ return control;
+ }
+
+ public Entry setControl(Control control) {
+ complete();
+ _removeChildren(PRE_RFC_CONTROL, true);
+ if (control != null)
+ _setChild(CONTROL, (OMElement)control);
+ else
+ _removeChildren(CONTROL, false);
+ return this;
+ }
+
+ public Link getLink(String rel) {
+ List<Link> links = getLinks(rel);
+ Link link = null;
+ if (links.size() > 0)
+ link = links.get(0);
+ return link;
+ }
+
+ public Link getAlternateLink() {
+ return getLink(Link.REL_ALTERNATE);
+ }
+
+ public Link getEnclosureLink() {
+ return getLink(Link.REL_ENCLOSURE);
+ }
+
+ public Link getEditLink() {
+ return getLink(Link.REL_EDIT);
+ }
+
+ public Link getSelfLink() {
+ return getLink(Link.REL_SELF);
+ }
+
+ public Link getEditMediaLink() {
+ return getLink(Link.REL_EDIT_MEDIA);
+ }
+
+ public IRI getLinkResolvedHref(String rel) {
+ Link link = getLink(rel);
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public IRI getAlternateLinkResolvedHref() {
+ Link link = getAlternateLink();
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public IRI getEnclosureLinkResolvedHref() {
+ Link link = getEnclosureLink();
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public IRI getEditLinkResolvedHref() {
+ Link link = getEditLink();
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public IRI getEditMediaLinkResolvedHref() {
+ Link link = getEditMediaLink();
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public IRI getSelfLinkResolvedHref() {
+ Link link = getSelfLink();
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public String getContent() {
+ Content content = getContentElement();
+ return (content != null) ? content.getValue() : null;
+ }
+
+ public InputStream getContentStream() throws IOException {
+ Content content = getContentElement();
+ DataHandler dh = content.getDataHandler();
+ return dh.getInputStream();
+ }
+
+ public IRI getContentSrc() {
+ Content content = getContentElement();
+ return (content != null) ? content.getResolvedSrc() : null;
+ }
+
+ public Type getContentType() {
+ Content content = getContentElement();
+ return (content != null) ? content.getContentType() : null;
+ }
+
+ public Text.Type getRightsType() {
+ Text text = getRightsElement();
+ return (text != null) ? text.getTextType() : null;
+ }
+
+ public Text.Type getSummaryType() {
+ Text text = getSummaryElement();
+ return (text != null) ? text.getTextType() : null;
+ }
+
+ public Text.Type getTitleType() {
+ Text text = getTitleElement();
+ return (text != null) ? text.getTextType() : null;
+ }
+
+ public MimeType getContentMimeType() {
+ Content content = getContentElement();
+ return (content != null) ? content.getMimeType() : null;
+ }
+
+ public Link getAlternateLink(String type, String hreflang) {
+ return selectLink(getLinks(Link.REL_ALTERNATE), type, hreflang);
+ }
+
+ public IRI getAlternateLinkResolvedHref(String type, String hreflang) {
+ Link link = getAlternateLink(type, hreflang);
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public Link getEditMediaLink(String type, String hreflang) {
+ return selectLink(getLinks(Link.REL_EDIT_MEDIA), type, hreflang);
+ }
+
+ public IRI getEditMediaLinkResolvedHref(String type, String hreflang) {
+ Link link = getEditMediaLink(type, hreflang);
+ return (link != null) ? link.getResolvedHref() : null;
+ }
+
+ public Entry setDraft(boolean draft) {
+ complete();
+ Control control = getControl();
+ if (control == null && draft) {
+ control = ((FOMFactory)factory).newControl(this);
+ }
+ if (control != null)
+ control.setDraft(draft);
+ return this;
+ }
+
+ /**
+ * Returns true if this entry is a draft
+ */
+ public boolean isDraft() {
+ Control control = getControl();
+ return (control != null) ? control.isDraft() : false;
+ }
+
+ public Control addControl() {
+ complete();
+ Control control = getControl();
+ if (control == null) {
+ control = ((FOMFactory)factory).newControl(this);
+ }
+ return control;
+ }
+
+ public List<Person> getAuthors(Selector selector) {
+ return _getChildrenAsSet(AUTHOR,selector);
+ }
+
+ public List<Category> getCategories(Selector selector) {
+ return _getChildrenAsSet(CATEGORY,selector);
+ }
+
+ public List<Person> getContributors(Selector selector) {
+ return _getChildrenAsSet(CONTRIBUTOR,selector);
+ }
+
+ public List<Link> getLinks(Selector selector) {
+ return _getChildrenAsSet(LINK,selector);
+ }
+}
Propchange: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMEntry.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMException.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMException.java?rev=1173209&view=auto
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMException.java (added)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMException.java Tue Sep 20 15:56:46 2011
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera2.parser.axiom;
+
+import org.apache.abdera2.parser.ParseException;
+
+public class FOMException extends ParseException {
+
+ private static final long serialVersionUID = 7631230122836829559L;
+
+ public FOMException() {
+ super();
+ }
+
+ public FOMException(String message) {
+ super(message);
+ }
+
+ public FOMException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public FOMException(Throwable cause) {
+ super(cause);
+ }
+
+}
Propchange: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMExtensibleElement.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMExtensibleElement.java?rev=1173209&view=auto
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMExtensibleElement.java (added)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMExtensibleElement.java Tue Sep 20 15:56:46 2011
@@ -0,0 +1,232 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera2.parser.axiom;
+
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera2.common.anno.AnnoUtil;
+import org.apache.abdera2.common.selector.Selector;
+import org.apache.abdera2.model.Element;
+import org.apache.abdera2.model.ElementIteratorWrapper;
+import org.apache.abdera2.model.ElementWrapper;
+import org.apache.abdera2.model.ExtensibleElement;
+import org.apache.abdera2.model.ExtensionIterator;
+import org.apache.abdera2.model.ElementList;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+@SuppressWarnings("unchecked")
+public class FOMExtensibleElement extends FOMElement implements ExtensibleElement {
+
+ private static final long serialVersionUID = -1652430686161947531L;
+
+ public FOMExtensibleElement(QName qname) {
+ super(qname);
+ }
+
+ protected FOMExtensibleElement(String name, OMNamespace namespace, OMContainer parent, OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ protected FOMExtensibleElement(QName qname, OMContainer parent, OMFactory factory) throws OMException {
+ super(qname, parent, factory);
+ }
+
+ protected FOMExtensibleElement(QName qname, OMContainer parent, OMFactory factory, OMXMLParserWrapper builder)
+ throws OMException {
+ super(qname, parent, factory, builder);
+ }
+
+ public List<Element> getExtensions() {
+ return new ElementList<Element>(new ExtensionIterator(this));
+ }
+
+ public List<Element> getExtensions(String uri) {
+ return new ElementList<Element>(new ExtensionIterator(this, uri));
+ }
+
+ public <T extends Element> List<T> getExtensions(QName qname) {
+ return new ElementList<T>(new ElementIteratorWrapper<T>(getFactory(),getChildrenWithName(qname)));
+ }
+
+ public <T extends Element> T getExtension(QName qname) {
+ FOMFactory factory = (FOMFactory)getFactory();
+ T t = (T)this.getFirstChildWithName(qname);
+ return (T)((t != null) ? factory.getElementWrapper(t) : null);
+ }
+
+ public <T extends ExtensibleElement> T addExtension(Element extension) {
+ complete();
+ if (extension instanceof ElementWrapper) {
+ ElementWrapper wrapper = (ElementWrapper)extension;
+ extension = wrapper.getInternal();
+ }
+ QName qname = extension.getQName();
+ String prefix = qname.getPrefix();
+ declareIfNecessary(qname.getNamespaceURI(), prefix);
+ addChild((OMElement)extension);
+ return (T)this;
+ }
+
+ public <T extends Element> T addExtension(QName qname) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)factory;
+ String prefix = qname.getPrefix();
+ declareIfNecessary(qname.getNamespaceURI(), prefix);
+ return (T)fomfactory.newExtensionElement(qname, this);
+ }
+
+ public <T extends Element> T addExtension(String namespace, String localpart, String prefix) {
+ complete();
+ declareIfNecessary(namespace, prefix);
+ return (prefix != null) ? (T)addExtension(new QName(namespace, localpart, prefix))
+ : (T)addExtension(new QName(namespace, localpart, ""));
+ }
+
+ public Element addSimpleExtension(QName qname, String value) {
+ complete();
+ FOMFactory fomfactory = (FOMFactory)factory;
+ Element el = fomfactory.newElement(qname, this);
+ el.setText(value);
+ String prefix = qname.getPrefix();
+ declareIfNecessary(qname.getNamespaceURI(), prefix);
+ return el;
+ }
+
+ public Element addSimpleExtension(String namespace, String localPart, String prefix, String value) {
+ complete();
+ declareIfNecessary(namespace, prefix);
+ return addSimpleExtension((prefix != null) ? new QName(namespace, localPart, prefix) : new QName(namespace,
+ localPart),
+ value);
+ }
+
+ public String getSimpleExtension(QName qname) {
+ Element el = getExtension(qname);
+ return (el != null) ? el.getText() : null;
+ }
+
+ public String getSimpleExtension(String namespace, String localPart, String prefix) {
+ return getSimpleExtension(new QName(namespace, localPart, prefix));
+ }
+
+ public void addExtensions(List<Element> extensions) {
+ for (Element e : extensions) {
+ addExtension(e);
+ }
+ }
+
+ public <T extends Element> T getExtension(Class<T> _class) {
+ QName qname = AnnoUtil.getQName(_class);
+ if (qname != null) {
+ return getExtension(qname);
+ } else {
+ T t = null;
+ List<Element> extensions = getExtensions();
+ for (Element ext : extensions) {
+ if (_class.isAssignableFrom(ext.getClass())) {
+ t = (T)ext;
+ break;
+ }
+ }
+ return t;
+ }
+ }
+
+ private Element getInternal(Element element) {
+ if (element instanceof ElementWrapper) {
+ ElementWrapper wrapper = (ElementWrapper)element;
+ element = wrapper.getInternal();
+ }
+ return element;
+ }
+
+ public <T extends ExtensibleElement> T addExtension(Element extension, Element before) {
+ complete();
+ extension = getInternal(extension);
+ before = getInternal(before);
+ if (before instanceof ElementWrapper) {
+ ElementWrapper wrapper = (ElementWrapper)before;
+ before = wrapper.getInternal();
+ }
+ if (before == null) {
+ addExtension(extension);
+ } else {
+ extension.setParentElement(this);
+ ((OMElement)before).insertSiblingBefore((OMElement)extension);
+ }
+ return (T)this;
+ }
+
+ public <T extends Element> T addExtension(QName qname, QName before) {
+ complete();
+ OMElement el = getFirstChildWithName(before);
+ T element = (T)getFactory().newElement(qname);
+ if (el == null) {
+ addExtension(element);
+ } else {
+ element.setParentElement(this);
+ el.insertSiblingBefore((OMElement)getInternal(element));
+ }
+ return (T)element;
+ }
+
+ public List<Element> getExtensions(Selector selector) {
+ return new ElementList<Element>(new ExtensionIterator(this,selector));
+ }
+
+ public <T extends Element> List<T> getExtensions(QName qname, Selector selector) {
+ return new ElementList<T>(new ElementIteratorWrapper<T>(getFactory(),getChildrenWithName(qname),selector));
+ }
+
+ public <T extends Element> List<T> getExtensions(Class<T> _class) {
+ QName qname = AnnoUtil.getQName(_class);
+ if (qname == null)
+ throw new IllegalArgumentException();
+ return getExtensions(qname);
+ }
+
+ public <T extends Element> List<T> getExtensions(Class<T> _class,
+ Selector selector) {
+ QName qname = AnnoUtil.getQName(_class);
+ if (qname == null)
+ throw new IllegalArgumentException();
+ return getExtensions(qname,selector);
+ }
+
+ public <T extends Element> T addExtension(Class<T> _class) {
+ QName qname = AnnoUtil.getQName(_class);
+ if (qname == null)
+ throw new IllegalArgumentException();
+ return addExtension(qname);
+ }
+
+ public <T extends Element> T addExtension(Class<T> _class, QName before) {
+ QName qname = AnnoUtil.getQName(_class);
+ if (qname == null)
+ throw new IllegalArgumentException();
+ return addExtension(_class,before);
+ }
+}
Propchange: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMExtensibleElement.java
------------------------------------------------------------------------------
svn:mime-type = text/plain