Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 164   Methods: 3
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DocumentBuilderBase.java 100% 100% 100% 100%
coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.data;
 11   
 12    import java.util.*;
 13   
 14    /*
 15    * Abstract base class for generating a document holding its elements in a
 16    * tree. Inherit from this class and create your own DocumentBuilder.
 17    * For example, have a look at XMLDocumentBuilder.
 18    *
 19    * @author Klaus Meffert
 20    * @since 2.0
 21    */
 22    public abstract class DocumentBuilderBase {
 23    /**@todo add new class DocumentCreatorBase that reads in data written by
 24    * DocumentBuilderBase */
 25   
 26    /** String containing the CVS revision. Read out via reflection!*/
 27    private final static String CVS_REVISION = "$Revision: 1.7 $";
 28   
 29    /**
 30    * Builds a document from the given input (input data + existing document).
 31    * @param a_dataholder the input structure holding the data to
 32    * be represented as a generic document
 33    * @param a_document the document to put the elements in
 34    * @throws Exception
 35    * @return the document built up by adding elements
 36    *
 37    * @author Klaus Meffert
 38    * @since 2.0
 39    */
 40  2 public Object buildDocument(final IDataCreators a_dataholder,
 41    final Object a_document)
 42    throws Exception {
 43    // Traverse over input structure.
 44    // ------------------------------
 45  2 IDataElementList tree = a_dataholder.getTree();
 46  2 int len = tree.getLength();
 47  2 IDataElement elem;
 48  2 for (int i = 0; i < len; i++) {
 49  2 elem = tree.item(i);
 50  2 doTraverse(elem, a_document, null);
 51    }
 52  2 return a_document;
 53    }
 54   
 55    /**
 56    * Recursive traversing over data tree containing elements to be transformed
 57    * into tags.
 58    * @param a_elem IDataElement
 59    * @param a_document Document
 60    * @param a_Element Element
 61    * @throws Exception
 62    *
 63    * @author Klaus Meffert
 64    * @since 2.0
 65    */
 66  7 private void doTraverse(final IDataElement a_elem, final Object a_document,
 67    final Object a_Element)
 68    throws Exception {
 69  7 String tagName = a_elem.getTagName();
 70  7 Object element = createElementGeneric(a_document, a_Element, tagName);
 71  7 Map attributes = a_elem.getAttributes();
 72  7 Set keys = attributes.keySet();
 73  7 Iterator it = keys.iterator();
 74  7 String key, value;
 75  7 while (it.hasNext()) {
 76  6 key = (String) it.next();
 77  6 value = (String) attributes.get(key);
 78  6 setAttribute(element, key, value);
 79    }
 80  7 IDataElementList list = a_elem.getChildNodes();
 81  7 if (list != null) {
 82  6 for (int j = 0; j < list.getLength(); j++) {
 83  5 IDataElement elem2 = list.item(j);
 84  5 doTraverse(elem2, a_document, element);
 85    }
 86    }
 87    }
 88   
 89    /**
 90    * Generically creates an element (Template Method).
 91    * @param a_document could be used as factory to create the element with
 92    * @param a_element null or existing element as template
 93    * @param a_tagName name of tag to create for the element
 94    * @return created element
 95    *
 96    * @author Klaus Meffert
 97    * @since 2.0
 98    */
 99  7 private Object createElementGeneric(final Object a_document,
 100    final Object a_element,
 101    final String a_tagName) {
 102  7 Object element;
 103  7 if (a_element == null) {
 104  2 element = createElement(a_document, null, a_tagName);
 105  2 documentAppendChild(a_document, element);
 106    }
 107    else {
 108  5 Object xmlElement2 = createElement(a_document, a_element, a_tagName);
 109  5 elementAppendChild(a_element, xmlElement2);
 110  5 element = xmlElement2;
 111    }
 112  7 return element;
 113    }
 114   
 115    /**
 116    * Append a child to a given document.
 117    * @param a_document to append element on (e.g. org.w3c.dom.Document)
 118    * @param a_element to append to document (e.g. org.w3c.com.Element)
 119    * @return a_document with appended element (e.g. org.w3c.dom.Document)
 120    *
 121    * @author Klaus Meffert
 122    * @since 2.0
 123    */
 124    protected abstract Object documentAppendChild(Object a_document,
 125    Object a_element);
 126   
 127    /**
 128    * Append a child to a given element.
 129    * @param a_rootElement to append childElement on (e.g. org.w3c.com.Element)
 130    * @param a_childElement to append to rootElement (e.g. org.w3c.com.Element)
 131    * @return rootElement with appended childElement (e.g. org.w3c.com.Element)
 132    *
 133    * @author Klaus Meffert
 134    * @since 2.0
 135    */
 136    protected abstract Object elementAppendChild(Object a_rootElement,
 137    Object a_childElement);
 138   
 139    /**
 140    * Creates an element with help for a given document.
 141    * @param a_document could be used as factory to create the element with
 142    * @param a_element null or existing element as template
 143    * @param a_tagName name of tag to create for the element
 144    * @return created element
 145    *
 146    * @author Klaus Meffert
 147    * @since 2.0
 148    */
 149    protected abstract Object createElement(Object a_document, Object a_element,
 150    String a_tagName);
 151   
 152    /**
 153    * Sets an attribute for a given Element.
 154    * @param a_element the Element to set an attribute for
 155    * (e.g. org.w3c.com.Element)
 156    * @param a_key the key of the attribute
 157    * @param a_value the value of the attribute
 158    *
 159    * @author Klaus Meffert
 160    * @since 2.0
 161    */
 162    protected abstract void setAttribute(Object a_element, String a_key,
 163    String a_value);
 164    }