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 2006/06/16 22:10:56 UTC
svn commit: r414893 [6/11] - in /incubator/abdera/java/trunk: ./ build/
core/ core/src/ core/src/main/ core/src/main/java/ core/src/main/java/org/
core/src/main/java/org/apache/ core/src/main/java/org/apache/abdera/
core/src/main/java/org/apache/abdera...
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,446 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.filter.ParseFilter;
+import org.apache.abdera.filter.TextFilter;
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Text;
+import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.util.Constants;
+import org.apache.axiom.om.OMConstants;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.impl.OMContainerEx;
+import org.apache.axiom.om.impl.OMNodeEx;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.util.OMSerializerUtil;
+
+
+public class FOMBuilder
+ extends StAXOMBuilder
+ implements Constants {
+
+ private FOMFactory fomfactory = null;
+ private Document fomDocument = null;
+ private ParserOptions parserOptions = null;
+ private int depth = 0;
+ private int depthInSkipElement = 0;
+
+ public FOMBuilder(
+ FOMFactory factory,
+ XMLStreamReader parser,
+ ParserOptions parserOptions) {
+ super(factory, parser);
+ this.parserOptions = parserOptions;
+ fomfactory = factory;
+ }
+
+ public FOMBuilder(
+ FOMFactory factory,
+ XMLStreamReader parser) {
+ super(factory, parser);
+ document = factory.createOMDocument(this);
+ fomfactory = factory;
+ }
+
+ public FOMBuilder(
+ String filePath,
+ ParserOptions parserOptions)
+ throws XMLStreamException,
+ FileNotFoundException {
+ super(filePath);
+ this.parserOptions = parserOptions;
+ fomfactory = getFomFactory();
+ setOMBuilderFactory(fomfactory);
+ }
+
+ public FOMBuilder(
+ FOMFactory factory,
+ String filePath,
+ ParserOptions parserOptions)
+ throws XMLStreamException,
+ FileNotFoundException {
+ super(filePath);
+ this.parserOptions = parserOptions;
+ fomfactory = factory;
+ setOMBuilderFactory(fomfactory);
+ }
+
+ public FOMBuilder(
+ InputStream inStream,
+ ParserOptions parserOptions)
+ throws XMLStreamException {
+ super(inStream);
+ this.parserOptions = parserOptions;
+ fomfactory = getFomFactory();
+ setOMBuilderFactory(fomfactory);
+ }
+
+ public FOMBuilder(
+ FOMFactory factory,
+ InputStream inStream,
+ ParserOptions parserOptions)
+ throws XMLStreamException {
+ super(inStream);
+ this.parserOptions = parserOptions;
+ fomfactory = factory;
+ setOMBuilderFactory(fomfactory);
+ }
+
+ public FOMBuilder(
+ XMLStreamReader parser,
+ ParserOptions parserOptions) {
+ super(parser);
+ this.parserOptions = parserOptions;
+ fomfactory = getFomFactory();
+ setOMBuilderFactory(fomfactory);
+ }
+
+ public FOMBuilder(
+ XMLStreamReader parser,
+ FOMFactory factory,
+ ParserOptions parserOptions) {
+ super(factory, parser);
+ this.parserOptions = parserOptions;
+ fomfactory = factory;
+ }
+
+ private FOMFactory getFomFactory() {
+ FOMFactory factory =
+ (parserOptions != null && parserOptions.getFactory() != null) ?
+ (FOMFactory)parserOptions.getFactory() : null;
+ if (factory == null)
+ factory = (Factory.INSTANCE instanceof FOMFactory) ?
+ (FOMFactory)Factory.INSTANCE : new FOMFactory();
+ return factory;
+ }
+
+ public ParserOptions getParserOptions() {
+ return parserOptions;
+ }
+
+ @Override
+ protected OMNode createOMElement() throws OMException {
+ depth++;
+ OMElement node;
+ String elementName = parser.getLocalName();
+ if (lastNode == null) {
+ node = constructNode(null, elementName);
+ } else if (lastNode.isComplete()) {
+ node =
+ constructNode(
+ (OMContainer) lastNode.getParent(),
+ elementName);
+ if (node != null) {
+ ((OMNodeEx) lastNode).setNextOMSibling(node);
+ ((OMNodeEx) node).setPreviousOMSibling(lastNode);
+ }
+ } else {
+ OMElement e = (OMElement) lastNode;
+ node = constructNode((OMElement) lastNode, elementName);
+ e.setFirstChild(node);
+ }
+ return node;
+ }
+
+ protected Text.Type getTextType() {
+ Text.Type ttype = Text.Type.TEXT;
+ String type = parser.getAttributeValue(null, LN_TYPE);
+ if (type != null) {
+ ttype = Text.Type.typeFromString(type);
+ if (ttype == null)
+ throw new FOMUnsupportedTextTypeException(type);
+ }
+ return ttype;
+ }
+
+ protected Content.Type getContentType() {
+ Content.Type ctype = Content.Type.TEXT;
+ String type = parser.getAttributeValue(null, LN_TYPE);
+ String src = parser.getAttributeValue(null, LN_SRC);
+ if (type != null) {
+ ctype = Content.Type.typeFromString(type);
+ if (ctype == null)
+ throw new FOMUnsupportedContentTypeException(type);
+ } else if (type == null && src != null) {
+ ctype = Content.Type.MEDIA;
+ }
+ return ctype;
+ }
+
+ private boolean isAcceptableToParse(QName qname, boolean attribute) {
+ if (parserOptions == null) return true;
+ ParseFilter filter = parserOptions.getParseFilter();
+ return (filter != null) ?
+ (!attribute) ?
+ filter.acceptable(qname) :
+ filter.acceptableAttribute(parser.getName(), qname):
+ true;
+ }
+
+ private OMNode applyTextFilter(int type) {
+ if (parserOptions != null) {
+ TextFilter filter = parserOptions.getTextFilter();
+ if (filter != null) {
+ String value = parser.getText();
+ if (!lastNode.isComplete())
+ value = filter.filterText(value, (Element)lastNode);
+ return createOMText(value, type);
+ }
+ }
+ return createOMText(type);
+ }
+
+ private String applyAttributeTextFilter(String value, QName attribute, Element parent) {
+ if (parserOptions != null) {
+ TextFilter filter = parserOptions.getTextFilter();
+ if (filter != null) {
+ return filter.filterAttributeText(value, attribute, parent);
+ }
+ }
+ return value;
+ }
+
+ private int getNextElementToParse()
+ throws XMLStreamException{
+ int token = parser.next();
+ if (depthInSkipElement == 0 && token != XMLStreamConstants.START_ELEMENT){
+ return token;
+ } else if (token == XMLStreamConstants.START_ELEMENT && isAcceptableToParse(parser.getName(), false) && depthInSkipElement == 0){
+ return token;
+ } else if (token == XMLStreamConstants.START_ELEMENT ){
+ depthInSkipElement++;
+ } else if (token == XMLStreamConstants.END_ELEMENT ){ // otherwise skip like crazy
+ depthInSkipElement--;
+ }
+ return getNextElementToParse();
+ }
+
+ /**
+ * Method next.
+ *
+ * @return Returns int.
+ * @throws OMException
+ */
+ public int next() throws OMException {
+ try {
+ if (done) {
+ throw new OMException();
+ }
+ int token = getNextElementToParse();
+ if (!cache) {
+ return token;
+ }
+ switch (token) {
+ case XMLStreamConstants.START_ELEMENT:
+ lastNode = createOMElement();
+ break;
+ case XMLStreamConstants.START_DOCUMENT:
+ document.setXMLVersion(
+ parser.getVersion() != null ?
+ parser.getVersion() : "1.0");
+ document.setCharsetEncoding(
+ parser.getEncoding() != null ?
+ parser.getEncoding() : "utf-8");
+ document.setStandalone(
+ parser.isStandalone() ? YES : NO);
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ lastNode = applyTextFilter(XMLStreamConstants.CHARACTERS);
+ break;
+ case XMLStreamConstants.CDATA:
+ lastNode = applyTextFilter(XMLStreamConstants.CDATA);
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ endElement();
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ done = true;
+ ((OMContainerEx) this.document).setComplete(true);
+ break;
+ case XMLStreamConstants.SPACE:
+ lastNode = createOMText(XMLStreamConstants.SPACE);
+ break;
+ case XMLStreamConstants.COMMENT:
+ createComment();
+ break;
+ case XMLStreamConstants.DTD:
+ createDTD();
+ break;
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ createPI();
+ break;
+ case XMLStreamConstants.ENTITY_REFERENCE:
+ lastNode = createOMText(XMLStreamConstants.ENTITY_REFERENCE);
+ break;
+ default :
+ throw new OMException();
+ }
+ return token;
+ } catch (OMException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ private void initDocument(String name) {
+ fomDocument = fomfactory.newDocument();
+ String enc = parser.getCharacterEncodingScheme();
+ getDocument().setCharsetEncoding(enc != null ? enc : "utf-8");
+ getDocument().setXMLVersion(
+ parser.getVersion() != null ?
+ parser.getVersion() : "1.0");
+ }
+
+ protected OMElement constructNode(OMContainer parent, String name) {
+ OMElement element = null;
+ if (fomDocument == null) {
+ initDocument(name);
+ parent = (OMContainer) fomDocument;
+ }
+ QName qname = parser.getName();
+ element = fomfactory.createElement(qname, parent, this);
+ if (element == null) {
+ element = new FOMExtensionElement(qname, parent, fomfactory, this);
+ }
+ if (element != null) {
+ this.processNamespaceData(element);
+ processAttributes(element);
+ }
+ return element;
+ }
+
+ @Override
+ protected void processAttributes(OMElement node) {
+ int attribCount = parser.getAttributeCount();
+ for (int i = 0; i < attribCount; i++) {
+ QName attr = parser.getAttributeName(i);
+ if (isAcceptableToParse(attr, true)) {
+ String uri = parser.getAttributeNamespace(i);
+ String prefix = parser.getAttributePrefix(i);
+ OMNamespace namespace = null;
+ if (uri != null && uri.length() > 0) {
+ namespace = node.findNamespace(uri, prefix);
+ if (namespace == null) {
+ if (prefix == null || "".equals(prefix)) {
+ prefix = OMSerializerUtil.getNextNSPrefix();
+ }
+ namespace = node.declareNamespace(uri, prefix);
+ }
+ }
+ String value = applyAttributeTextFilter(
+ parser.getAttributeValue(i),
+ attr, (Element)node);
+ node.addAttribute(parser.getAttributeLocalName(i),
+ value, namespace);
+ }
+ }
+}
+
+ @Override
+ protected void endElement() {
+ if (lastNode != null && lastNode.isComplete()) {
+ OMElement parent = (OMElement) lastNode.getParent();
+ ((OMNodeEx) parent).setComplete(true);
+ lastNode = parent;
+ } else {
+ OMNode e = lastNode;
+ if (e != null)
+ ((OMNodeEx) e).setComplete(true);
+ }
+ depth--;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Element>Document<T> getFomDocument() {
+ while ((fomDocument == null) && !done) {
+ next();
+ }
+ return fomDocument;
+ }
+
+ public OMDocument getDocument() {
+ return (OMDocument) getFomDocument();
+ }
+
+ public FOMFactory getFactory() {
+ return fomfactory;
+ }
+
+ /**
+ * Method createOMText.
+ *
+ * @return Returns OMNode.
+ * @throws OMException
+ */
+ protected OMNode createOMText(String value, int textType) throws OMException {
+ OMNode node = null;
+ if (lastNode == null) {
+ return null;
+ } else if (!lastNode.isComplete()) {
+ node = createOMText(value, (OMElement) lastNode, textType);
+ } else {
+ OMContainer parent = lastNode.getParent();
+ if (!(parent instanceof OMDocument)) {
+ node = createOMText(value, (OMElement) parent, textType);
+ }
+ }
+ return node;
+ }
+
+ /**
+ * This method will check whether the text can be optimizable using IS_BINARY flag.
+ * If that is set then we try to get the data handler.
+ *
+ * @param omElement
+ * @param textType
+ * @return omNode
+ */
+ private OMNode createOMText(String value, OMElement omElement, int textType) {
+ try {
+ if (isDataHandlerAware && Boolean.TRUE == parser.getProperty(OMConstants.IS_BINARY)) {
+ Object dataHandler = parser.getProperty(OMConstants.DATA_HANDLER);
+ OMText text = omfactory.createOMText(dataHandler, true);
+ omElement.addChild(text);
+ return text;
+ } else {
+ return omfactory.createOMText(omElement, value, textType);
+ }
+ } catch (IllegalArgumentException e) {
+ return omfactory.createOMText(omElement, value, textType);
+ }
+ }
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCategory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCategory.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCategory.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCategory.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,116 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Category;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+
+public class FOMCategory
+ extends FOMExtensibleElement
+ implements Category {
+
+ private static final long serialVersionUID = -4313042828936786803L;
+
+ public FOMCategory(
+ String name,
+ OMNamespace namespace,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMCategory(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory) {
+ super(qname, parent, factory);
+ }
+
+ public FOMCategory(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(qname, parent, factory, builder);
+ }
+
+ public FOMCategory(
+ OMContainer parent,
+ OMFactory factory) {
+ super(CATEGORY, parent, factory);
+ }
+
+ public FOMCategory(
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(CATEGORY, parent, factory, builder);
+ }
+
+ public String getTerm() {
+ return _getAttributeValue(TERM);
+ }
+
+ public void setTerm(String term) {
+ _setAttributeValue(TERM, term);
+ }
+
+ public URI getScheme() throws URISyntaxException {
+ String value = _getAttributeValue(SCHEME);
+ return (value != null) ? new URI(value) : null;
+ }
+
+ public void setScheme(URI scheme) {
+ _setAttributeValue(SCHEME, (scheme != null) ? scheme.toString() : null);
+ }
+
+ public void setScheme(String scheme) throws URISyntaxException {
+ setScheme((scheme != null) ? new URI(scheme) : null);
+ }
+
+ public String getLabel() {
+ return _getAttributeValue(LABEL);
+ }
+
+ public void setLabel(String label) {
+ _setAttributeValue(LABEL, label);
+ }
+
+ public String getValue() {
+ return getText();
+ }
+
+ public void setValue(String value) {
+ if (value != null)
+ setText(value);
+ else
+ _removeAllChildren();
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,142 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.activation.MimeType;
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Collection;
+import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+
+public class FOMCollection
+ extends FOMExtensibleElement
+ implements Collection {
+
+ private static final long serialVersionUID = -5291734055253987136L;
+
+ public FOMCollection(
+ String name,
+ OMNamespace namespace,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMCollection(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory) {
+ super(qname, parent, factory);
+ }
+
+ public FOMCollection(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(qname, parent, factory, builder);
+ }
+
+ public FOMCollection(
+ OMContainer parent,
+ OMFactory factory) {
+ super(COLLECTION, parent, factory);
+ }
+
+ public FOMCollection(
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(COLLECTION, parent, factory, builder);
+ }
+
+ public String getTitle() {
+ return _getAttributeValue(ATITLE);
+ }
+
+ public void setTitle(String title) {
+ if (title != null)
+ _setAttributeValue(ATITLE, title);
+ else
+ _removeAttribute(ATITLE);
+ }
+
+ public URI getHref() throws URISyntaxException {
+ return _getUriValue(_getAttributeValue(HREF));
+ }
+
+ public URI getResolvedHref() throws URISyntaxException {
+ return _resolve(getResolvedBaseUri(), getHref());
+ }
+
+ public void setHref(URI href) {
+ if (href != null)
+ _setAttributeValue(HREF, _getStringValue(href));
+ else
+ _removeAttribute(HREF);
+ }
+
+ public void setHref(String href) throws URISyntaxException {
+ setHref((href != null) ? new URI(href) : null);
+ }
+
+ public String[] getAccept(){
+ String accept = _getElementValue(ACCEPT);
+ String[] list = accept.split(",");
+ for (int n = 0; n < list.length; n++) {
+ list[n] = list[n].trim();
+ }
+ return list;
+ }
+
+ public void setAccept(String[] mediaRanges) {
+ if (mediaRanges != null) {
+ String value = "";
+ for (String type : mediaRanges) {
+ if (value.length() > 0) value+=",";
+ value += type;
+ }
+ _setElementValue(ACCEPT, value);
+ } else {
+ _removeElement(ACCEPT, false);
+ }
+ }
+
+ public boolean accepts(String mediaType) {
+ String[] accept = getAccept();
+ for (String a : accept) {
+ if (MimeTypeHelper.isMatch(a, mediaType)) return true;
+ }
+ return false;
+ }
+
+ public boolean accepts(MimeType mediaType) {
+ return accepts(mediaType.toString());
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMContent.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMContent.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMContent.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMContent.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,246 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.activation.DataHandler;
+import javax.activation.MimeType;
+import javax.activation.URLDataSource;
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Div;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensionElement;
+import org.apache.abdera.util.Constants;
+import org.apache.axiom.attachments.DataHandlerUtils;
+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;
+
+
+public class FOMContent
+ extends FOMExtensibleElement
+ implements Content {
+
+ private static final long serialVersionUID = -5499917654824498563L;
+ protected Type type = null;
+
+ public FOMContent(
+ String name,
+ OMNamespace namespace,
+ Type type,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMContent(
+ QName qname,
+ Type type,
+ OMContainer parent,
+ OMFactory factory) {
+ super(qname, parent, factory);
+ init(type);
+ }
+
+ public FOMContent(
+ QName qname,
+ Type type,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(qname, parent, factory, builder);
+ init(type);
+ }
+
+ public FOMContent(
+ Type type,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(CONTENT, parent, factory);
+ init(type);
+ }
+
+ public FOMContent(
+ Type type,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(CONTENT, parent, factory, builder);
+ init(type);
+ }
+
+ private void init(Type type) {
+ this.type = type;
+ if (Type.TEXT.equals(type))
+ _setAttributeValue(TYPE, "text");
+ else if (Type.HTML.equals(type))
+ _setAttributeValue(TYPE, "html");
+ else if (Type.XHTML.equals(type))
+ _setAttributeValue(TYPE, "xhtml");
+ }
+
+ public final Type getContentType() {
+ return type;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends ExtensionElement> T getValueElement() {
+ return (T)this.getFirstElement();
+ }
+
+ public <T extends ExtensionElement> void setValueElement(T value) {
+ if (value != null) {
+ if (this.getFirstElement() != null)
+ this.getFirstElement().discard();
+ this.setFirstChild((OMElement)value);
+ } else {
+ _removeAllChildren();
+ }
+ }
+
+ public MimeType getMimeType() {
+ MimeType type = null;
+ String mimeType = _getAttributeValue(TYPE);
+ if (mimeType != null) {
+ try {
+ type = new MimeType(mimeType);
+ } catch (Exception e) {}
+ }
+ return type;
+ }
+
+ public void setMimeType(MimeType type) {
+ if (type != null)
+ _setAttributeValue(TYPE, type.toString());
+ else
+ _removeAttribute(TYPE);
+ }
+
+ public void setMimeType(String type) {
+ _setAttributeValue(TYPE, type);
+ }
+
+ public URI getSrc() throws URISyntaxException {
+ return _getUriValue(_getAttributeValue(SRC));
+ }
+
+ public URI getResolvedSrc() throws URISyntaxException {
+ return _resolve(getResolvedBaseUri(), getSrc());
+ }
+
+ public void setSrc(URI src) {
+ if (src != null)
+ _setAttributeValue(SRC, _getStringValue(src));
+ else
+ _removeAttribute(SRC);
+ }
+
+ public void setSrc(String src) throws URISyntaxException {
+ setSrc((src != null) ? new URI(src) : null);
+ }
+
+ public DataHandler getDataHandler() {
+ if (!Type.MEDIA.equals(type))
+ throw new UnsupportedOperationException(
+ "Only supported on media content entries");
+ MimeType type = getMimeType();
+ java.net.URL src = null;
+ try {
+ src = getSrc().toURL();
+ } catch (Exception e) {}
+ DataHandler dh = null;
+ if (src == null) {
+ dh = (DataHandler)DataHandlerUtils.getDataHandlerFromText(
+ getText(), (type != null) ? type.toString() : null);
+ } else {
+ dh = new DataHandler(new URLDataSource(src));
+ }
+ return dh;
+ }
+
+ public void setDataHandler(DataHandler dataHandler) {
+ if (!Type.MEDIA.equals(type)) throw new IllegalArgumentException();
+ if (dataHandler.getContentType() != null) {
+ try {
+ setMimeType(new MimeType(dataHandler.getContentType()));
+ } catch (Exception e) {}
+ }
+ _removeAllChildren();
+ addChild(factory.createOMText(dataHandler, true));
+ }
+
+ public String getValue() {
+ String val = null;
+ if (Type.TEXT.equals(type)) {
+ val = getText();
+ } else if (Type.HTML.equals(type)) {
+ val = getText();
+ } else if (Type.XHTML.equals(type)) {
+ val = this.getFirstChildWithName(Constants.DIV).toString();
+ } else if (Type.XML.equals(type)) {
+ val = this.getFirstElement().toString();
+ } else if (Type.MEDIA.equals(type)) {
+ val = getText();
+ }
+ return val;
+ }
+
+ public void setValue(String value) {
+ if (value != null) setSrc((URI)null);
+ if (value != null) {
+ if (Type.TEXT.equals(type)) {
+ _removeAllChildren();
+ setText(value);
+ } else if (Type.HTML.equals(type)) {
+ _removeAllChildren();
+ setText(value);
+ } else if (Type.XHTML.equals(type)) {
+ URI baseUri = null;
+ try {
+ baseUri = getResolvedBaseUri();
+ } catch (Exception e) {}
+ Element element = _parse(value, baseUri);
+ if (element != null && element instanceof Div)
+ setValueElement((Div)element);
+ } else if (Type.XML.equals(type)) {
+ URI baseUri = null;
+ try {
+ baseUri = getResolvedBaseUri();
+ } catch (Exception e) {}
+ Element element = _parse(value, baseUri);
+ if (element != null && element instanceof ExtensionElement)
+ setValueElement((ExtensionElement)element);
+ } else if (Type.MEDIA.equals(type)) {
+ _removeAllChildren();
+ setText(value);
+ }
+ } else {
+ _removeAllChildren();
+ }
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMControl.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMControl.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMControl.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMControl.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,91 @@
+/*
+* 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.abdera.parser.stax;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Control;
+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;
+
+
+public class FOMControl
+ extends FOMExtensibleElement
+ implements Control {
+
+ private static final long serialVersionUID = -3816493378953555206L;
+
+ public FOMControl(
+ String name,
+ OMNamespace namespace,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMControl(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory) {
+ super(qname, parent, factory);
+ }
+
+ public FOMControl(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(qname, parent, factory, builder);
+ }
+
+ public FOMControl(
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder)
+ throws OMException {
+ super(CONTROL, parent, factory, builder);
+ }
+
+ public FOMControl(
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(CONTROL, parent, factory);
+ }
+
+ public boolean isDraft() {
+ String value = _getElementValue(DRAFT);
+ return (value != null && YES.equalsIgnoreCase(value));
+ }
+
+ public void setDraft(boolean draft) {
+ _setElementValue(DRAFT, (draft) ? YES:NO);
+ }
+
+ public void unsetDraft() {
+ OMElement el = getFirstChildWithName(DRAFT);
+ if (el != null)
+ el.discard();
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDateTime.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDateTime.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDateTime.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDateTime.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,127 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.AtomDate;
+import org.apache.abdera.model.DateTime;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+
+public class FOMDateTime
+ extends FOMExtensionElement
+ implements DateTime {
+
+ private static final long serialVersionUID = -6611503566172011733L;
+
+ public FOMDateTime(
+ String name,
+ OMNamespace namespace,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMDateTime(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(qname, parent, factory);
+ }
+
+ public FOMDateTime(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder)
+ throws OMException {
+ super(qname, parent, factory, builder);
+ }
+
+ public AtomDate getValue() {
+ AtomDate value = null;
+ String v = getText();
+ if (v != null) {
+ value = AtomDate.valueOf(v);
+ }
+ return value;
+ }
+
+ public void setValue(AtomDate dateTime) {
+ if (dateTime != null)
+ setText((dateTime != null) ? dateTime.getValue() : null);
+ else
+ _removeAllChildren();
+ }
+
+ public void setDate(Date date) {
+ if (date != null)
+ setText((date != null) ? AtomDate.valueOf(date).getValue() : null);
+ else
+ _removeAllChildren();
+ }
+
+ public void setCalendar(Calendar date) {
+ if (date != null)
+ setText((date != null) ? AtomDate.valueOf(date).getValue() : null);
+ else
+ _removeAllChildren();
+ }
+
+ public void setTime(long date) {
+ setText(AtomDate.valueOf(date).getValue());
+ }
+
+ public void setString(String date) {
+ if (date != null)
+ setText((date != null) ? AtomDate.valueOf(date).getValue() : null);
+ else
+ _removeAllChildren();
+ }
+
+ public Date getDate() {
+ AtomDate ad = getValue();
+ return (ad != null) ? ad.getDate() : null;
+ }
+
+ public Calendar getCalendar() {
+ AtomDate ad = getValue();
+ return (ad != null) ? ad.getCalendar() : null;
+ }
+
+ public long getTime() {
+ AtomDate ad = getValue();
+ return (ad != null) ? ad.getTime() : null;
+ }
+
+ public String getString() {
+ AtomDate ad = getValue();
+ return (ad != null) ? ad.getValue() : null;
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,115 @@
+/*
+* 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.abdera.parser.stax;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Div;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMXMLParserWrapper;
+
+
+public class FOMDiv
+ extends FOMExtensibleElement
+ implements Div {
+
+ private static final long serialVersionUID = -2319449893405850433L;
+
+ public FOMDiv(
+ String name,
+ OMNamespace namespace,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(name, namespace, parent, factory);
+ }
+
+ public FOMDiv(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory)
+ throws OMException {
+ super(qname, parent, factory);
+ }
+
+ public FOMDiv(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder)
+ throws OMException {
+ super(qname, parent, factory, builder);
+ }
+
+ public String[] getXhtmlClass() {
+ String _class = _getAttributeValue(CLASS);
+ String[] classes = null;
+ if (_class != null) {
+ classes = _class.split(" ");
+ }
+ return classes;
+ }
+
+ public String getId() {
+ return _getAttributeValue(AID);
+ }
+
+ public String getTitle() {
+ return _getAttributeValue(ATITLE);
+ }
+
+ public void setId(String id) {
+ if (id != null)
+ _setAttributeValue(AID, id);
+ else
+ _removeAttribute(AID);
+ }
+
+ public void setTitle(String title) {
+ if (title != null)
+ _setAttributeValue(ATITLE, title);
+ else
+ _removeAttribute(ATITLE);
+ }
+
+ public void setXhtmlClass(String[] classes) {
+ if (classes != null) {
+ String val = "";
+ for (String s : classes) {
+ if (s.length() > 0) val += " ";
+ val += s;
+ }
+ _setAttributeValue(CLASS, val);
+ } else _removeAttribute(CLASS);
+ }
+
+ public String getValue() {
+ return getText();
+ }
+
+ public void setValue(String value) {
+ if (value != null)
+ setText(value);
+ else
+ _removeAllChildren();
+ }
+
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,148 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+import javax.activation.MimeType;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.impl.llom.OMDocumentImpl;
+
+
+public class FOMDocument<T extends Element>
+ extends OMDocumentImpl
+ implements Document<T> {
+
+ private static final long serialVersionUID = -3255339511063344662L;
+ protected URI base = null;
+ protected MimeType contentType = null;
+ protected Date lastModified = null;
+
+ public FOMDocument() {
+ super();
+ }
+
+ public FOMDocument(
+ OMElement documentElement,
+ OMXMLParserWrapper parserWrapper,
+ OMFactory factory) {
+ super(documentElement, parserWrapper, factory);
+ }
+
+ public FOMDocument(
+ OMElement documentElement,
+ OMXMLParserWrapper parserWrapper) {
+ super(documentElement, parserWrapper);
+ }
+
+ public FOMDocument(
+ OMFactory factory) {
+ super(factory);
+ }
+
+ public FOMDocument(
+ OMXMLParserWrapper parserWrapper,
+ OMFactory factory) {
+ super(parserWrapper, factory);
+ }
+
+ public FOMDocument(
+ OMXMLParserWrapper parserWrapper) {
+ super(parserWrapper);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T getRoot() {
+ return (T)this.getOMDocumentElement();
+ }
+
+ public void setRoot(T root) {
+ this.setOMDocumentElement((OMElement) root);
+ }
+
+ public URI getBaseUri() {
+ return base;
+ }
+
+ public void setBaseUri(URI base) {
+ this.base = base;
+ }
+
+ public void setBaseUri(String base) throws URISyntaxException {
+ setBaseUri((base != null) ? new URI(base) : null);
+ }
+
+ public void writeTo(OutputStream out) throws IOException {
+ try {
+ setComplete(true);
+ OMOutputFormat outputFormat = new OMOutputFormat();
+ outputFormat.setCharSetEncoding(this.getCharsetEncoding());
+ serializeAndConsume(out, outputFormat);
+ } catch (XMLStreamException e) {
+ throw new FOMException(e);
+ }
+ }
+
+ public MimeType getContentType() {
+ return contentType;
+ }
+
+ public void setContentType(MimeType contentType) {
+ this.contentType = contentType;
+ }
+
+ public Date getLastModified() {
+ return this.lastModified;
+ }
+
+ public void setLastModified(Date lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Document<T> clone() {
+ T rootClone = (T)getRoot().clone();
+ Document<T> doc = ((FOMFactory)factory).newDocument();
+ doc.setRoot(rootClone);
+ return doc;
+ }
+
+ public String getCharset() {
+ return this.getCharsetEncoding();
+ }
+
+ public void setCharset(String charset) {
+ this.setCharsetEncoding(charset);
+ }
+
+ public Factory getFactory() {
+ return (Factory) this.factory;
+ }
+}
Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?rev=414893&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java Fri Jun 16 13:10:47 2006
@@ -0,0 +1,540 @@
+/*
+* 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.abdera.parser.stax;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Div;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Text;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.util.Constants;
+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;
+
+
+public class FOMElement
+ extends OMElementImpl
+ implements Element,
+ OMElement,
+ Constants {
+
+ private static final long serialVersionUID = 8024257594220911953L;
+
+ 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, parent, factory);
+ }
+
+ public FOMElement(
+ QName qname,
+ OMContainer parent,
+ OMFactory factory,
+ OMXMLParserWrapper builder) {
+ super(
+ qname.getLocalPart(),
+ factory.createOMNamespace(
+ qname.getNamespaceURI(),
+ qname.getPrefix()),
+ parent,
+ builder,
+ factory);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Base>T getParentElement() {
+ return (T)super.getParent();
+ }
+
+ public void setParentElement(Element parent) {
+ super.setParent((FOMElement)parent);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Element>T getPreviousSibling() {
+ OMNode el = this.getPreviousOMSibling();
+ while (el != null) {
+ if (el instanceof Element) return (T)el;
+ else el = el.getPreviousOMSibling();
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Element>T getNextSibling() {
+ OMNode el = this.getNextOMSibling();
+ while (el != null) {
+ if (el instanceof Element) return (T) el;
+ else el = el.getNextOMSibling();
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Element>T getFirstChild() {
+ return (T)this.getFirstElement();
+ }
+
+ @SuppressWarnings("unchecked")
+ 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)omel;
+ el = el.getPreviousSibling();
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ 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)omel;
+ el = el.getNextSibling();
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Element>T getFirstChild(QName qname) {
+ return (T)this.getFirstChildWithName(qname);
+ }
+
+ public String getLanguage() {
+ return _getAttributeValue(LANG);
+ }
+
+ public void setLanguage(String language) {
+ _setAttributeValue(LANG,language);
+ }
+
+ public URI getBaseUri() throws URISyntaxException {
+ URI 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 URI getResolvedBaseUri() throws URISyntaxException {
+ URI baseUri = null;
+ URI 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) {
+ if (baseUri != null) {
+ uri = baseUri.resolve(uri);
+ } else {
+ return getBaseUri();
+ }
+ } else if (uri == null) {
+ uri = baseUri;
+ }
+ return uri;
+ }
+
+ public void setBaseUri(URI base) {
+ _setAttributeValue(BASE,_getStringValue(base));
+ }
+
+ public void setBaseUri(String base) throws URISyntaxException {
+ setBaseUri((base != null) ? new URI(base) : null);
+ }
+
+ protected String _getAttributeValue(QName qname) {
+ OMAttribute attr = getAttribute(qname);
+ return (attr != null) ? attr.getAttributeValue() : null;
+ }
+
+ protected void _setAttributeValue(QName qname, String value) {
+ OMAttribute attr = this.getAttribute(qname);
+ if (attr != null && value != null) {
+ attr.setAttributeValue(value);
+ } else {
+ if (value != null) {
+ if (qname.getNamespaceURI() != null) {
+ attr = factory.createOMAttribute(
+ qname.getLocalPart(),
+ factory.createOMNamespace(
+ qname.getNamespaceURI(),
+ qname.getPrefix()),
+ value);
+ } else {
+ attr = factory.createOMAttribute(
+ qname.getLocalPart(),
+ null,
+ value);
+ }
+ addAttribute(attr);
+ } else {
+ removeAttribute(attr);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected <E extends Element>List<E> _getChildrenAsSet(QName qname) {
+ List<E> set = new ArrayList<E>();
+ for (Iterator i = getChildrenWithName(qname);i.hasNext();) {
+ set.add((E)i.next());
+ }
+ return set;
+ }
+
+ protected <E extends Element> void _setChildrenFromSet(QName qname, List<E> set) {
+ for (Iterator i = getChildrenWithName(qname);i.hasNext();) {
+ ((OMElement)i.next()).discard();
+ }
+ if (set != null) {
+ for (Element element : set) {
+ addChild((OMElement)element);
+ }
+ }
+ }
+
+ 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 URI _getUriValue(String v) throws URISyntaxException {
+ return (v != null) ? new URI(v) : null;
+ }
+
+ protected String _getStringValue(URI uri) {
+ return (uri != null) ? uri.toString() : null;
+ }
+
+ protected URI _resolve(URI base, URI value) {
+ if (value == null) return null;
+ if ("".equals(value.toString()) ||
+ "#".equals(value.toString()) ||
+ ".".equals(value.toString()) ||
+ "./".equals(value.toString())) return base;
+ if (base == null) return value;
+ if ("".equals(base.getPath())) base = base.resolve("/");
+ URI resolved = (base != null) ? base.resolve(value) : value;
+ return resolved;
+ }
+
+ public void writeTo(OutputStream out) throws IOException {
+ try {
+ OMOutputFormat outputFormat = new OMOutputFormat();
+ outputFormat.setCharSetEncoding(getDocument().getCharset());
+ serializeAndConsume(out, outputFormat);
+ } catch (XMLStreamException e) {
+ throw new FOMException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ 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 void setAttributeValue(QName qname, String value) {
+ _setAttributeValue(qname, value);
+ }
+
+ public void setAttributeValue(
+ String namespace,
+ String localPart,
+ String prefix,
+ String value) {
+ setAttributeValue(new QName(namespace, localPart, prefix), value);
+ }
+
+ public String getAttributeValue(QName qname) {
+ OMAttribute attr = getAttribute(qname);
+ return (attr != null) ? attr.getAttributeValue() : null;
+ }
+
+ public String getAttributeValue(String namespace, String localPart) {
+ return this.getAttributeValue(new QName(namespace, localPart));
+ }
+
+ public String getAttributeValue(String name) {
+ return getAttributeValue(new QName(name));
+ }
+
+ public void setAttributeValue(String name, String value) {
+ setAttributeValue(new QName(name), value);
+ }
+
+ protected void _setElementValue(QName qname, String value) {
+ OMElement element = this.getFirstChildWithName(qname);
+ if (element != null && value != null) {
+ element.setText(value);
+ } else if (element != null && value == null) {
+ for (Iterator i = element.getChildren(); i.hasNext();) {
+ OMNode node = (OMNode) 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 value;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected <T extends Text>T getTextElement(QName qname) {
+ return (T)getFirstChildWithName(qname);
+ }
+
+ protected <T extends Text>void setTextElement(QName qname, T text, boolean many) {
+ if (text != null) {
+ if (!many) {
+ OMElement el = getFirstChildWithName(qname);
+ if (el != null) el.discard();
+ }
+ _setChild(qname, (OMElement)text);
+ } else _removeElement(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, null);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setHtmlText(QName qname, String value, URI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory) factory;
+ Text text = fomfactory.newText(qname, Text.Type.HTML, null);
+ if (baseUri != null) text.setBaseUri(baseUri);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setXhtmlText(QName qname, String value, URI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory) factory;
+ Text text = fomfactory.newText(qname, Text.Type.XHTML, null);
+ if (baseUri != null) text.setBaseUri(baseUri);
+ text.setValue(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected Text setXhtmlText(QName qname, Div value, URI baseUri) {
+ if (value == null) {
+ setTextElement(qname, null, false);
+ return null;
+ }
+ FOMFactory fomfactory = (FOMFactory) factory;
+ Text text = fomfactory.newText(qname, Text.Type.XHTML, null);
+ if (baseUri != null) text.setBaseUri(baseUri);
+ text.setValueElement(value);
+ setTextElement(qname, text, false);
+ return text;
+ }
+
+ protected String getText(QName qname) {
+ Text text = getTextElement(qname);
+ return (text != null) ? text.getValue() : null;
+ }
+
+ protected Element _parse(String value, URI baseUri) {
+ if (value == null) return null;
+ FOMFactory fomfactory = (FOMFactory) factory;
+ Parser parser = fomfactory.newParser();
+ ByteArrayInputStream bais = new ByteArrayInputStream(value.getBytes());
+ ParserOptions options = parser.getDefaultParserOptions();
+ options.setCharset(getXMLStreamReader().getCharacterEncodingScheme());
+ options.setFactory(fomfactory);
+ Document doc = parser.parse(bais, baseUri, options);
+ return doc.getRoot();
+ }
+
+ protected void _removeAttribute(QName qname) {
+ if (getAttribute(qname) != null)
+ removeAttribute(getAttribute(qname));
+ }
+
+ protected void _removeElement(QName qname, boolean many) {
+ if (many) {
+ for (Iterator i = getChildrenWithName(qname); i.hasNext();) {
+ OMElement element = (OMElement) i.next();
+ element.discard();
+ }
+ } else {
+ OMElement element = getFirstChildWithName(qname);
+ if (element != null) element.discard();
+ }
+ }
+
+ protected void _removeAllChildren() {
+ for (Iterator i = getChildren(); i.hasNext();) {
+ OMNode node = (OMNode) i.next();
+ node.discard();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Base>T clone() {
+ // TODO: axiom 1.0 now has a clone method, but it doesn't seem
+ // to want to work with this. we need to investigate
+ OMElement el = _create(this);
+ _copyElement(this, el);
+ return (T) el;
+
+ }
+
+ protected OMElement _copyElement(OMElement src, OMElement dest) {
+ for (Iterator i = src.getAllAttributes(); i.hasNext();) {
+ OMAttribute attr = (OMAttribute) i.next();
+ dest.addAttribute(
+ attr.getLocalName(),
+ attr.getAttributeValue(),
+ (attr.getNamespace() != null) ?
+ dest.declareNamespace(attr.getNamespace()) : null);
+ }
+ for (Iterator i = src.getChildren(); i.hasNext();) {
+ OMNode node = (OMNode) 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(), null, factory, obj);
+
+ return el;
+ }
+
+
+ public Factory getFactory() {
+ return (Factory) this.factory;
+ }
+}