You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by el...@apache.org on 2006/08/19 22:19:31 UTC
svn commit: r432872 -
/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
Author: eliast
Date: Sat Aug 19 13:19:30 2006
New Revision: 432872
URL: http://svn.apache.org/viewvc?rev=432872&view=rev
Log:
- Reformatted to add spaces after 'if'
Modified:
incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
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=432872&r1=432871&r2=432872&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 Sat Aug 19 13:19:30 2006
@@ -1,20 +1,20 @@
/*
-* 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.
-*/
+ * 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.json;
@@ -46,166 +46,163 @@
public class JSONWriter implements NamedWriter {
public static final String NAME = "json";
-
+
public String getName() {
return NAME;
}
-
+
public Object write(Base base) throws IOException {
try {
return toJSON(base).toString();
- } catch(Exception e) {
+ } catch (Exception e) {
throw new IOException(e.getMessage());
- }
+ }
}
public void writeTo(Base base, OutputStream out) throws IOException {
try {
Object result = toJSON(base);
out.write(result.toString().getBytes());
- } catch(Exception e) {
+ } catch (Exception e) {
throw new IOException(e.getMessage());
- }
+ }
}
public void writeTo(Base base, java.io.Writer out) throws IOException {
try {
Object result = toJSON(base);
out.write(result.toString());
- } catch(Exception e) {
+ } catch (Exception e) {
throw new IOException(e.getMessage());
- }
+ }
}
-
+
public static Object toJSON(Object object) throws Exception {
- if(object instanceof Feed) {
+ if (object instanceof Feed) {
return toJSON((Feed) object);
- } else if(object instanceof Entry) {
+ } else if (object instanceof Entry) {
return toJSON((Entry) object);
- } else if(object instanceof Service) {
+ } else if (object instanceof Service) {
return toJSON((Service) object);
- } else if(object instanceof Document) {
- return toJSON(((Document)object).getRoot());
+ } else if (object instanceof Document) {
+ return toJSON(((Document) object).getRoot());
}
return new IllegalArgumentException("Element is not supported by JSONWriter.");
}
-
+
public static JSONObject toJSON(Entry entry) throws Exception {
JSONObject jsentry = new JSONObject();
- if(entry.getTitle() != null)
+ if (entry.getTitle() != null)
jsentry.put("title", entry.getTitle());
-
- if(entry.getSummary() != null)
- jsentry.put("summary", entry.getSummary());
-
- if(entry.getId() != null)
- jsentry.put("id", entry.getId().toString());
-
- if(entry.getUpdated() != null)
- jsentry.put("updated", entry.getUpdated().toString());
-
- if(entry.getPublished() != null)
- jsentry.put("published", entry.getPublished().toString());
-
- jsentry.put("authors", personsToJSON(entry.getAuthors()));
-
+
+ if (entry.getSummary() != null)
+ jsentry.put("summary", entry.getSummary());
+
+ if (entry.getId() != null)
+ jsentry.put("id", entry.getId().toString());
+
+ if (entry.getUpdated() != null)
+ jsentry.put("updated", entry.getUpdated().toString());
+
+ if (entry.getPublished() != null)
+ jsentry.put("published", entry.getPublished().toString());
+
+ jsentry.put("authors", personsToJSON(entry.getAuthors()));
+
jsentry.put("contributors", personsToJSON(entry.getContributors()));
-
- jsentry.put("categories", categoriesToJSON(entry.getCategories()));
-
+
+ jsentry.put("categories", categoriesToJSON(entry.getCategories()));
+
jsentry.put("links", linksToJSON(entry.getLinks()));
-
+
JSONObject jscontent = new JSONObject();
- if(entry.getContentElement() != null) {
-
- Content content = entry.getContentElement();
- Type type = entry.getContentType();
- if (type.equals(Content.Type.HTML) ||
- type.equals(Content.Type.XHTML) ||
- type.equals(Content.Type.TEXT)) {
- jscontent.put("type", type.toString().toLowerCase());
- } else {
- jscontent.put("type", content.getMimeType().toString());
- }
- jscontent.put("value", JSONObject.quote(content.getValue()));
- jsentry.put("content", jscontent);
- }
-
+ if (entry.getContentElement() != null) {
+
+ Content content = entry.getContentElement();
+ Type type = entry.getContentType();
+ if (type.equals(Content.Type.HTML) || type.equals(Content.Type.XHTML) || type.equals(Content.Type.TEXT)) {
+ jscontent.put("type", type.toString().toLowerCase());
+ } else {
+ jscontent.put("type", content.getMimeType().toString());
+ }
+ jscontent.put("value", JSONObject.quote(content.getValue()));
+ jsentry.put("content", jscontent);
+ }
+
return jsentry;
}
public static JSONObject toJSON(Feed feed) throws Exception {
JSONObject jsfeed = new JSONObject();
-
+
if (feed.getGenerator() != null) {
Generator gen = feed.getGenerator();
JSONObject jsgen = new JSONObject();
- jsgen.put("uri",gen.getUri().toString());
- jsgen.put("value",gen.getText());
+ jsgen.put("uri", gen.getUri().toString());
+ jsgen.put("value", gen.getText());
}
-
+
if (feed.getTitle() != null) {
- jsfeed.put("title",feed.getTitle());
+ jsfeed.put("title", feed.getTitle());
}
-
+
if (feed.getSubtitle() != null) {
- jsfeed.put("subtitle",feed.getSubtitle());
+ jsfeed.put("subtitle", feed.getSubtitle());
}
-
+
if (feed.getId() != null) {
- jsfeed.put("id",feed.getId().toString());
- }
-
+ jsfeed.put("id", feed.getId().toString());
+ }
+
if (feed.getRights() != null) {
- jsfeed.put("rights",feed.getRights());
+ jsfeed.put("rights", feed.getRights());
}
-
+
if (feed.getLogo() != null) {
- jsfeed.put("logo",feed.getLogo().toString());
+ jsfeed.put("logo", feed.getLogo().toString());
}
-
+
if (feed.getIcon() != null) {
- jsfeed.put("icon",feed.getIcon().toString());
- }
-
+ jsfeed.put("icon", feed.getIcon().toString());
+ }
+
if (feed.getUpdatedString() != null) {
- jsfeed.put("updated",feed.getUpdatedString());
- }
-
+ jsfeed.put("updated", feed.getUpdatedString());
+ }
+
jsfeed.put("authors", personsToJSON(feed.getAuthors()));
-
+
jsfeed.put("contributors", personsToJSON(feed.getContributors()));
-
+
jsfeed.put("categories", categoriesToJSON(feed.getCategories()));
-
- jsfeed.put("links", linksToJSON(feed.getLinks()));
-
+
+ jsfeed.put("links", linksToJSON(feed.getLinks()));
+
JSONArray jsentries = new JSONArray();
List<Entry> entries = feed.getEntries();
- for(Entry entry : entries) {
- jsentries.put(toJSON(entry));
+ for (Entry entry : entries) {
+ jsentries.put(toJSON(entry));
}
-
+
jsfeed.put("entries", jsentries);
-
+
return jsfeed;
}
-
public static JSONObject toJSON(Service service) throws Exception {
JSONObject jssvc = new JSONObject();
JSONArray jsworkspaces = new JSONArray();
List<Workspace> workspaces = service.getWorkspaces();
- for(Workspace workspace : workspaces) {
+ for (Workspace workspace : workspaces) {
JSONObject jsworkspace = new JSONObject();
JSONArray jscollections = new JSONArray();
jsworkspace.put("title", workspace.getTitle());
List<Collection> collections = workspace.getCollections();
- for(Collection collection : collections) {
+ for (Collection collection : collections) {
JSONObject jscollection = new JSONObject();
JSONArray jsaccepts = new JSONArray();
String[] accepts = collection.getAccept();
- for(String accept : accepts) {
+ for (String accept : accepts) {
jsaccepts.put(accept);
}
jscollection.put("href", collection.getHref().toString());
@@ -216,10 +213,10 @@
jsworkspaces.put(jsworkspace);
}
jssvc.put("workspaces", jsworkspaces);
-
+
return jssvc;
- }
-
+ }
+
private static JSONArray categoriesToJSON(List<Category> categories) throws URISyntaxException, JSONException {
JSONArray jscategories = new JSONArray();
for (Category category : categories) {
@@ -238,42 +235,42 @@
}
return jscategories;
}
-
+
private static JSONArray personsToJSON(List<Person> persons) throws URISyntaxException, JSONException {
JSONArray jspersons = new JSONArray();
for (Person p : persons) {
- if(p.getName() != null || p.getUri() != null || p.getEmail() != null) {
- JSONObject jsperson = new JSONObject();
- if(p.getName() != null)
- jsperson.put("name", p.getName());
- if(p.getUri() != null)
- jsperson.put("uri", p.getUri().toString());
- if(p.getEmail() != null)
- jsperson.put("email", p.getEmail());
- jspersons.put(jsperson);
+ if (p.getName() != null || p.getUri() != null || p.getEmail() != null) {
+ JSONObject jsperson = new JSONObject();
+ if (p.getName() != null)
+ jsperson.put("name", p.getName());
+ if (p.getUri() != null)
+ jsperson.put("uri", p.getUri().toString());
+ if (p.getEmail() != null)
+ jsperson.put("email", p.getEmail());
+ jspersons.put(jsperson);
}
- }
+ }
return jspersons;
}
-
+
private static JSONArray linksToJSON(List<Link> links) throws URISyntaxException, MimeTypeParseException, JSONException {
JSONArray jslinks = new JSONArray();
- for(Link link : links) {
+ for (Link link : links) {
JSONObject jslink = new JSONObject();
- if(link.getHref() != null) {
+ if (link.getHref() != null) {
jslink.put("href", link.getHref().toString());
-
- if(link.getRel() != null)
+
+ if (link.getRel() != null)
jslink.put("rel", link.getRel());
-
- if(link.getMimeType() != null)
+
+ if (link.getMimeType() != null)
jslink.put("type", link.getMimeType().getBaseType());
-
- if(link.getHrefLang() != null)
+
+ if (link.getHrefLang() != null)
jslink.put("hreflang", link.getHrefLang());
}
}
return jslinks;
}
-
+
}