KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > xml > AbstractXMLConverter


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23
24 package org.mdarad.framework.util.xml;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.dataisland.primitives.bean.Entity;
32 import org.jdom.Document;
33 import org.jdom.Element;
34 import org.jdom.Namespace;
35 import org.xml.sax.ContentHandler JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.AttributesImpl JavaDoc;
38
39 public abstract class AbstractXMLConverter implements XMLConverter {
40     
41     public static final String JavaDoc XML_SERIALIZATION_DEFAULT_ENCODING = "UTF-8";
42     public static final int XML_SERIALIZATION_DEFAULT_INDENTING = 4;
43     public static final String JavaDoc ENTITY_REFERENCES = "References";
44     public static final String JavaDoc REFERENCES = "references";
45     public static final String JavaDoc ID_ATTRIBUTE = "id";
46     public static final String JavaDoc REFID_ATTRIBUTE = "refid";
47     public static final String JavaDoc LANG_ATTRIBUTE = "lang";
48     public static final Namespace XML_NAMESPACE = Namespace.XML_NAMESPACE;
49     
50     public void streamReferences(EntityMap referenceMap, ContentHandler JavaDoc contentHandler) throws MarshallingException {
51         try {
52             Iterator JavaDoc typeIterator = referenceMap.keySet().iterator();
53             while(typeIterator.hasNext()) {
54                 String JavaDoc key = (String JavaDoc) typeIterator.next();
55                 EntitySet entitySet = referenceMap.getEntitySet(key);
56                 
57                 String JavaDoc entityName = key.substring(key.lastIndexOf(".") + 1, key.length());
58                 
59                 contentHandler.startElement(null, entityName + ENTITY_REFERENCES, "", new AttributesImpl JavaDoc());
60
61                 //By reflexion, go get the marshall method of the xml facade
62
Class JavaDoc typeClassXmlFacade = Class.forName(entitySet.getXmlFacadeClassName());
63                 
64                 Iterator JavaDoc entityIterator = entitySet.iterator();
65                 while(entityIterator.hasNext()) {
66                     //Build method
67
Entity entity = (Entity) entityIterator.next();
68                     Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
69                     parameterTypes[0] = entity.getClass();
70                     parameterTypes[1] = ContentHandler JavaDoc.class;
71                     Method JavaDoc marshallEntityMethod = typeClassXmlFacade.getMethod(entitySet.getMarshallEntityMethodName(), parameterTypes);
72                     
73                     //Call method
74
Object JavaDoc[] parameters = new Object JavaDoc[2];
75                     parameters[0] = entity;
76                     parameters[1] = contentHandler;
77                     marshallEntityMethod.invoke(null, parameters);
78                 }
79                 
80                 contentHandler.endElement(null, entityName, "");
81             }
82         } catch (SAXException JavaDoc e) {
83             throw new MarshallingException(e);
84         } catch (SecurityException JavaDoc e) {
85             throw new MarshallingException(e);
86         } catch (IllegalArgumentException JavaDoc e) {
87             throw new MarshallingException(e);
88         } catch (ClassNotFoundException JavaDoc e) {
89             throw new MarshallingException(e);
90         } catch (NoSuchMethodException JavaDoc e) {
91             throw new MarshallingException(e);
92         } catch (IllegalAccessException JavaDoc e) {
93             throw new MarshallingException(e);
94         } catch (InvocationTargetException JavaDoc e) {
95             throw new MarshallingException(e);
96         }
97     }
98     
99     protected static Element getDocumentElement(String JavaDoc entity, String JavaDoc refid, Document document) throws UnreferencedEntityException {
100         Element element = null;
101         //Find the reference in the document
102
Element references = document.getRootElement().getChild("references");
103         Element referenceList = references.getChild(entity + ENTITY_REFERENCES);
104         List JavaDoc childReferenceList = referenceList.getChildren();
105         Iterator JavaDoc i = childReferenceList.iterator();
106         while(i.hasNext()) {
107             Element elementCandidate = (Element) i.next();
108             String JavaDoc candidateId = elementCandidate.getAttributeValue(ID_ATTRIBUTE);
109             if(candidateId != null && candidateId.equals(refid)) {
110                 
111                 //We found the reference
112
element = elementCandidate;
113             }
114         }
115         if(element == null) {
116             throw new UnreferencedEntityException("Could not find the entity reference for: " + entity + refid);
117         }
118         return element;
119     }
120 }
121
Popular Tags