001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.mdarad.framework.util.xml;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.dataisland.primitives.bean.Entity;
032: import org.jdom.Document;
033: import org.jdom.Element;
034: import org.jdom.Namespace;
035: import org.xml.sax.ContentHandler;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.AttributesImpl;
038:
039: public abstract class AbstractXMLConverter implements XMLConverter {
040:
041: public static final String XML_SERIALIZATION_DEFAULT_ENCODING = "UTF-8";
042: public static final int XML_SERIALIZATION_DEFAULT_INDENTING = 4;
043: public static final String ENTITY_REFERENCES = "References";
044: public static final String REFERENCES = "references";
045: public static final String ID_ATTRIBUTE = "id";
046: public static final String REFID_ATTRIBUTE = "refid";
047: public static final String LANG_ATTRIBUTE = "lang";
048: public static final Namespace XML_NAMESPACE = Namespace.XML_NAMESPACE;
049:
050: public void streamReferences(EntityMap referenceMap,
051: ContentHandler contentHandler) throws MarshallingException {
052: try {
053: Iterator typeIterator = referenceMap.keySet().iterator();
054: while (typeIterator.hasNext()) {
055: String key = (String) typeIterator.next();
056: EntitySet entitySet = referenceMap.getEntitySet(key);
057:
058: String entityName = key.substring(
059: key.lastIndexOf(".") + 1, key.length());
060:
061: contentHandler.startElement(null, entityName
062: + ENTITY_REFERENCES, "", new AttributesImpl());
063:
064: //By reflexion, go get the marshall method of the xml facade
065: Class typeClassXmlFacade = Class.forName(entitySet
066: .getXmlFacadeClassName());
067:
068: Iterator entityIterator = entitySet.iterator();
069: while (entityIterator.hasNext()) {
070: //Build method
071: Entity entity = (Entity) entityIterator.next();
072: Class[] parameterTypes = new Class[2];
073: parameterTypes[0] = entity.getClass();
074: parameterTypes[1] = ContentHandler.class;
075: Method marshallEntityMethod = typeClassXmlFacade
076: .getMethod(entitySet
077: .getMarshallEntityMethodName(),
078: parameterTypes);
079:
080: //Call method
081: Object[] parameters = new Object[2];
082: parameters[0] = entity;
083: parameters[1] = contentHandler;
084: marshallEntityMethod.invoke(null, parameters);
085: }
086:
087: contentHandler.endElement(null, entityName, "");
088: }
089: } catch (SAXException e) {
090: throw new MarshallingException(e);
091: } catch (SecurityException e) {
092: throw new MarshallingException(e);
093: } catch (IllegalArgumentException e) {
094: throw new MarshallingException(e);
095: } catch (ClassNotFoundException e) {
096: throw new MarshallingException(e);
097: } catch (NoSuchMethodException e) {
098: throw new MarshallingException(e);
099: } catch (IllegalAccessException e) {
100: throw new MarshallingException(e);
101: } catch (InvocationTargetException e) {
102: throw new MarshallingException(e);
103: }
104: }
105:
106: protected static Element getDocumentElement(String entity,
107: String refid, Document document)
108: throws UnreferencedEntityException {
109: Element element = null;
110: //Find the reference in the document
111: Element references = document.getRootElement().getChild(
112: "references");
113: Element referenceList = references.getChild(entity
114: + ENTITY_REFERENCES);
115: List childReferenceList = referenceList.getChildren();
116: Iterator i = childReferenceList.iterator();
117: while (i.hasNext()) {
118: Element elementCandidate = (Element) i.next();
119: String candidateId = elementCandidate
120: .getAttributeValue(ID_ATTRIBUTE);
121: if (candidateId != null && candidateId.equals(refid)) {
122:
123: //We found the reference
124: element = elementCandidate;
125: }
126: }
127: if (element == null) {
128: throw new UnreferencedEntityException(
129: "Could not find the entity reference for: "
130: + entity + refid);
131: }
132: return element;
133: }
134: }
|