cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amania...@apache.org
Subject svn commit: r760798 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml: SerializableEntity.java XMLDecoder.java XMLEncoder.java XMLMappingDescriptor.java XMLUtil.java
Date Wed, 01 Apr 2009 07:22:50 GMT
Author: amaniatis
Date: Wed Apr  1 07:22:48 2009
New Revision: 760798

URL: http://svn.apache.org/viewvc?rev=760798&view=rev
Log:
Made a couple of package scoped methods into protected scope. This allows user to override
certain parts of the XML serialisation functionality.
At the same time, implemented generics in this whole xml package.

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/SerializableEntity.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLDecoder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLEncoder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLMappingDescriptor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLUtil.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/SerializableEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/SerializableEntity.java?rev=760798&r1=760797&r2=760798&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/SerializableEntity.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/SerializableEntity.java
Wed Apr  1 07:22:48 2009
@@ -58,11 +58,11 @@
 
     public void encodeAsXML(XMLEncoder encoder) {
         if (object instanceof Collection) {
-            Collection c = (Collection) object;
+            Collection<?> c = (Collection<?>) object;
             if (!c.isEmpty()) {
 
                 // push the first node, and create the rest as peers.
-                Iterator it = c.iterator();
+                Iterator<?> it = c.iterator();
                 encodeObject(encoder, it.next(), true);
                 while (it.hasNext()) {
                     encodeObject(encoder, it.next(), false);
@@ -87,10 +87,7 @@
     void encodeObject(XMLEncoder encoder, Object object, boolean push) {
         encoder.setRoot(descriptor.getAttribute("xmlTag"), null, push);
 
-        Iterator it = XMLUtil.getChildren(descriptor).iterator();
-        while (it.hasNext()) {
-
-            Element property = (Element) it.next();
+        for (Element property : XMLUtil.getChildren(descriptor)) {
             String xmlTag = property.getAttribute("xmlTag");
             String name = property.getAttribute("name");
             Object value = PropertyUtils.getProperty(object, name);

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLDecoder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLDecoder.java?rev=760798&r1=760797&r2=760798&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLDecoder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLDecoder.java
Wed Apr  1 07:22:48 2009
@@ -28,16 +28,14 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
 import javax.xml.parsers.DocumentBuilder;
 
 import org.apache.cayenne.CayenneRuntimeException;
-import org.apache.cayenne.Persistent;
 import org.apache.cayenne.ObjectContext;
-import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.Persistent;
 import org.apache.cayenne.reflect.PropertyUtils;
 import org.apache.cayenne.util.Util;
 import org.w3c.dom.Document;
@@ -51,7 +49,7 @@
  */
 public class XMLDecoder {
 
-    static final Map classMapping = new HashMap();
+    static final Map<String, Class<?>> classMapping = new HashMap<String,
Class<?>>();
 
     static {
         classMapping.put("boolean", Boolean.class);
@@ -176,7 +174,7 @@
      * @param child The XML element.
      * @return The tag's value.
      */
-    private Object decodeObject(Element child) {
+    protected Object decodeObject(Element child) {
 
         if (null == child) {
             return null;
@@ -191,7 +189,7 @@
         }
 
         // temp hack to support primitives...
-        Class objectClass = (Class) classMapping.get(type);
+        Class<?> objectClass = classMapping.get(type);
         if (null == objectClass) {
             try {
                 objectClass = Class.forName(type);
@@ -264,7 +262,7 @@
             }
 
             // handle all other primitive types...
-            Constructor c = objectClass.getConstructor(String.class);
+            Constructor<?> c = objectClass.getConstructor(String.class);
             return c.newInstance(text);
         }
         catch (Exception e) {
@@ -333,7 +331,7 @@
      * @return The decoded object.
      * @throws CayenneRuntimeException
      */
-    private Object decodeElement(Element element) throws CayenneRuntimeException {
+    protected Object decodeElement(Element element) throws CayenneRuntimeException {
 
         // Update root to be the supplied xml element. This is necessary as
         // root is used for decoding properties.
@@ -369,13 +367,13 @@
      * @return A List of all the decoded objects.
      * @throws CayenneRuntimeException
      */
-    private Collection decodeCollection(Element xml) throws CayenneRuntimeException {
+    protected Collection<Object> decodeCollection(Element xml) throws CayenneRuntimeException
{
 
-        Collection ret;
+        Collection<Object> ret;
         try {
             String parentClass = ((Element) xml.getParentNode()).getAttribute("type");
             Object property = Class.forName(parentClass).newInstance();
-            Collection c = (Collection) PropertyUtils.getProperty(property, xml
+            Collection<Object> c = (Collection<Object>) PropertyUtils.getProperty(property,
xml
                     .getNodeName());
 
             ret = c.getClass().newInstance();
@@ -389,12 +387,8 @@
         // Each child of the root corresponds to an XML representation of
         // the object. The idea is decode each of those into an object and add them to the
         // list to be returned.
-        Iterator it = XMLUtil
-                .getChildren(xml.getParentNode(), xml.getNodeName())
-                .iterator();
-        while (it.hasNext()) {
+        for (Element e : XMLUtil.getChildren(xml.getParentNode(), xml.getNodeName())) {
             // Decode the object.
-            Element e = (Element) it.next();
             decodedCollections.add(e);
             Object o = decodeElement(e);
 
@@ -412,7 +406,7 @@
      * @return The list of decoded DataObjects.
      * @throws CayenneRuntimeException
      */
-    public static List decodeList(Reader xml) throws CayenneRuntimeException {
+    public static List<Object> decodeList(Reader xml) throws CayenneRuntimeException
{
         return decodeList(xml, null, null);
     }
 
@@ -424,7 +418,7 @@
      * @return The list of decoded DataObjects.
      * @throws CayenneRuntimeException
      */
-    public static List decodeList(Reader xml, ObjectContext objectContext)
+    public static List<Object> decodeList(Reader xml, ObjectContext objectContext)
             throws CayenneRuntimeException {
         return decodeList(xml, null, objectContext);
     }
@@ -439,7 +433,7 @@
      * @return The list of decoded DataObjects.
      * @throws CayenneRuntimeException
      */
-    public static List decodeList(Reader xml, String mappingUrl)
+    public static List<Object> decodeList(Reader xml, String mappingUrl)
             throws CayenneRuntimeException {
         return decodeList(xml, mappingUrl, null);
     }
@@ -455,16 +449,16 @@
      * @return The list of decoded DataObjects.
      * @throws CayenneRuntimeException
      */
-    public static List decodeList(Reader xml, String mappingUrl, ObjectContext objectContext)
+    public static List<Object> decodeList(Reader xml, String mappingUrl, ObjectContext
objectContext)
             throws CayenneRuntimeException {
 
         XMLDecoder decoder = new XMLDecoder(objectContext);
         Element listRoot = parse(xml).getDocumentElement();
 
-        List ret;
+        List<Object> ret;
         try {
             String parentClass = listRoot.getAttribute("type");
-            ret = (List) Class.forName(parentClass).newInstance();
+            ret = (List<Object>) Class.forName(parentClass).newInstance();
         }
         catch (Exception ex) {
             throw new CayenneRuntimeException(
@@ -480,10 +474,8 @@
         // Each child of the root corresponds to an XML representation of
         // the object. The idea is decode each of those into an object and add them to the
         // list to be returned.
-        Iterator it = XMLUtil.getChildren(listRoot).iterator();
-        while (it.hasNext()) {
+        for (Element e : XMLUtil.getChildren(listRoot)) {
             // Decode the object.
-            Element e = (Element) it.next();
             decoder.decodedCollections.add(e);
 
             // Decode the item using the appropriate decoding method.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLEncoder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLEncoder.java?rev=760798&r1=760797&r2=760798&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLEncoder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLEncoder.java
Wed Apr  1 07:22:48 2009
@@ -23,7 +23,6 @@
 import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Date;
-import java.util.Iterator;
 
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Result;
@@ -123,7 +122,7 @@
     /**
      * Resets the encoder to process a new object tree.
      */
-    void initDocument(String rootTag, String type) {
+    protected void initDocument(String rootTag, String type) {
         this.document = XMLUtil.newBuilder().newDocument();
         this.parent = document;
 
@@ -139,7 +138,7 @@
     /**
      * Returns a root DOM node of the encoder.
      */
-    Node getRootNode(boolean forceSyntheticRoot) {
+    protected Node getRootNode(boolean forceSyntheticRoot) {
         if (document == null) {
             return null;
         }
@@ -154,7 +153,7 @@
         return root;
     }
 
-    String nodeToString(Node rootNode) {
+    protected String nodeToString(Node rootNode) {
 
         StringWriter out = new StringWriter();
         Result result = new StreamResult(out);
@@ -175,7 +174,7 @@
         return out.toString().trim() + System.getProperty("line.separator");
     }
 
-    void setRoot(String xmlTag, String type, boolean push) {
+    protected void setRoot(String xmlTag, String type, boolean push) {
 
         // all public methods must implicitly init the document
         if (!inProgress) {
@@ -198,7 +197,7 @@
         }
     }
 
-    void encodeProperty(String xmlTag, Object value, boolean useType) {
+    protected void encodeProperty(String xmlTag, Object value, boolean useType) {
         // all public methods must be able to implicitly init the document
         if (!inProgress) {
             String type = (useType && value != null) ? value.getClass().getName()
: null;
@@ -212,14 +211,14 @@
             encodeSerializable(xmlTag, (XMLSerializable) value);
         }
         else if (value instanceof Collection) {
-            encodeCollection(xmlTag, (Collection) value, useType);
+            encodeCollection(xmlTag, (Collection<Object>) value, useType);
         }
         else {
             encodeSimple(xmlTag, value, useType);
         }
     }
 
-    void encodeSimple(String xmlTag, Object object, boolean useType) {
+    protected void encodeSimple(String xmlTag, Object object, boolean useType) {
 
         // simple properties will not call setRoot, so push manually
         Element node = push(xmlTag);
@@ -240,7 +239,7 @@
         pop();
     }
 
-    void encodeSerializable(String xmlTag, XMLSerializable object) {
+    protected void encodeSerializable(String xmlTag, XMLSerializable object) {
         // don't allow children to reset XML tag name ... unless they are at the root
         // level
         if (document.getDocumentElement() != parent) {
@@ -259,13 +258,11 @@
      * @param xmlTag The name of the root XML element for the encoded collection.
      * @param c The collection to encode.
      */
-    void encodeCollection(String xmlTag, Collection c, boolean useType) {
-
-        Iterator it = c.iterator();
-        while (it.hasNext()) {
-            // encode collection without doing push/pop so that its elements are encoded
-            // without an intermediate grouping node.
-            encodeProperty(xmlTag, it.next(), useType);
+    protected void encodeCollection(String xmlTag, Collection<Object> c, boolean useType)
{
+        // encode collection without doing push/pop so that its elements are encoded
+        // without an intermediate grouping node.
+        for (Object o : c) {
+            encodeProperty(xmlTag, o, useType);
         }
 
         if (c.size() == 1) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLMappingDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLMappingDescriptor.java?rev=760798&r1=760797&r2=760798&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLMappingDescriptor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLMappingDescriptor.java
Wed Apr  1 07:22:48 2009
@@ -21,15 +21,13 @@
 
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 
 import javax.xml.parsers.DocumentBuilder;
 
 import org.apache.cayenne.CayenneRuntimeException;
-import org.apache.cayenne.Persistent;
 import org.apache.cayenne.ObjectContext;
-import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.Persistent;
 import org.apache.cayenne.reflect.PropertyUtils;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -75,10 +73,7 @@
         }
 
         Map<String, SerializableEntity> entities = new HashMap<String, SerializableEntity>();
-        Iterator it = XMLUtil.getChildren(root).iterator();
-        while (it.hasNext()) {
-            Element e = (Element) it.next();
-
+        for (Element e : XMLUtil.getChildren(root)) {
             SerializableEntity entity = new SerializableEntity(this, e);
             String tag = e.getAttribute("xmlTag");
             entities.put(tag, entity);
@@ -115,9 +110,8 @@
 
         // We want to read each value from the XML file and then set the corresponding
         // property value in the object to be returned.
-        for (Iterator it = XMLUtil.getChildren(xml).iterator(); it.hasNext();) {
-            Element value = (Element) it.next();
-            decodeProperty(ret, rootEntity.getDescriptor(), value);
+        for (Element e : XMLUtil.getChildren(xml)) {
+            decodeProperty(ret, rootEntity.getDescriptor(), e);
         }
 
         return ret;
@@ -141,9 +135,7 @@
      * @return A name of the Java property mapped for the XML tag.
      */
     private String getPropertyMappingName(Element entityMapping, String propertyXmlTag) {
-        for (Iterator it = XMLUtil.getChildren(entityMapping).iterator(); it.hasNext();)
{
-            Element propertyMapping = (Element) it.next();
-
+        for (Element propertyMapping : XMLUtil.getChildren(entityMapping)) {
             if (propertyXmlTag.equals(propertyMapping.getAttribute("xmlTag"))) {
                 return propertyMapping.getAttribute("name");
             }
@@ -185,9 +177,7 @@
 
             // Decode each of the property's children, setting values in the newly
             // created object.
-            Iterator it = XMLUtil.getChildren(propertyData).iterator();
-            while (it.hasNext()) {
-                Element child = (Element) it.next();
+            for (Element child : XMLUtil.getChildren(propertyData)) {
                 decodeProperty(o, targetEntityMapping.getDescriptor(), child);
             }
 
@@ -251,8 +241,7 @@
         NamedNodeMap attributes = objectData.getAttributes();
         for (int i = 0; i < attributes.getLength(); i++) {
             Attr attribute = (Attr) attributes.item(i);
-            String propertyName = getPropertyMappingName(entityMapping, attribute
-                    .getName());
+            String propertyName = getPropertyMappingName(entityMapping, attribute.getName());
 
             if (propertyName != null) {
                 PropertyUtils.setProperty(object, propertyName, attribute.getValue());

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLUtil.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLUtil.java?rev=760798&r1=760797&r2=760798&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLUtil.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/xml/XMLUtil.java
Wed Apr  1 07:22:48 2009
@@ -20,15 +20,15 @@
 package org.apache.cayenne.xml;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.commons.collections.Predicate;
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.commons.collections.Predicate;
+import org.apache.commons.collections.CollectionUtils;
 import org.w3c.dom.CharacterData;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -65,13 +65,10 @@
     /**
      * Moves all children of the oldParent to the newParent
      */
-    static List replaceParent(Node oldParent, Node newParent) {
-
-        List children = XMLUtil.getChildren(oldParent);
+    static List<Element> replaceParent(Node oldParent, Node newParent) {
 
-        Iterator it = children.iterator();
-        while (it.hasNext()) {
-            Element child = (Element) it.next();
+        List<Element> children = XMLUtil.getChildren(oldParent);
+        for (Node child : children) {
             oldParent.removeChild(child);
             newParent.appendChild(child);
         }
@@ -125,34 +122,37 @@
     /**
      * Returns all elements among the direct children that have a matching name.
      */
-    static List getChildren(Node node, final String name) {
+    static List<Element> getChildren(Node node, final String name) {
+        
         Predicate p = new Predicate() {
-
             public boolean evaluate(Object object) {
                 if (object instanceof Element) {
                     Element e = (Element) object;
                     return name.equals(e.getNodeName());
                 }
-
                 return false;
             }
         };
 
-        return allMatches(node.getChildNodes(), p);
+        return (List<Element>) CollectionUtils.select(getChildren(node), p);
     }
 
     /**
      * Returns all children of a given Node that are Elements.
      */
-    static List getChildren(Node node) {
-        Predicate p = new Predicate() {
-
-            public boolean evaluate(Object object) {
-                return object instanceof Element;
+    static List<Element> getChildren(Node node) {
+        NodeList list = node.getChildNodes();
+        int len = list.getLength();
+        
+        List<Element> children = new ArrayList<Element>(len);
+        for (int i = 0; i < len; i++) {
+            Node child = list.item(i);
+            if (child instanceof Element) {
+                children.add((Element) child);
             }
-        };
+        }
 
-        return allMatches(node.getChildNodes(), p);
+        return children;
     }
 
     private static Node firstMatch(NodeList list, Predicate predicate) {
@@ -168,17 +168,4 @@
         return null;
     }
 
-    private static List allMatches(NodeList list, Predicate predicate) {
-        int len = list.getLength();
-        List children = new ArrayList(len);
-
-        for (int i = 0; i < len; i++) {
-            Node node = list.item(i);
-            if (predicate.evaluate(node)) {
-                children.add(node);
-            }
-        }
-
-        return children;
-    }
 }



Mime
View raw message