Repository: incubator-ignite Updated Branches: refs/heads/ignite-32 29e70d523 -> 0d907c4e0
# IGNITE-32 WIP: Refactoring to static. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e8acedf7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e8acedf7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e8acedf7 Branch: refs/heads/ignite-32 Commit: e8acedf795e7611dac005bdb29d4360296fb5092 Parents: 29e70d5 Author: AKuznetsov <akuznet...@gridgain.com> Authored: Sun Dec 28 11:17:39 2014 +0700 Committer: AKuznetsov <akuznet...@gridgain.com> Committed: Sun Dec 28 11:17:39 2014 +0700 ---------------------------------------------------------------------- .../ignite/schema/xml/XmlTransformer.java | 202 ++++++++----------- 1 file changed, 89 insertions(+), 113 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e8acedf7/modules/schema-load/src/main/java/org/apache/ignite/schema/xml/XmlTransformer.java ---------------------------------------------------------------------- diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/xml/XmlTransformer.java b/modules/schema-load/src/main/java/org/apache/ignite/schema/xml/XmlTransformer.java index a123964..84b388a 100644 --- a/modules/schema-load/src/main/java/org/apache/ignite/schema/xml/XmlTransformer.java +++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/xml/XmlTransformer.java @@ -17,66 +17,21 @@ import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import java.io.*; import java.util.*; /** * Transformer that will take cache query metadata and write it to xml. */ public class XmlTransformer { - /** */ - private final Document doc; - - /** */ - private final Element beans; - - /** */ - private final Transformer transformer; - - /** - * Create transformer. - * - * @param indent Number of additional whitespace when outputting the result tree. - * @throws IllegalStateException If failed to create instance of {@code CacheQueryTypeMetadataXmlTransformer}. - */ - public XmlTransformer(int indent) throws IllegalStateException { - try { - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - doc = docBuilder.newDocument(); - doc.setXmlStandalone(true); - - beans = addElement(doc, "beans"); - beans.setAttribute("xmlns", "http://www.springframework.org/schema/beans"); - beans.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); - beans.setAttribute("xmlns:util", "http://www.springframework.org/schema/util"); - beans.setAttribute("xsi:schemaLocation", - "http://www.springframework.org/schema/beans " + - "http://www.springframework.org/schema/beans/spring-beans.xsd " + - "http://www.springframework.org/schema/util " + - "http://www.springframework.org/schema/util/spring-util.xsd"); - - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - - transformer = transformerFactory.newTransformer(); - - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); - - } - catch (ParserConfigurationException | TransformerConfigurationException e) { - throw new IllegalStateException(e); - } - } - /** * Add bean to XML document. * * @param parent Parent XML node. * @param clazz Bean class. */ - private Element addBean(Node parent, Class<?> clazz) { + private static Element addBean(Document doc, Node parent, Class<?> clazz) { Element elem = doc.createElement("bean"); elem.setAttribute("class", clazz.getName()); @@ -96,7 +51,8 @@ public class XmlTransformer { * @param attr2 Name for second attr. * @param val2 Value for second attribute. */ - private Element addElement(Node parent, String tagName, String attr1, String val1, String attr2, String val2) { + private static Element addElement(Document doc, Node parent, String tagName, + String attr1, String val1, String attr2, String val2) { Element elem = doc.createElement(tagName); if (attr1 != null) @@ -116,8 +72,8 @@ public class XmlTransformer { * @param parent Parent XML node. * @param tagName XML tag name. */ - private Element addElement(Node parent, String tagName) { - return addElement(parent, tagName, null, null, null, null); + private static Element addElement(Document doc, Node parent, String tagName) { + return addElement(doc, parent, tagName, null, null, null, null); } /** @@ -126,8 +82,8 @@ public class XmlTransformer { * @param parent Parent XML node. * @param tagName XML tag name. */ - private Element addElement(Node parent, String tagName, String attrName, String attrVal) { - return addElement(parent, tagName, attrName, attrVal, null, null); + private static Element addElement(Document doc, Node parent, String tagName, String attrName, String attrVal) { + return addElement(doc, parent, tagName, attrName, attrVal, null, null); } /** @@ -137,10 +93,10 @@ public class XmlTransformer { * @param name Value for "name" attribute * @param val Value for "value" attribute */ - private Element addProperty(Node parent, String name, String val) { + private static Element addProperty(Document doc, Node parent, String name, String val) { String valAttr = val != null ? "value" : null; - return addElement(parent, "property", "name", name, valAttr, val); + return addElement(doc, parent, "property", "name", name, valAttr, val); } /** @@ -150,14 +106,14 @@ public class XmlTransformer { * @param name Property name. * @param fields Map with fields. */ - private void addFields(Node parent, String name, Map<String, Class<?>> fields) { + private static void addFields(Document doc, Node parent, String name, Map<String, Class<?>> fields) { if (!fields.isEmpty()) { - Element prop = addProperty(parent, name, null); + Element prop = addProperty(doc, parent, name, null); - Element map = addElement(prop, "map"); + Element map = addElement(doc, prop, "map"); for (Map.Entry<String, Class<?>> item : fields.entrySet()) - addElement(map, "entry", "key", item.getKey(), "value", item.getValue().getName()); + addElement(doc, map, "entry", "key", item.getKey(), "value", item.getValue().getName()); } } @@ -168,19 +124,20 @@ public class XmlTransformer { * @param name Property name. * @param descs Map with type descriptors. */ - private void addTypeDescriptors(Node parent, String name, Collection<GridCacheQueryTypeDescriptor> descs) { + private static void addTypeDescriptors(Document doc, Node parent, String name, + Collection<GridCacheQueryTypeDescriptor> descs) { if (!descs.isEmpty()) { - Element prop = addProperty(parent, name, null); + Element prop = addProperty(doc, parent, name, null); - Element list = addElement(prop, "list"); + Element list = addElement(doc, prop, "list"); for (GridCacheQueryTypeDescriptor desc : descs) { - Element item = addBean(list, GridCacheQueryTypeDescriptor.class); + Element item = addBean(doc, list, GridCacheQueryTypeDescriptor.class); - addProperty(item, "javaName", desc.getJavaName()); - addProperty(item, "javaType", desc.getJavaType().getName()); - addProperty(item, "dbName", desc.getDbName()); - addProperty(item, "dbType", String.valueOf(desc.getDbType())); + addProperty(doc, item, "javaName", desc.getJavaName()); + addProperty(doc, item, "javaType", desc.getJavaType().getName()); + addProperty(doc, item, "dbName", desc.getDbName()); + addProperty(doc, item, "dbType", String.valueOf(desc.getDbType())); } } } @@ -190,14 +147,14 @@ public class XmlTransformer { * * @param textFields Collection with text fields. */ - private void addTextFields(Node parent, Collection<String> textFields) { + private static void addTextFields(Document doc, Node parent, Collection<String> textFields) { if (!textFields.isEmpty()) { - Element prop = addProperty(parent, "textFields", null); + Element prop = addProperty(doc, parent, "textFields", null); - Element list = addElement(prop, "list"); + Element list = addElement(doc, prop, "list"); for (String textField : textFields) - addElement(list, "value").setNodeValue(textField); + addElement(doc, list, "value").setNodeValue(textField); } } @@ -206,23 +163,24 @@ public class XmlTransformer { * * @param groups Map with indexes. */ - private void addGroups(Node parent, Map<String, LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>>> groups) { + private static void addGroups(Document doc, Node parent, + Map<String, LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>>> groups) { if (!F.isEmpty(groups)) { - Element prop = addProperty(parent, "groups", null); + Element prop = addProperty(doc, parent, "groups", null); - Element map = addElement(prop, "map"); + Element map = addElement(doc, prop, "map"); for (Map.Entry<String, LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>>> group : groups.entrySet()) { - Element entry1 = addElement(map, "entry", "key", group.getKey()); + Element entry1 = addElement(doc, map, "entry", "key", group.getKey()); - Element val1 = addElement(entry1, "map"); + Element val1 = addElement(doc, entry1, "map"); LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>> fields = group.getValue(); for (Map.Entry<String, IgniteBiTuple<Class<?>, Boolean>> field : fields.entrySet()) { - Element entry2 = addElement(val1, "entry", "key", field.getKey()); + Element entry2 = addElement(doc, val1, "entry", "key", field.getKey()); - Element val2 = addBean(entry2, IgniteBiTuple.class); + Element val2 = addBean(doc, entry2, IgniteBiTuple.class); IgniteBiTuple<Class<?>, Boolean> tuple = field.getValue(); @@ -230,8 +188,8 @@ public class XmlTransformer { assert clazz != null; - addElement(val2, "constructor-arg", null, null, "value", clazz.getName()); - addElement(val2, "constructor-arg", null, null, "value", String.valueOf(tuple.get2())); + addElement(doc, val2, "constructor-arg", null, null, "value", clazz.getName()); + addElement(doc, val2, "constructor-arg", null, null, "value", String.valueOf(tuple.get2())); } } } @@ -243,64 +201,82 @@ public class XmlTransformer { * @param parent Parent XML node. * @param meta Meta. */ - private void addCacheQueryTypeMetadata(Node parent, String pkg, GridCacheQueryTypeMetadata meta) { - Element bean = addBean(parent, GridCacheQueryTypeMetadata.class); + private static void addCacheQueryTypeMetadata(Document doc, Node parent, String pkg, + GridCacheQueryTypeMetadata meta) { + Element bean = addBean(doc, parent, GridCacheQueryTypeMetadata.class); - addProperty(bean, "type", pkg + "." + meta.getType()); + addProperty(doc, bean, "type", pkg + "." + meta.getType()); - addProperty(bean, "keyType", pkg + "." + meta.getKeyType()); + addProperty(doc, bean, "keyType", pkg + "." + meta.getKeyType()); - addProperty(bean, "schema", meta.getSchema()); + addProperty(doc, bean, "schema", meta.getSchema()); - addProperty(bean, "tableName", meta.getTableName()); + addProperty(doc, bean, "tableName", meta.getTableName()); - addTypeDescriptors(bean, "keyDescriptors", meta.getKeyDescriptors()); + addTypeDescriptors(doc, bean, "keyDescriptors", meta.getKeyDescriptors()); - addTypeDescriptors(bean, "valueDescriptors", meta.getValueDescriptors()); + addTypeDescriptors(doc, bean, "valueDescriptors", meta.getValueDescriptors()); - addFields(bean, "queryFields", meta.getQueryFields()); + addFields(doc, bean, "queryFields", meta.getQueryFields()); - addFields(bean, "ascendingFields", meta.getAscendingFields()); + addFields(doc, bean, "ascendingFields", meta.getAscendingFields()); - addFields(bean, "descendingFields", meta.getDescendingFields()); + addFields(doc, bean, "descendingFields", meta.getDescendingFields()); - addTextFields(bean, meta.getTextFields()); + addTextFields(doc, bean, meta.getTextFields()); - addGroups(bean, meta.getGroups()); - } - - /** - * @param out Output result. - * @throws TransformerException If an unrecoverable error occurs during the course of the transformation. - */ - private void transform(Result out) throws TransformerException { - transformer.transform(new DOMSource(doc), out); + addGroups(doc, bean, meta.getGroups()); } /** * Transform metadata into xml. * * @param meta Metadata to transform. - * @param out Output result. - * @throws TransformerException If an unrecoverable error occurs during the course of the transformation. + * @param out File to output result. */ - public void transform(String pkg, GridCacheQueryTypeMetadata meta, Result out) throws TransformerException { - addCacheQueryTypeMetadata(beans, pkg, meta); - - transform(out); + public static void transform(String pkg, GridCacheQueryTypeMetadata meta, File out) { + transform(pkg, Collections.singleton(meta), out); } /** * Transform metadata into xml. * * @param meta Metadata to transform. - * @param out Output result. - * @throws TransformerException If an unrecoverable error occurs during the course of the transformation. + * @param out File to output result. */ - public void transform(String pkg, Collection<GridCacheQueryTypeMetadata> meta, Result out) throws TransformerException { - for (GridCacheQueryTypeMetadata item : meta) - addCacheQueryTypeMetadata(beans, pkg, item); + public static void transform(String pkg, Collection<GridCacheQueryTypeMetadata> meta, File out) { + try { + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - transform(out); + Document doc = docBuilder.newDocument(); + doc.setXmlStandalone(true); + + Element beans = addElement(doc, doc, "beans"); + beans.setAttribute("xmlns", "http://www.springframework.org/schema/beans"); + beans.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); + beans.setAttribute("xmlns:util", "http://www.springframework.org/schema/util"); + beans.setAttribute("xsi:schemaLocation", + "http://www.springframework.org/schema/beans " + + "http://www.springframework.org/schema/beans/spring-beans.xsd " + + "http://www.springframework.org/schema/util " + + "http://www.springframework.org/schema/util/spring-util.xsd"); + + for (GridCacheQueryTypeMetadata item : meta) + addCacheQueryTypeMetadata(doc, beans, pkg, item); + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + + Transformer transformer = transformerFactory.newTransformer(); + + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + + transformer.transform(new DOMSource(doc), new StreamResult(out)); + } + catch (ParserConfigurationException | TransformerException e) { + throw new IllegalStateException(e); + } } }