cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andre Juffer (Commented) (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (COCOON3-77) Text and JSON serializers
Date Sun, 06 Nov 2011 10:32:52 GMT

    [ https://issues.apache.org/jira/browse/COCOON3-77?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13144968#comment-13144968
] 

Andre Juffer commented on COCOON3-77:
-------------------------------------

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- Locally New
+++ Locally New
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.
+ */
+
+package org.apache.cocoon.components.serializers.util;
+
+import org.json.JSONObject;
+import org.json.JSONException;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.Attributes;
+
+/**
+ * A text serializer for JSON. This serializer expects all JSON text to be enclosed by a

+ * single element. It also checks whether or not the received JSON text is valid.
+ * @author Andr&#233; Juffer, Triacle Biocomputing
+ * 
+ * @see <a href="http://www.json.org/">JSON.org</a>
+ */
+public class JsonSerializer extends TextSerializer {
+    
+    StringBuilder jsonString;
+    boolean element;
+    
+    public JsonSerializer()
+        throws Exception        
+    {
+        super();
+        this.jsonString = new StringBuilder();
+        this.element = false;
+    }
+    
+    @Override
+    public String getMimeType()
+    {
+        return "application/json; charset=" + this.charset.getName();
+    }
+    
+    @Override
+    public void characters(char[] chars,
+                           int start,
+                           int len)
+        throws SAXException
+    {
+        if ( this.element )
+        {
+            String s = new String(chars, start, len);
+            this.jsonString.append(s);
+        }
+        super.characters(chars, start, len);
+    }
+    
+    @Override
+    public void startElement(String uri,
+                             String localName,
+                             String qName,
+                             Attributes atts)
+    throws SAXException
+    {
+        this.element = true;
+        super.startElement(uri, uri, uri, atts);
+        
+    }
+        
+    /**
+     * Validates JSON.
+     * @throws SAXException if JSON is invalid.
+     */
+    @Override
+    public void endElement(String uri,
+                           String localName,
+                           String qName)
+        throws SAXException
+    {
+        String s = this.jsonString.toString();
+        try
+        {
+            JSONObject jsonObject = new JSONObject(s);
+        }
+        catch (JSONException exception)
+        {
+            throw new SAXException(s + ": Invalid JSON.", exception);
+        }
+        super.endElement(uri, localName, qName);
+    }
+    
+    @Override
+    public void recycle()
+    {
+        this.jsonString = new StringBuilder();
+        this.element = false;
+        super.recycle();
+    }
+
+    
+    @Override
+    public String toString()
+    {
+        String newline = System.getProperty("line.separator");
+        
+        StringBuilder s = new StringBuilder(this.getClass().getName() + " : [" + newline);
+        if ( this.charset != null )
+            s.append("charset - ").append(this.charset.toString());
+        if ( this.doctype != null )
+            s.append("").append(this.doctype.toString()).append(newline);
+        s.append("mime-type - ").append(this.getMimeType()).append(newline);
+        s.append("]").append(newline);
+        
+        return s.toString();
+    }
+}

                
> Text and JSON serializers
> -------------------------
>
>                 Key: COCOON3-77
>                 URL: https://issues.apache.org/jira/browse/COCOON3-77
>             Project: Cocoon 3
>          Issue Type: Improvement
>          Components: cocoon-optional
>    Affects Versions: 3.0.0-alpha-3
>            Reporter: Andre Juffer
>            Priority: Minor
>         Attachments: EncodingJsonSerializer.java, EncodingTextSerializer.java, JsonSerializer.java,
TextEncoder.java, TextSerializer.java, pom.xml, tribc-cocoon-3.xml
>
>
> Serveral classes have been created for serializing text and JSON in the sitemap. The
JsonSerializer also checks whether the JSON text actually is valid. The organization of the
classes follows the encoding serializers (such as the EncodingHTMLSerializer).

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message