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/10/28 02:36:17 UTC
svn commit: r468598 - in
/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch:
OpenSearchV11Helper.java Query.java
Author: jmsnell
Date: Fri Oct 27 17:36:17 2006
New Revision: 468598
URL: http://svn.apache.org/viewvc?view=rev&rev=468598
Log:
OpenSearch v1.1 feed extension element implementation. This demonstrates an alternative approach
to handling extension elements that is independent of the underlying Axiom implementation. The
other OpenSearch v1.0 stuff should be refactored to match this.
Added:
incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/OpenSearchV11Helper.java
incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/Query.java
Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/OpenSearchV11Helper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/OpenSearchV11Helper.java?view=auto&rev=468598
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/OpenSearchV11Helper.java (added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/OpenSearchV11Helper.java Fri Oct 27 17:36:17 2006
@@ -0,0 +1,113 @@
+/*
+* 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.ext.opensearch;
+
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Feed;
+
+public class OpenSearchV11Helper {
+
+ public static final String OPENSEARCH_NS = "http://a9.com/-/spec/opensearchrss/1.1/";
+
+ public static final String TOTAL_RESULTS_LN = "totalResults";
+ public static final String ITEMS_PER_PAGE_LN = "itemsPerPage";
+ public static final String START_INDEX_LN = "startIndex";
+ public static final String QUERY_LN = "Query";
+
+ public static final String OS_PREFIX = "os";
+
+ public static final QName TOTAL_RESULTS = new QName(OPENSEARCH_NS, TOTAL_RESULTS_LN, OS_PREFIX);
+ public static final QName ITEMS_PER_PAGE = new QName(OPENSEARCH_NS, ITEMS_PER_PAGE_LN, OS_PREFIX);
+ public static final QName START_INDEX = new QName(OPENSEARCH_NS, START_INDEX_LN, OS_PREFIX);
+ public static final QName QUERY = new QName(OPENSEARCH_NS, QUERY_LN, OS_PREFIX);
+
+ public static void setTotalResults(Feed feed, int totalResults) {
+ if (totalResults < 0) throw new IllegalArgumentException();
+ feed.declareNS(OpenSearchV11Helper.OPENSEARCH_NS, OS_PREFIX);
+ Element el = feed.getExtension(TOTAL_RESULTS);
+ if (totalResults != -1) {
+ if (el != null) el.setText(String.valueOf(totalResults));
+ else feed.addSimpleExtension(
+ TOTAL_RESULTS,
+ String.valueOf(totalResults));
+ } else {
+ if (el != null) el.discard();
+ }
+ }
+
+ public static int getTotalResults(Feed feed) {
+ String val = feed.getSimpleExtension(TOTAL_RESULTS);
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public static void setItemsPerPage(Feed feed, int items) {
+ if (items < 0) throw new IllegalArgumentException();
+ feed.declareNS(OpenSearchV11Helper.OPENSEARCH_NS, OS_PREFIX);
+ Element el = feed.getExtension(ITEMS_PER_PAGE);
+ if (items != -1) {
+ if (el != null) el.setText(String.valueOf(items));
+ else feed.addSimpleExtension(
+ ITEMS_PER_PAGE,
+ String.valueOf(items));
+ } else {
+ if (el != null) el.discard();
+ }
+ }
+
+ public static int getItemsPerPage(Feed feed) {
+ String val = feed.getSimpleExtension(ITEMS_PER_PAGE);
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public static void setStartIndex(Feed feed, int startIndex) {
+ if (startIndex < 0) throw new IllegalArgumentException();
+ feed.declareNS(OpenSearchV11Helper.OPENSEARCH_NS, OS_PREFIX);
+ Element el = feed.getExtension(START_INDEX);
+ if (startIndex != -1) {
+ if (el != null) el.setText(String.valueOf(startIndex));
+ else feed.addSimpleExtension(
+ START_INDEX,
+ String.valueOf(startIndex));
+ } else {
+ if (el != null) el.discard();
+ }
+ }
+
+ public static int getStartIndex(Feed feed) {
+ String val = feed.getSimpleExtension(START_INDEX);
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public static Query addQuery(Feed feed) {
+ return new Query(feed);
+ }
+
+ public static Query[] getQueries(Feed feed) {
+ List<Element> els = feed.getExtensions(QUERY);
+ Query[] queries = new Query[els.size()];
+ int n = 0;
+ for (Element el : els) {
+ queries[n++] = new Query(el);
+ }
+ return queries;
+ }
+}
Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/Query.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/Query.java?view=auto&rev=468598
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/Query.java (added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch/Query.java Fri Oct 27 17:36:17 2006
@@ -0,0 +1,223 @@
+/*
+* 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.ext.opensearch;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensibleElement;
+
+public class Query {
+
+ public enum Role {
+ CORRECTION,
+ EXAMPLE,
+ RELATED,
+ REQUEST,
+ SUBSET,
+ SUPERSET;
+ }
+
+ private final Element internal;
+
+ public Query(Element internal) {
+ this.internal = internal;
+ }
+
+ public Query(Abdera abdera) {
+ internal = abdera.getFactory().newElement(OpenSearchV11Helper.QUERY);
+ }
+
+ public Query(ExtensibleElement parent) {
+ parent.declareNS(OpenSearchV11Helper.OPENSEARCH_NS, OpenSearchV11Helper.OS_PREFIX);
+ internal = parent.addExtension(OpenSearchV11Helper.QUERY);
+ }
+
+ public void setParent(ExtensibleElement parent) {
+ parent.addExtension(internal);
+ }
+
+ public void discard() {
+ internal.discard();
+ }
+
+ public Role getRole() {
+ String role = internal.getAttributeValue("role");
+ if (role == null) return null;
+ try {
+ return Role.valueOf(role.toUpperCase());
+ } catch (Exception e) {
+ return null; // role is likely an extension. we don't currently support extension roles
+ }
+ }
+
+ public void setRole(Role role) {
+ if (role != null) {
+ internal.setAttributeValue("role", role.name().toLowerCase());
+ } else {
+ internal.removeAttribute(new QName("role"));
+ }
+ }
+
+ public String getTitle() {
+ return internal.getAttributeValue("title");
+ }
+
+ public void setTitle(String title) {
+ if (title != null) {
+ if (title.length() > 256) throw new IllegalArgumentException("Title too long (max 256 characters)");
+ internal.setAttributeValue("title", title);
+ } else {
+ internal.removeAttribute(new QName("title"));
+ }
+ }
+
+ public int getTotalResults() {
+ String val = internal.getAttributeValue("totalResults");
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public void setTotalResults(int totalResults) {
+ if (totalResults > -1) {
+ internal.setAttributeValue("totalResults", String.valueOf(totalResults));
+ } else {
+ internal.removeAttribute(new QName("totalResults"));
+ }
+ }
+
+ public String getSearchTerms() {
+ return internal.getAttributeValue("searchTerms");
+ }
+
+ public void setSearchTerms(String terms) {
+ if (terms != null) {
+ internal.setAttributeValue("searchTerms", terms);
+ } else {
+ internal.removeAttribute(new QName("searchTerms"));
+ }
+ }
+
+ public int getCount() {
+ String val = internal.getAttributeValue("count");
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public void setCount(int count) {
+ if (count > -1) {
+ internal.setAttributeValue("count", String.valueOf(count));
+ } else {
+ internal.removeAttribute(new QName("count"));
+ }
+ }
+
+ public int getStartIndex() {
+ String val = internal.getAttributeValue("startIndex");
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public void setStartIndex(int startIndex) {
+ if (startIndex > -1) {
+ internal.setAttributeValue("startIndex", String.valueOf(startIndex));
+ } else {
+ internal.removeAttribute(new QName("startIndex"));
+ }
+ }
+
+ public int getStartPage() {
+ String val = internal.getAttributeValue("startPage");
+ return (val != null) ? Integer.parseInt(val) : -1;
+ }
+
+ public void setStartPage(int startPage) {
+ if (startPage > -1) {
+ internal.setAttributeValue("startPage", String.valueOf(startPage));
+ } else {
+ internal.removeAttribute(new QName("startPage"));
+ }
+ }
+
+ public String getLanguage() {
+ return internal.getAttributeValue("language");
+ }
+
+ public void setLanguage(String language) {
+ if (language != null) {
+ internal.setAttributeValue("language", language);
+ } else {
+ internal.removeAttribute(new QName("language"));
+ }
+ }
+
+ public String getInputEncoding() {
+ return internal.getAttributeValue("inputEncoding");
+ }
+
+ public void setInputEncoding(String encoding) {
+ if (encoding != null) {
+ internal.setAttributeValue("inputEncoding", encoding);
+ } else {
+ internal.removeAttribute(new QName("inputEncoding"));
+ }
+ }
+
+ public String getOutputEncoding() {
+ return internal.getAttributeValue("outputEncoding");
+ }
+
+ public void setOutputEncoding(String encoding) {
+ if (encoding != null) {
+ internal.setAttributeValue("outputEncoding", encoding);
+ } else {
+ internal.removeAttribute(new QName("outputEncoding"));
+ }
+ }
+
+ public String getAttribute(QName qname) {
+ return internal.getAttributeValue(qname);
+ }
+
+ public void setAttribute(QName qname, String value) {
+ if (value != null) {
+ internal.setAttributeValue(qname, value);
+ } else {
+ internal.removeAttribute(qname);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return internal.toString();
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Query)) return false;
+ Query query = (Query) other;
+ return (internal.equals(query.internal));
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = super.hashCode();
+ result = PRIME * result + ((internal == null) ? 0 : internal.hashCode());
+ return result;
+ }
+
+}
Re: svn commit: r468598 - in /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch:
OpenSearchV11Helper.java Query.java
Posted by James M Snell <ja...@gmail.com>.
I agree it's less than ideal. Here's one approach that could possibly
work (but caries a bit of additional overhead in the implementation).
For extension elements, the FOMBuilder could always create a FOMElement
instance, then pass that off to ExtensionFactory which would return an
ElementWrapper instance. ElementWrapper would implement the Element
interface. The various getExtension methods on ExtensibleElement would
need to be capable of wrapping and unwrapping the element as necessary
meaning that we'd have to keep track of the QName-to-ExtensionFactory
mapping. Extension Implementors would then need only to extend
ElementWrapper to implement their stuff.
Shouldn't take too long to code up.
- James
Garrett Rooney wrote:
> On 10/27/06, jmsnell@apache.org <jm...@apache.org> wrote:
>
>> OpenSearch v1.1 feed extension element implementation. This
>> demonstrates an alternative approach
>> to handling extension elements that is independent of the underlying
>> Axiom implementation. The
>> other OpenSearch v1.0 stuff should be refactored to match this.
>
> I'm not sure I like this new approach. On one hand, yes, it does
> allow manipulation of extension elements without knowledge of the
> underlying parser implementation. On the other, it means that you
> have to treat the extension elements specially, where a big part of
> what I like about Abdera is that extension elements can be treated
> just like any other type of element. If we're going to migrate to a
> non-axiom based parser anyway, is it really necessary to bend over
> backwards this way to break the connection? It seems like we should
> be able to make the parser implementation unaware of the exact
> implementation class used for a given Element, with a little bit of
> work.
>
> -garrett
>
Re: svn commit: r468598 - in /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/opensearch: OpenSearchV11Helper.java Query.java
Posted by Garrett Rooney <ro...@electricjellyfish.net>.
On 10/27/06, jmsnell@apache.org <jm...@apache.org> wrote:
> OpenSearch v1.1 feed extension element implementation. This demonstrates an alternative approach
> to handling extension elements that is independent of the underlying Axiom implementation. The
> other OpenSearch v1.0 stuff should be refactored to match this.
I'm not sure I like this new approach. On one hand, yes, it does
allow manipulation of extension elements without knowledge of the
underlying parser implementation. On the other, it means that you
have to treat the extension elements specially, where a big part of
what I like about Abdera is that extension elements can be treated
just like any other type of element. If we're going to migrate to a
non-axiom based parser anyway, is it really necessary to bend over
backwards this way to break the connection? It seems like we should
be able to make the parser implementation unaware of the exact
implementation class used for a given Element, with a little bit of
work.
-garrett