Advertisement
Guest User

dougnukem

a guest
Feb 8th, 2008
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.07 KB | None | 0 0
  1. /**
  2.  * Copyright 2006 Envoi Solutions LLC
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.codehaus.jettison.mapped;
  17.  
  18. import java.io.IOException;
  19. import java.io.Writer;
  20.  
  21. import javax.xml.namespace.NamespaceContext;
  22. import javax.xml.stream.XMLStreamException;
  23.  
  24. import org.codehaus.jettison.AbstractXMLStreamWriter;
  25. import org.codehaus.jettison.json.JSONArray;
  26. import org.codehaus.jettison.json.JSONException;
  27. import org.codehaus.jettison.json.JSONObject;
  28. import org.codehaus.jettison.util.FastStack;
  29.  
  30. public class MappedXMLStreamWriter extends AbstractXMLStreamWriter {
  31.     MappedNamespaceConvention convention;
  32.     JSONObject root;
  33.     Object current;
  34.     Writer writer;
  35.     FastStack nodes = new FastStack();
  36.     String currentKey;
  37.     int depth = 0;
  38.     NamespaceContext ctx = new NullNamespaceContext();
  39.    
  40.     public MappedXMLStreamWriter(MappedNamespaceConvention convention, Writer writer) {
  41.         super();
  42.         this.convention = convention;
  43.         this.writer = writer;
  44.     }
  45.  
  46.     public void close() throws XMLStreamException {
  47.        
  48.     }
  49.  
  50.     public void flush() throws XMLStreamException {
  51.  
  52.     }
  53.  
  54.     public NamespaceContext getNamespaceContext() {
  55.         return ctx;
  56.     }
  57.  
  58.     public String getPrefix(String arg0) throws XMLStreamException {
  59.         // TODO Auto-generated method stub
  60.         return null;
  61.     }
  62.  
  63.     public Object getProperty(String arg0) throws IllegalArgumentException {
  64.         // TODO Auto-generated method stub
  65.         return null;
  66.     }
  67.  
  68.     public void setDefaultNamespace(String arg0) throws XMLStreamException {
  69.         // TODO Auto-generated method stub
  70.  
  71.     }
  72.  
  73.     public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
  74.         this.ctx = context;
  75.     }
  76.  
  77.     public void setPrefix(String arg0, String arg1) throws XMLStreamException {
  78.  
  79.     }
  80.  
  81.     public void writeAttribute(String p, String ns, String local, String value) throws XMLStreamException {
  82.         if (convention.isElement(p, ns, local)) {
  83.             writeStartElement(p, local, ns);
  84.             writeCharacters(value);
  85.             writeEndElement();
  86.             return;
  87.         }
  88.        
  89.         String key = convention.createAttributeKey(p, ns, local);
  90.         try {
  91.             makeCurrentJSONObject();
  92.            
  93.             Object o = ((JSONObject) current).opt(key);
  94.             if (o == null) {
  95.                 ((JSONObject) current).put(key, value);
  96.             }
  97.         } catch (JSONException e) {
  98.             throw new XMLStreamException(e);
  99.         }
  100.     }
  101.  
  102.     private void makeCurrentJSONObject() throws JSONException {
  103.         if (current.equals("")) {
  104.             JSONObject newCurrent = new JSONObject();
  105.             setNewValue(newCurrent);
  106.             current = newCurrent;
  107.             nodes.push(newCurrent);
  108.         }
  109.     }
  110.  
  111.     private void setNewValue(Object newCurrent) throws JSONException {
  112.         if (isJsonPrimitive(current)) {
  113.             setNewValue(newCurrent, nodes.peek());
  114.         } else {
  115.             setNewValue(newCurrent, current);
  116.         }
  117.     }
  118.    
  119.     private void setNewValue(Object newCurrent, Object node) throws JSONException {
  120.         if (node instanceof JSONObject) {
  121.             ((JSONObject) node).put(currentKey, newCurrent);
  122.         } else if (node instanceof JSONArray) {
  123.             JSONArray arr = ((JSONArray) node);
  124.             arr.put(arr.length() - 1, newCurrent);
  125.         }
  126.         current = newCurrent;
  127.     }
  128.  
  129.     public void writeAttribute(String ns, String local, String value) throws XMLStreamException {
  130.         writeAttribute(null, ns, local, value);
  131.     }
  132.  
  133.     public void writeAttribute(String local, String value) throws XMLStreamException {
  134.         writeAttribute(null, local, value);
  135.     }
  136.  
  137.     public void writeCharacters(String text) throws XMLStreamException {
  138.         try {
  139.             Object convertedPrimitive = convertToJSONPrimitive(text);
  140.             if (current instanceof String) {
  141.                 current = current + convertedPrimitive.toString();
  142.                
  143.                 setNewValue(convertedPrimitive);
  144.             } else if (current instanceof JSONArray) {
  145.                 JSONArray arr = (JSONArray) current;
  146.                 if (arr.get(arr.length()-1).equals("")) {
  147.                     arr.put(arr.length()-1, convertedPrimitive);
  148.                 } else {
  149.                     arr.put(convertedPrimitive);
  150.                 }
  151.                 current = convertedPrimitive;
  152.             }
  153.         } catch (JSONException e) {
  154.             throw new XMLStreamException(e);
  155.         }
  156.     }
  157.     //Float, Long, Double, Boolean
  158.     private Object convertToJSONPrimitive(String text) {
  159.  
  160.         Object primitive = null;
  161.         //Attempt to convert to Integer
  162.         try {
  163.             primitive = Long.parseLong(text);
  164.         } catch(Exception e) {}
  165.         //Attempt to convert to double
  166.         if(primitive == null) {
  167.             try {
  168.                 primitive = Double.parseDouble(text);
  169.             } catch(Exception e) {}            
  170.         }
  171.         //Attempt to convert to boolean
  172.         if(primitive == null) {
  173.             try {
  174.                 if(text.trim().equalsIgnoreCase("true") || text.trim().equalsIgnoreCase("false")) {
  175.                     primitive = Boolean.parseBoolean(text);
  176.                 }
  177.             } catch(Exception e) {}            
  178.         }
  179.         //By default we'll just return text
  180.         if(primitive == null) {
  181.             primitive = text;
  182.         }
  183.         return primitive;
  184.     }
  185.  
  186.     public void writeComment(String arg0) throws XMLStreamException {
  187.     }
  188.  
  189.     public void writeDefaultNamespace(String ns) throws XMLStreamException {
  190.         // TODO
  191.     }
  192.  
  193.     public void writeDTD(String arg0) throws XMLStreamException {
  194.         // TODO Auto-generated method stub
  195.  
  196.     }
  197.  
  198.     public void writeEndDocument() throws XMLStreamException {
  199.         try {
  200.             root.write(writer);
  201.             writer.flush();
  202.         } catch (JSONException e) {
  203.             throw new XMLStreamException(e);
  204.         } catch (IOException e) {
  205.             throw new XMLStreamException(e);
  206.         }
  207.     }
  208.  
  209.     public void writeEndElement() throws XMLStreamException {
  210.         if (isJsonPrimitive(current)) {
  211.             current = nodes.peek();
  212.         } else if (nodes.size() > 1 ) {
  213.             nodes.pop();
  214.             current = nodes.peek();
  215.             if (current instanceof JSONArray) {
  216.                 nodes.pop();
  217.                 current = nodes.peek();
  218.             }
  219.         }
  220.         depth--;
  221.     }
  222.  
  223.     public void writeEntityRef(String arg0) throws XMLStreamException {
  224.     }
  225.  
  226.     public void writeNamespace(String arg0, String arg1) throws XMLStreamException {
  227.     }
  228.  
  229.     public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {
  230.     }
  231.  
  232.     public void writeProcessingInstruction(String arg0) throws XMLStreamException {
  233.     }
  234.  
  235.     public void writeStartDocument() throws XMLStreamException {
  236.     }
  237.  
  238.     public void writeStartElement(String prefix, String local, String ns) throws XMLStreamException {
  239.         depth++;
  240.        
  241.         try {
  242.             if (current == null) {
  243.                 root = new JSONObject();
  244.                 current = root;
  245.                 nodes.push(root);
  246.             } else {
  247.                 makeCurrentJSONObject();
  248.             }
  249.            
  250.             currentKey = convention.createKey(prefix, ns, local);
  251.             if (current instanceof JSONArray) {
  252.                 JSONArray array = (JSONArray)current;
  253.                 if (array.get(array.length()-1).equals("")) {              
  254.                     JSONObject newNode = new JSONObject();
  255.                     newNode.put(currentKey, "");
  256.                     setNewValue(newNode);
  257.                     nodes.push(newNode);
  258.                     current = "";
  259.                 }
  260.             } else {
  261.                 Object o = ((JSONObject) current).opt(currentKey);
  262.                 // hack to support nested arrays
  263.                 if (o == null && nodes.size() > 2) {
  264.                     Object next = nodes.get(nodes.size() - 2);
  265.                     if (next instanceof JSONObject) {
  266.                         Object maybe = ((JSONObject)next).opt(currentKey);
  267.                         if (maybe != null && maybe instanceof JSONObject) {
  268.                             o = maybe;
  269.                             nodes.pop();
  270.                             current = nodes.pop();                         
  271.                         }
  272.                            
  273.                     }
  274.                 }
  275.                 if (o instanceof JSONObject || isJsonPrimitive(o)) {
  276.                     JSONArray arr = new JSONArray();
  277.                     arr.put(o);
  278.                     arr.put("");
  279.                     setNewValue(arr);
  280.                     current = arr;
  281.                     nodes.push(arr);
  282.                 } else if (o instanceof JSONArray) {
  283.                     current = "";
  284.                     ((JSONArray) o).put("");
  285.                     nodes.push(o);
  286.                 } else {
  287.                     setNewValue("");
  288.                     current = "";
  289.                 }
  290.             }
  291.         } catch (JSONException e) {
  292.             throw new XMLStreamException("Could not write start element!", e);
  293.         }
  294.     }
  295.  
  296.     private boolean isJsonPrimitive(Object o) {
  297.        if(     o instanceof String ||
  298.                o instanceof Long ||
  299.                o instanceof Double ||
  300.                o instanceof Float ||
  301.                o instanceof Boolean ||
  302.                o instanceof Integer)
  303.        {
  304.            return true;
  305.        } else {          
  306.            return false;
  307.        }
  308.     }
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement