|
| 1 | +package com.capgemini.cobigen.xmlplugin.inputreader; |
| 2 | + |
| 3 | +import java.nio.charset.Charset; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.w3c.dom.Document; |
| 10 | +import org.w3c.dom.Element; |
| 11 | +import org.w3c.dom.NamedNodeMap; |
| 12 | +import org.w3c.dom.Node; |
| 13 | +import org.w3c.dom.NodeList; |
| 14 | + |
| 15 | +import com.capgemini.cobigen.extension.IInputReader; |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @author fkreis (10.11.2014) |
| 20 | + */ |
| 21 | +public class XmlInputReader implements IInputReader { |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritDoc} |
| 25 | + * @author fkreis (10.11.2014) |
| 26 | + */ |
| 27 | + @Override |
| 28 | + public boolean isValidInput(Object input) { |
| 29 | + if (input instanceof Document) { |
| 30 | + return true; |
| 31 | + } else { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritDoc} |
| 39 | + * @author fkreis (10.11.2014) |
| 40 | + */ |
| 41 | + @Override |
| 42 | + public Map<String, Object> createModel(Object input) { |
| 43 | + if (isValidInput(input)) { |
| 44 | + Document doc = (Document) input; |
| 45 | + Element rootElement = doc.getDocumentElement(); |
| 46 | + Map<String, Object> model = new HashMap<>(); |
| 47 | + model.put(rootElement.getNodeName(), deriveSubModel(rootElement)); |
| 48 | + return new HashMap<>(model); |
| 49 | + } else { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritDoc}<br> |
| 57 | + * <br> |
| 58 | + * Since the {@link XmlInputReader} does not support multiple input objects it always returns |
| 59 | + * <code>false</code>. |
| 60 | + * @author fkreis (10.11.2014) |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public boolean combinesMultipleInputObjects(Object input) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritDoc}<br> |
| 69 | + * <br> |
| 70 | + * Since the {@link XmlInputReader} does not support multiple input objects it always returns an empty |
| 71 | + * {@link List}. |
| 72 | + * @author fkreis (10.11.2014) |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public List<Object> getInputObjects(Object input, Charset inputCharset) { |
| 76 | + List<Object> emptyList = new LinkedList<>(); |
| 77 | + return emptyList; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritDoc} <br> |
| 82 | + * <br> |
| 83 | + * Since the {@link XmlInputReader} does not provide any template methods it always returns an empty |
| 84 | + * {@link Map}. |
| 85 | + * @author fkreis (10.11.2014) |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public Map<String, Object> getTemplateMethods(Object input) { |
| 89 | + Map<String, Object> emptyMap = new HashMap<>(); |
| 90 | + return emptyMap; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param input |
| 95 | + * the element the model should derived from |
| 96 | + * @return derived sub model |
| 97 | + * @author fkreis (17.11.2014) |
| 98 | + */ |
| 99 | + private Map<String, Object> deriveSubModel(Element input) { |
| 100 | + // prepare result object |
| 101 | + Map<String, Object> submodel = new HashMap<>(); |
| 102 | + |
| 103 | + // get all child nodes |
| 104 | + NodeList childList = input.getChildNodes(); |
| 105 | + |
| 106 | + // put element's name into the model |
| 107 | + submodel.put(ModelConstant.NODE_NAME, input.getNodeName()); |
| 108 | + |
| 109 | + // put element's attributes into a list and as single attributes into the model |
| 110 | + NamedNodeMap attributeNodes = input.getAttributes(); |
| 111 | + List<Map<String, Object>> attrList = new LinkedList<>(); |
| 112 | + for (int i = 0; i < attributeNodes.getLength(); i++) { |
| 113 | + Map<String, Object> att = new HashMap<>(); |
| 114 | + Node currentAttrNode = attributeNodes.item(i); |
| 115 | + String attrName = currentAttrNode.getNodeName(); |
| 116 | + String attrValue = currentAttrNode.getNodeValue(); |
| 117 | + |
| 118 | + // as list |
| 119 | + att.put(ModelConstant.ATTRIBUTE_NAME, attrName); |
| 120 | + att.put(ModelConstant.ATTRIBUTE_VALUE, attrValue); |
| 121 | + attrList.add(att); |
| 122 | + |
| 123 | + // as single attributes |
| 124 | + submodel.put(ModelConstant.SINGLE_ATTRIBUTE + attrName, attrValue); |
| 125 | + } |
| 126 | + submodel.put(ModelConstant.ATTRIBUTES, attrList); |
| 127 | + |
| 128 | + // put text nodes (pcdata) into the model |
| 129 | + List<String> textNodeList = new LinkedList<>(); |
| 130 | + String textcontent = ""; |
| 131 | + for (int i = 0; i < childList.getLength(); i++) { |
| 132 | + Node currentChild = childList.item(i); |
| 133 | + if (currentChild.getNodeType() == Node.TEXT_NODE) { |
| 134 | + String currentTextContent = currentChild.getTextContent().trim(); |
| 135 | + // as String List |
| 136 | + if (!currentTextContent.equals("")) { |
| 137 | + textNodeList.add(currentTextContent); |
| 138 | + } |
| 139 | + // as concatenated String |
| 140 | + textcontent += currentTextContent; |
| 141 | + } |
| 142 | + } |
| 143 | + submodel.put(ModelConstant.TEXT_NODES, textNodeList); |
| 144 | + submodel.put(ModelConstant.TEXT_CONTENT, textcontent); |
| 145 | + |
| 146 | + // put element child nodes into the model as list (call method recursively) |
| 147 | + List<Map<String, Object>> modelChildList = new LinkedList<>(); |
| 148 | + List<String> blackList = new LinkedList<>(); |
| 149 | + for (int i = 0; i < childList.getLength(); i++) { |
| 150 | + Node currentChild = childList.item(i); |
| 151 | + if (currentChild.getNodeType() == Node.ELEMENT_NODE) { |
| 152 | + Map<String, Object> currentChildModel = deriveSubModel((Element) currentChild); |
| 153 | + |
| 154 | + // as list |
| 155 | + modelChildList.add(currentChildModel); |
| 156 | + |
| 157 | + // as single child, only add if its name is unique, otherwise it just can be provided via |
| 158 | + // CHILDREN list |
| 159 | + String childname = currentChild.getNodeName(); |
| 160 | + if (blackList.contains(childname)) { |
| 161 | + |
| 162 | + } else if (submodel.containsKey(childname)) { |
| 163 | + submodel.remove(childname); |
| 164 | + blackList.add(childname); |
| 165 | + } else { |
| 166 | + submodel.put(childname, currentChildModel); |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + } |
| 171 | + submodel.put(ModelConstant.CHILDREN, modelChildList); |
| 172 | + |
| 173 | + return new HashMap<>(submodel); |
| 174 | + } |
| 175 | +} |
0 commit comments