1 10 package org.jgap.data; 11 12 import java.util.*; 13 14 22 public abstract class DocumentBuilderBase { 23 25 26 27 private final static String CVS_REVISION = "$Revision: 1.7 $"; 28 29 40 public Object buildDocument(final IDataCreators a_dataholder, 41 final Object a_document) 42 throws Exception { 43 IDataElementList tree = a_dataholder.getTree(); 46 int len = tree.getLength(); 47 IDataElement elem; 48 for (int i = 0; i < len; i++) { 49 elem = tree.item(i); 50 doTraverse(elem, a_document, null); 51 } 52 return a_document; 53 } 54 55 66 private void doTraverse(final IDataElement a_elem, final Object a_document, 67 final Object a_Element) 68 throws Exception { 69 String tagName = a_elem.getTagName(); 70 Object element = createElementGeneric(a_document, a_Element, tagName); 71 Map attributes = a_elem.getAttributes(); 72 Set keys = attributes.keySet(); 73 Iterator it = keys.iterator(); 74 String key, value; 75 while (it.hasNext()) { 76 key = (String ) it.next(); 77 value = (String ) attributes.get(key); 78 setAttribute(element, key, value); 79 } 80 IDataElementList list = a_elem.getChildNodes(); 81 if (list != null) { 82 for (int j = 0; j < list.getLength(); j++) { 83 IDataElement elem2 = list.item(j); 84 doTraverse(elem2, a_document, element); 85 } 86 } 87 } 88 89 99 private Object createElementGeneric(final Object a_document, 100 final Object a_element, 101 final String a_tagName) { 102 Object element; 103 if (a_element == null) { 104 element = createElement(a_document, null, a_tagName); 105 documentAppendChild(a_document, element); 106 } 107 else { 108 Object xmlElement2 = createElement(a_document, a_element, a_tagName); 109 elementAppendChild(a_element, xmlElement2); 110 element = xmlElement2; 111 } 112 return element; 113 } 114 115 124 protected abstract Object documentAppendChild(Object a_document, 125 Object a_element); 126 127 136 protected abstract Object elementAppendChild(Object a_rootElement, 137 Object a_childElement); 138 139 149 protected abstract Object createElement(Object a_document, Object a_element, 150 String a_tagName); 151 152 162 protected abstract void setAttribute(Object a_element, String a_key, 163 String a_value); 164 } 165 | Popular Tags |