abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r437876 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/util/ core/src/main/java/org/apache/abdera/writer/ extensions/src/main/java/org/apache/abdera/ext/json/ parser/src/main/java/org/apache/abdera/parser/stax/ pars...
Date Mon, 28 Aug 2006 23:35:04 GMT
Author: jmsnell
Date: Mon Aug 28 16:35:04 2006
New Revision: 437876

URL: http://svn.apache.org/viewvc?rev=437876&view=rev
Log:
* Add the ability to lookup NamedWriter instances by the media type of the output
  format they support (e.g., WriterFactory.getWriterByMediaType("application/json"); )

* Create an abstract base impl for NamedWriters

* Modify the JSON and PrettyXML writers to use the abstract base impl

Added:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/NamedWriter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterFactory.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterFactory.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java?rev=437876&view=auto
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
(added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
Mon Aug 28 16:35:04 2006
@@ -0,0 +1,48 @@
+/*
+* 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.util;
+
+import org.apache.abdera.writer.NamedWriter;
+
+public abstract class AbstractNamedWriter 
+  implements NamedWriter {
+
+  protected String name = null;
+  protected String[] formats = null;
+  
+  protected AbstractNamedWriter(String name, String[] formats ) {
+    this.name = name;
+    this.formats = formats;
+  }
+  
+  public String getName() {
+    return name;
+  }
+
+  public String[] getOutputFormats() {
+    return formats;
+  }
+
+  public boolean outputsFormat(String mediatype) {
+    for (String format : formats) {
+      if (MimeTypeHelper.isMatch(format, mediatype))
+        return true;
+    }
+    return false;
+  }
+}

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/NamedWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/NamedWriter.java?rev=437876&r1=437875&r2=437876&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/NamedWriter.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/NamedWriter.java
Mon Aug 28 16:35:04 2006
@@ -20,5 +20,15 @@
 public interface NamedWriter extends Writer {
 
   String getName();
+
+  /**
+   * Return the media type of the format produced by this writer
+   */
+  String[] getOutputFormats();
   
+  /**
+   * Returns true if the format produced by this writer matches the specified
+   * media type
+   */
+  boolean outputsFormat(String mediatype);
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterFactory.java?rev=437876&r1=437875&r2=437876&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterFactory.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterFactory.java
Mon Aug 28 16:35:04 2006
@@ -23,4 +23,5 @@
   
   Writer getWriter(String name);
   
+  Writer getWriterByMediaType(String mediatype);
 }

Modified: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java?rev=437876&r1=437875&r2=437876&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
(original)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
Mon Aug 28 16:35:04 2006
@@ -38,15 +38,30 @@
 import org.apache.abdera.model.Service;
 import org.apache.abdera.model.Workspace;
 import org.apache.abdera.model.Content.Type;
+import org.apache.abdera.util.AbstractNamedWriter;
 import org.apache.abdera.writer.NamedWriter;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-public class JSONWriter implements NamedWriter {
+public class JSONWriter
+  extends AbstractNamedWriter
+  implements NamedWriter {
 
   public static final String NAME = "json";
 
+  public static final String[] FORMATS = {
+    "application/json",
+    "application/javascript",
+    "application/ecmascript",
+    "text/javascript",
+    "text/ecmascript"
+  };
+  
+  public JSONWriter() {
+    super(NAME,FORMATS);
+  }
+  
   public String getName() {
     return NAME;
   }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterFactory.java?rev=437876&r1=437875&r2=437876&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterFactory.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterFactory.java
Mon Aug 28 16:35:04 2006
@@ -50,6 +50,14 @@
       loadWriters().get(name.toLowerCase()) : getWriter();
   }
 
+  public Writer getWriterByMediaType(String mediatype) {
+    Map<String,NamedWriter> writers = loadWriters();
+    for (NamedWriter writer : writers.values()) {
+      if (writer.outputsFormat(mediatype)) return writer;
+    }
+    return null;
+  }
+  
   private Map<String,NamedWriter> loadWriters() {
     return getAbdera().getConfiguration().getNamedWriters();
   }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java?rev=437876&r1=437875&r2=437876&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
Mon Aug 28 16:35:04 2006
@@ -30,17 +30,26 @@
 
 import org.apache.abdera.model.Base;
 import org.apache.abdera.model.Document;
+import org.apache.abdera.util.AbstractNamedWriter;
 import org.apache.abdera.writer.NamedWriter;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.util.StAXUtils;
 
-public class PrettyWriter implements NamedWriter {
-
-  public String getName() {
-    return "PrettyXML";
+public class PrettyWriter
+  extends AbstractNamedWriter 
+  implements NamedWriter {
+
+  private static final String[] FORMATS = {
+    "application/atom+xml",
+    "application/atomserv+xml",
+    "application/xml"
+  };
+  
+  public PrettyWriter() {
+    super("PrettyXML", FORMATS);
   }
-
+  
   public Object write(Base base) throws IOException {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     writeTo(base,out);
@@ -259,4 +268,5 @@
       return spaces;
     }
   }
+
 }



Mime
View raw message