|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2006, 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* $Id: FilterTransformationHelper.java,v 1.11 2007/03/21 21:30:59 |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* IBM - Initial API and implementation |
| 11 |
**********************************************************************/ |
| 12 |
package org.eclipse.tptp.monitoring.logui.internal.util; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.io.StringReader; |
| 16 |
import java.io.StringWriter; |
| 17 |
|
| 18 |
import javax.xml.parsers.DocumentBuilder; |
| 19 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 20 |
|
| 21 |
import org.apache.xml.serialize.OutputFormat; |
| 22 |
import org.apache.xml.serialize.XMLSerializer; |
| 23 |
import org.eclipse.tptp.monitoring.logui.internal.MonitoringLogUIPlugin; |
| 24 |
|
| 25 |
import org.w3c.dom.Document; |
| 26 |
import org.w3c.dom.Element; |
| 27 |
import org.w3c.dom.NamedNodeMap; |
| 28 |
import org.w3c.dom.Node; |
| 29 |
import org.w3c.dom.NodeList; |
| 30 |
import org.xml.sax.InputSource; |
| 31 |
|
| 32 |
public class FilterTransformationHelper |
| 33 |
{ |
| 34 |
public static String convertXmiToXml(String xmiString) |
| 35 |
{ |
| 36 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 37 |
try |
| 38 |
{ |
| 39 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 40 |
|
| 41 |
Document document = builder.parse(new InputSource(new StringReader(xmiString))); |
| 42 |
|
| 43 |
Document newDocument = builder.newDocument(); |
| 44 |
|
| 45 |
Element element = (Element) document.getElementsByTagName("logsets").item(0); |
| 46 |
if(element != null) |
| 47 |
{ |
| 48 |
buildXMLDocument(element, null, newDocument); |
| 49 |
} |
| 50 |
|
| 51 |
Node root = newDocument.getElementsByTagName("logsets").item(0); |
| 52 |
|
| 53 |
return toString(root); |
| 54 |
} |
| 55 |
catch (Exception e) |
| 56 |
{ |
| 57 |
MonitoringLogUIPlugin.log(e); |
| 58 |
return null; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private static void buildXMLDocument(Element element, Element parent, Document newDocument) |
| 63 |
{ |
| 64 |
String nodeName = element.getNodeName(); |
| 65 |
if(!nodeName.equals("xmi:XMI")) |
| 66 |
{ |
| 67 |
if(nodeName.equals("HierarchyExtensions:SimpleSearchQuery")) |
| 68 |
{ |
| 69 |
nodeName = "LogAnalyzerFilter"; |
| 70 |
} |
| 71 |
|
| 72 |
Element newElement = newDocument.createElement(nodeName); |
| 73 |
|
| 74 |
newElement.setTextContent(element.getTextContent()); |
| 75 |
|
| 76 |
NamedNodeMap attributes = element.getAttributes(); |
| 77 |
for(int i = 0; i < attributes.getLength(); i++) |
| 78 |
{ |
| 79 |
Node attribute = attributes.item(i); |
| 80 |
String name = attribute.getNodeName(); |
| 81 |
if(name.equals("xsi:type")) |
| 82 |
{ |
| 83 |
name = "category"; |
| 84 |
} |
| 85 |
else if(nodeName.equals("valueType") && name.equals("href")) |
| 86 |
{ |
| 87 |
name = "type"; |
| 88 |
} |
| 89 |
|
| 90 |
String value = attribute.getNodeValue(); |
| 91 |
if(value.startsWith("HierarchyExtensions:")) |
| 92 |
{ |
| 93 |
value = value.replace("HierarchyExtensions:", "Hierarchial"); |
| 94 |
} |
| 95 |
else if(value.startsWith("ecore:E")) |
| 96 |
{ |
| 97 |
value = value.replace("ecore:E", "Event:"); |
| 98 |
} |
| 99 |
else if(value.contains("http://www.eclipse.org")) |
| 100 |
{ |
| 101 |
value = value.replace("http://www.eclipse.org/emf/2002/Ecore#//E", ""); |
| 102 |
} |
| 103 |
else if(value.startsWith("org.eclipse.hyades.log.ui.ImportLogFilterType:")) |
| 104 |
{ |
| 105 |
value = value.replace("org.eclipse.hyades.log.ui.ImportLogFilterType:", "ImportLogFilterType:"); |
| 106 |
} |
| 107 |
|
| 108 |
newElement.setAttribute(name, value); |
| 109 |
} |
| 110 |
|
| 111 |
if(parent == null) |
| 112 |
{ |
| 113 |
newDocument.appendChild(newElement); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
parent.appendChild(newElement); |
| 118 |
} |
| 119 |
|
| 120 |
parent = newElement; |
| 121 |
} |
| 122 |
|
| 123 |
NodeList children = element.getChildNodes(); |
| 124 |
for(int i = 0; i < children.getLength(); i++) |
| 125 |
{ |
| 126 |
Object object = children.item(i); |
| 127 |
if(object instanceof Element) |
| 128 |
{ |
| 129 |
Element child = (Element) object; |
| 130 |
if(child != null) |
| 131 |
{ |
| 132 |
buildXMLDocument(child, parent, newDocument); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public static String convertXmlToXmi(String xmlString) |
| 139 |
{ |
| 140 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 141 |
try |
| 142 |
{ |
| 143 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 144 |
|
| 145 |
Document document = builder.parse(new InputSource(new StringReader(xmlString))); |
| 146 |
|
| 147 |
Document newDocument = builder.newDocument(); |
| 148 |
|
| 149 |
Element element = (Element) document.getElementsByTagName("logsets").item(0); |
| 150 |
if(element != null) |
| 151 |
{ |
| 152 |
buildXmiDocument(element, null, newDocument); |
| 153 |
} |
| 154 |
|
| 155 |
Node root = newDocument.getElementsByTagName("logsets").item(0); |
| 156 |
|
| 157 |
return toString(root); |
| 158 |
} |
| 159 |
catch (Exception e) |
| 160 |
{ |
| 161 |
MonitoringLogUIPlugin.log(e); |
| 162 |
return null; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private static void buildXmiDocument(Element element, Element parent, Document newDocument) |
| 167 |
{ |
| 168 |
String nodeName = element.getNodeName(); |
| 169 |
|
| 170 |
if(nodeName.equals("LogAnalyzerFilter")) |
| 171 |
{ |
| 172 |
nodeName = "HierarchyExtensions:SimpleSearchQuery"; |
| 173 |
} |
| 174 |
|
| 175 |
Element newElement = newDocument.createElement(nodeName); |
| 176 |
|
| 177 |
newElement.setTextContent(element.getTextContent()); |
| 178 |
|
| 179 |
NamedNodeMap attributes = element.getAttributes(); |
| 180 |
for(int i = 0; i < attributes.getLength(); i++) |
| 181 |
{ |
| 182 |
Node attribute = attributes.item(i); |
| 183 |
String name = attribute.getNodeName(); |
| 184 |
if(name.equals("category")) |
| 185 |
{ |
| 186 |
name = "xsi:type"; |
| 187 |
} |
| 188 |
else if(nodeName.equals("valueType") && name.equals("type")) |
| 189 |
{ |
| 190 |
name = "href"; |
| 191 |
} |
| 192 |
|
| 193 |
String value = attribute.getNodeValue(); |
| 194 |
if(value.startsWith("Hierarchial")) |
| 195 |
{ |
| 196 |
value = value.replace("Hierarchial", "HierarchyExtensions:"); |
| 197 |
} |
| 198 |
else if(value.startsWith("Event:")) |
| 199 |
{ |
| 200 |
value = value.replace("Event:", "ecore:E"); |
| 201 |
} |
| 202 |
else if(nodeName.equals("valueType") && name.equals("href")) |
| 203 |
{ |
| 204 |
value = "http://www.eclipse.org/emf/2002/Ecore#//E" + value; |
| 205 |
} |
| 206 |
else if(value.startsWith("ImportLogFilterType:")) |
| 207 |
{ |
| 208 |
value = value.replace("ImportLogFilterType:", "org.eclipse.hyades.log.ui.ImportLogFilterType:"); |
| 209 |
} |
| 210 |
|
| 211 |
newElement.setAttribute(name, value); |
| 212 |
} |
| 213 |
|
| 214 |
if(parent == null) |
| 215 |
{ |
| 216 |
newDocument.appendChild(newElement); |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
parent.appendChild(newElement); |
| 221 |
} |
| 222 |
parent = newElement; |
| 223 |
|
| 224 |
if(nodeName.equals("filters")) |
| 225 |
{ |
| 226 |
Element xmiElement = newDocument.createElement("xmi:XMI"); |
| 227 |
|
| 228 |
xmiElement.setAttribute("xmi:version", "2.0"); |
| 229 |
xmiElement.setAttribute("xmlns:xmi", "http://www.omg.org/XMI"); |
| 230 |
xmiElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); |
| 231 |
xmiElement.setAttribute("xmlns:HierarchyExtensions", "http://www.eclipse.org/hyades/models/hierarchy-extensions.xmi"); |
| 232 |
xmiElement.setAttribute("xmlns:ecore", "http://www.eclipse.org/emf/2002/Ecore"); |
| 233 |
|
| 234 |
parent.appendChild(xmiElement); |
| 235 |
|
| 236 |
parent = xmiElement; |
| 237 |
} |
| 238 |
|
| 239 |
NodeList children = element.getChildNodes(); |
| 240 |
for(int i = 0; i < children.getLength(); i++) |
| 241 |
{ |
| 242 |
Object object = children.item(i); |
| 243 |
if(object instanceof Element) |
| 244 |
{ |
| 245 |
Element child = (Element) object; |
| 246 |
if(child != null) |
| 247 |
{ |
| 248 |
buildXmiDocument(child, parent, newDocument); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public static String toString(Node xml) |
| 255 |
{ |
| 256 |
return toString(xml, true); |
| 257 |
} |
| 258 |
|
| 259 |
public static String toString(Node xml, boolean printHeader) |
| 260 |
{ |
| 261 |
return toString(xml, printHeader, true); |
| 262 |
} |
| 263 |
|
| 264 |
public static String toString(Node xml, boolean printHeader, boolean printIndents) |
| 265 |
{ |
| 266 |
short type = xml.getNodeType(); |
| 267 |
if (type == 3) |
| 268 |
{ |
| 269 |
return xml.getNodeValue(); |
| 270 |
} |
| 271 |
|
| 272 |
XMLSerializer serializer = new XMLSerializer(); |
| 273 |
serializer.setNamespaces(true); |
| 274 |
OutputFormat formatter = new OutputFormat(); |
| 275 |
formatter.setOmitXMLDeclaration(!printHeader); |
| 276 |
formatter.setIndenting(printIndents); |
| 277 |
serializer.setOutputFormat(formatter); |
| 278 |
StringWriter writer = new StringWriter(); |
| 279 |
serializer.setOutputCharStream(writer); |
| 280 |
|
| 281 |
try |
| 282 |
{ |
| 283 |
if (type == 9) |
| 284 |
{ |
| 285 |
serializer.serialize((Document) xml); |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
serializer.serialize((Element) xml); |
| 290 |
} |
| 291 |
} |
| 292 |
catch (IOException error) |
| 293 |
{ |
| 294 |
throw new RuntimeException(error.getMessage(), error); |
| 295 |
} |
| 296 |
|
| 297 |
return writer.toString(); |
| 298 |
} |
| 299 |
} |