| 1 | /*******************************************************************************\r |
| 2 | * Copyright (c) 2010 Ericsson\r |
| 3 | * \r |
| 4 | * All rights reserved. This program and the accompanying materials are\r |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which\r |
| 6 | * accompanies this distribution, and is available at\r |
| 7 | * http://www.eclipse.org/legal/epl-v10.html\r |
| 8 | * \r |
| 9 | * Contributors:\r |
| 10 | * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation\r |
| 11 | *******************************************************************************/\r |
| 12 | \r |
| 13 | package org.eclipse.linuxtools.tmf.filter.xml;\r |
| 14 | \r |
| 15 | import java.io.File;\r |
| 16 | import java.io.IOException;\r |
| 17 | \r |
| 18 | import javax.xml.parsers.DocumentBuilder;\r |
| 19 | import javax.xml.parsers.DocumentBuilderFactory;\r |
| 20 | import javax.xml.parsers.ParserConfigurationException;\r |
| 21 | import javax.xml.transform.Transformer;\r |
| 22 | import javax.xml.transform.TransformerConfigurationException;\r |
| 23 | import javax.xml.transform.TransformerException;\r |
| 24 | import javax.xml.transform.TransformerFactory;\r |
| 25 | import javax.xml.transform.dom.DOMSource;\r |
| 26 | import javax.xml.transform.stream.StreamResult;\r |
| 27 | \r |
| 28 | import org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode;\r |
| 29 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterAndNode;\r |
| 30 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterCompareNode;\r |
| 31 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterContainsNode;\r |
| 32 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterEqualsNode;\r |
| 33 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterEventTypeNode;\r |
| 34 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterMatchesNode;\r |
| 35 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterNode;\r |
| 36 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterOrNode;\r |
| 37 | import org.w3c.dom.Document;\r |
| 38 | import org.w3c.dom.Element;\r |
| 39 | \r |
| 40 | /**\r |
| 41 | * <b><u>FilterXMLParser</u></b>\r |
| 42 | * <p>\r |
| 43 | * This is SAX based XML parser\r |
| 44 | * <p>\r |
| 45 | */\r |
| 46 | public class TmfFilterXMLWriter {\r |
| 47 | \r |
| 48 | private Document document = null;\r |
| 49 | \r |
| 50 | /**\r |
| 51 | * The XMLParser constructor\r |
| 52 | * \r |
| 53 | * @param uri The XML file to parse\r |
| 54 | * @throws ParserConfigurationException \r |
| 55 | */ \r |
| 56 | public TmfFilterXMLWriter(final ITmfFilterTreeNode root) throws IOException, ParserConfigurationException {\r |
| 57 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();\r |
| 58 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();\r |
| 59 | document = documentBuilder.newDocument();\r |
| 60 | \r |
| 61 | Element rootElement = document.createElement(root.getNodeName());\r |
| 62 | document.appendChild(rootElement);\r |
| 63 | \r |
| 64 | for (ITmfFilterTreeNode node : root.getChildren()) {\r |
| 65 | buildXMLTree(document, node, rootElement);\r |
| 66 | }\r |
| 67 | }\r |
| 68 | \r |
| 69 | /**\r |
| 70 | * The Tree to XML parser\r |
| 71 | * \r |
| 72 | * @param document The XML document\r |
| 73 | * @param treenode The node to write\r |
| 74 | * @param parentElement The XML element of the parent\r |
| 75 | */ \r |
| 76 | public static void buildXMLTree(final Document document, final ITmfFilterTreeNode treenode, Element parentElement) {\r |
| 77 | Element element = document.createElement(treenode.getNodeName());\r |
| 78 | \r |
| 79 | if (treenode instanceof TmfFilterNode) {\r |
| 80 | \r |
| 81 | TmfFilterNode node = (TmfFilterNode) treenode;\r |
| 82 | element.setAttribute(TmfFilterNode.NAME_ATTR, node.getFilterName());\r |
| 83 | \r |
| 84 | } else if (treenode instanceof TmfFilterEventTypeNode) {\r |
| 85 | \r |
| 86 | TmfFilterEventTypeNode node = (TmfFilterEventTypeNode) treenode;\r |
| 87 | element.setAttribute(TmfFilterEventTypeNode.TYPE_ATTR, node.getEventType());\r |
| 88 | element.setAttribute(TmfFilterEventTypeNode.NAME_ATTR, node.getName());\r |
| 89 | \r |
| 90 | } else if (treenode instanceof TmfFilterAndNode) {\r |
| 91 | \r |
| 92 | TmfFilterAndNode node = (TmfFilterAndNode) treenode;\r |
| 93 | element.setAttribute(TmfFilterAndNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 94 | \r |
| 95 | } else if (treenode instanceof TmfFilterOrNode) {\r |
| 96 | \r |
| 97 | TmfFilterOrNode node = (TmfFilterOrNode) treenode;\r |
| 98 | element.setAttribute(TmfFilterOrNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 99 | \r |
| 100 | } else if (treenode instanceof TmfFilterContainsNode) {\r |
| 101 | \r |
| 102 | TmfFilterContainsNode node = (TmfFilterContainsNode) treenode;\r |
| 103 | element.setAttribute(TmfFilterContainsNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 104 | element.setAttribute(TmfFilterContainsNode.FIELD_ATTR, node.getField());\r |
| 105 | element.setAttribute(TmfFilterContainsNode.VALUE_ATTR, node.getValue());\r |
| 106 | element.setAttribute(TmfFilterContainsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase()));\r |
| 107 | \r |
| 108 | } else if (treenode instanceof TmfFilterEqualsNode) {\r |
| 109 | \r |
| 110 | TmfFilterEqualsNode node = (TmfFilterEqualsNode) treenode;\r |
| 111 | element.setAttribute(TmfFilterEqualsNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 112 | element.setAttribute(TmfFilterEqualsNode.FIELD_ATTR, node.getField());\r |
| 113 | element.setAttribute(TmfFilterEqualsNode.VALUE_ATTR, node.getValue());\r |
| 114 | element.setAttribute(TmfFilterEqualsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase()));\r |
| 115 | \r |
| 116 | } else if (treenode instanceof TmfFilterMatchesNode) {\r |
| 117 | \r |
| 118 | TmfFilterMatchesNode node = (TmfFilterMatchesNode) treenode;\r |
| 119 | element.setAttribute(TmfFilterMatchesNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 120 | element.setAttribute(TmfFilterMatchesNode.FIELD_ATTR, node.getField());\r |
| 121 | element.setAttribute(TmfFilterMatchesNode.REGEX_ATTR, node.getRegex());\r |
| 122 | \r |
| 123 | } else if (treenode instanceof TmfFilterCompareNode) {\r |
| 124 | \r |
| 125 | TmfFilterCompareNode node = (TmfFilterCompareNode) treenode;\r |
| 126 | element.setAttribute(TmfFilterCompareNode.NOT_ATTR, Boolean.toString(node.isNot()));\r |
| 127 | element.setAttribute(TmfFilterCompareNode.FIELD_ATTR, node.getField());\r |
| 128 | element.setAttribute(TmfFilterCompareNode.RESULT_ATTR, Integer.toString(node.getResult()));\r |
| 129 | element.setAttribute(TmfFilterCompareNode.TYPE_ATTR, node.getType().toString());\r |
| 130 | element.setAttribute(TmfFilterCompareNode.VALUE_ATTR, node.getValue());\r |
| 131 | \r |
| 132 | }\r |
| 133 | \r |
| 134 | parentElement.appendChild(element);\r |
| 135 | \r |
| 136 | for (int i = 0; i < treenode.getChildrenCount(); i++) {\r |
| 137 | buildXMLTree(document, treenode.getChild(i), element);\r |
| 138 | }\r |
| 139 | }\r |
| 140 | \r |
| 141 | /**\r |
| 142 | * Save the tree \r |
| 143 | * \r |
| 144 | * @param uri The new Filter XML path\r |
| 145 | */ \r |
| 146 | public void saveTree(final String uri) {\r |
| 147 | TransformerFactory transformerFactory = TransformerFactory.newInstance();\r |
| 148 | \r |
| 149 | try {\r |
| 150 | Transformer transformer = transformerFactory.newTransformer();\r |
| 151 | DOMSource source = new DOMSource(document);\r |
| 152 | StreamResult result = new StreamResult(new File(uri));\r |
| 153 | transformer.transform(source, result);\r |
| 154 | } catch (TransformerConfigurationException e) {\r |
| 155 | e.printStackTrace();\r |
| 156 | } catch (TransformerException e) {\r |
| 157 | e.printStackTrace();\r |
| 158 | }\r |
| 159 | }\r |
| 160 | \r |
| 161 | }\r |