/*
Mdarad-Toolobox is a collection of tools for Architected RAD
(Rapid Application Development) based on an MDA approach.
The toolbox contains frameworks and generators for many environments
(JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
applications from a design Model
Copyright (C) 2004-2005 Elapse Technologies Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mdarad.framework.util.xml;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import org.dataisland.primitives.bean.Entity;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public abstract class AbstractXMLConverter implements XMLConverter {
public static final String XML_SERIALIZATION_DEFAULT_ENCODING = "UTF-8";
public static final int XML_SERIALIZATION_DEFAULT_INDENTING = 4;
public static final String ENTITY_REFERENCES = "References";
public static final String REFERENCES = "references";
public static final String ID_ATTRIBUTE = "id";
public static final String REFID_ATTRIBUTE = "refid";
public static final String LANG_ATTRIBUTE = "lang";
public static final Namespace XML_NAMESPACE = Namespace.XML_NAMESPACE;
public void streamReferences(EntityMap referenceMap, ContentHandler contentHandler) throws MarshallingException {
try {
Iterator typeIterator = referenceMap.keySet().iterator();
while(typeIterator.hasNext()) {
String key = (String) typeIterator.next();
EntitySet entitySet = referenceMap.getEntitySet(key);
String entityName = key.substring(key.lastIndexOf(".") + 1, key.length());
contentHandler.startElement(null, entityName + ENTITY_REFERENCES, "", new AttributesImpl());
//By reflexion, go get the marshall method of the xml facade
Class typeClassXmlFacade = Class.forName(entitySet.getXmlFacadeClassName());
Iterator entityIterator = entitySet.iterator();
while(entityIterator.hasNext()) {
//Build method
Entity entity = (Entity) entityIterator.next();
Class[] parameterTypes = new Class[2];
parameterTypes[0] = entity.getClass();
parameterTypes[1] = ContentHandler.class;
Method marshallEntityMethod = typeClassXmlFacade.getMethod(entitySet.getMarshallEntityMethodName(), parameterTypes);
//Call method
Object[] parameters = new Object[2];
parameters[0] = entity;
parameters[1] = contentHandler;
marshallEntityMethod.invoke(null, parameters);
}
contentHandler.endElement(null, entityName, "");
}
} catch (SAXException e) {
throw new MarshallingException(e);
} catch (SecurityException e) {
throw new MarshallingException(e);
} catch (IllegalArgumentException e) {
throw new MarshallingException(e);
} catch (ClassNotFoundException e) {
throw new MarshallingException(e);
} catch (NoSuchMethodException e) {
throw new MarshallingException(e);
} catch (IllegalAccessException e) {
throw new MarshallingException(e);
} catch (InvocationTargetException e) {
throw new MarshallingException(e);
}
}
protected static Element getDocumentElement(String entity, String refid, Document document) throws UnreferencedEntityException {
Element element = null;
//Find the reference in the document
Element references = document.getRootElement().getChild("references");
Element referenceList = references.getChild(entity + ENTITY_REFERENCES);
List childReferenceList = referenceList.getChildren();
Iterator i = childReferenceList.iterator();
while(i.hasNext()) {
Element elementCandidate = (Element) i.next();
String candidateId = elementCandidate.getAttributeValue(ID_ATTRIBUTE);
if(candidateId != null && candidateId.equals(refid)) {
//We found the reference
element = elementCandidate;
}
}
if(element == null) {
throw new UnreferencedEntityException("Could not find the entity reference for: " + entity + refid);
}
return element;
}
}
|