1 23 24 package org.mdarad.framework.util.xml; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.Iterator ; 29 import java.util.List ; 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 ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.AttributesImpl ; 38 39 public abstract class AbstractXMLConverter implements XMLConverter { 40 41 public static final String XML_SERIALIZATION_DEFAULT_ENCODING = "UTF-8"; 42 public static final int XML_SERIALIZATION_DEFAULT_INDENTING = 4; 43 public static final String ENTITY_REFERENCES = "References"; 44 public static final String REFERENCES = "references"; 45 public static final String ID_ATTRIBUTE = "id"; 46 public static final String REFID_ATTRIBUTE = "refid"; 47 public static final String LANG_ATTRIBUTE = "lang"; 48 public static final Namespace XML_NAMESPACE = Namespace.XML_NAMESPACE; 49 50 public void streamReferences(EntityMap referenceMap, ContentHandler contentHandler) throws MarshallingException { 51 try { 52 Iterator typeIterator = referenceMap.keySet().iterator(); 53 while(typeIterator.hasNext()) { 54 String key = (String ) typeIterator.next(); 55 EntitySet entitySet = referenceMap.getEntitySet(key); 56 57 String entityName = key.substring(key.lastIndexOf(".") + 1, key.length()); 58 59 contentHandler.startElement(null, entityName + ENTITY_REFERENCES, "", new AttributesImpl ()); 60 61 Class typeClassXmlFacade = Class.forName(entitySet.getXmlFacadeClassName()); 63 64 Iterator entityIterator = entitySet.iterator(); 65 while(entityIterator.hasNext()) { 66 Entity entity = (Entity) entityIterator.next(); 68 Class [] parameterTypes = new Class [2]; 69 parameterTypes[0] = entity.getClass(); 70 parameterTypes[1] = ContentHandler .class; 71 Method marshallEntityMethod = typeClassXmlFacade.getMethod(entitySet.getMarshallEntityMethodName(), parameterTypes); 72 73 Object [] parameters = new Object [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 e) { 83 throw new MarshallingException(e); 84 } catch (SecurityException e) { 85 throw new MarshallingException(e); 86 } catch (IllegalArgumentException e) { 87 throw new MarshallingException(e); 88 } catch (ClassNotFoundException e) { 89 throw new MarshallingException(e); 90 } catch (NoSuchMethodException e) { 91 throw new MarshallingException(e); 92 } catch (IllegalAccessException e) { 93 throw new MarshallingException(e); 94 } catch (InvocationTargetException e) { 95 throw new MarshallingException(e); 96 } 97 } 98 99 protected static Element getDocumentElement(String entity, String refid, Document document) throws UnreferencedEntityException { 100 Element element = null; 101 Element references = document.getRootElement().getChild("references"); 103 Element referenceList = references.getChild(entity + ENTITY_REFERENCES); 104 List childReferenceList = referenceList.getChildren(); 105 Iterator i = childReferenceList.iterator(); 106 while(i.hasNext()) { 107 Element elementCandidate = (Element) i.next(); 108 String candidateId = elementCandidate.getAttributeValue(ID_ATTRIBUTE); 109 if(candidateId != null && candidateId.equals(refid)) { 110 111 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 |