ed962f12d268400600b6734eb89171825bdaddfc
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.remote.ui / src / org / eclipse / tracecompass / internal / tmf / remote / ui / wizards / fetch / model / RemoteImportProfilesReader.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URL;
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.xml.XMLConstants;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.transform.stream.StreamSource;
26 import javax.xml.validation.Schema;
27 import javax.xml.validation.SchemaFactory;
28 import javax.xml.validation.Validator;
29
30 import org.eclipse.core.runtime.FileLocator;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.tracecompass.internal.tmf.remote.ui.Activator;
33 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
34 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.importexport.ManifestReader;
35 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.importexport.Messages;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
41
42 /**
43 * Reads a profiles file from an input stream
44 *
45 * @author Marc-Andre Laperle
46 */
47 public class RemoteImportProfilesReader {
48
49 private static final String SCHEMA_FOLDER_NAME = "schema"; //$NON-NLS-1$
50 private static final String PROFILES_SCHEMA_FILE_NAME = "remote-profile.xsd"; //$NON-NLS-1$
51 private static final TracePackageElement[] EMPTY_ARRAY = new TracePackageElement[0];
52
53 /**
54 * Validate the content of the profiles file from an input stream
55 *
56 * @param input
57 * the input stream to validate from
58 * @throws IOException
59 * on error
60 * @throws SAXException
61 * on error
62 */
63 public static void validate(InputStream input) throws IOException, SAXException {
64 URL schemaFileUrl = FileLocator.find(
65 Activator.getDefault().getBundle(),
66 new Path(SCHEMA_FOLDER_NAME).append(PROFILES_SCHEMA_FILE_NAME),
67 null);
68 if (schemaFileUrl == null) {
69 throw new IOException(
70 MessageFormat.format(
71 Messages.TracePackageExtractManifestOperation_SchemaFileNotFound,
72 PROFILES_SCHEMA_FILE_NAME));
73 }
74
75 try {
76 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
77 Schema schema = factory.newSchema(new StreamSource(
78 schemaFileUrl.openStream()));
79 Validator validator = schema.newValidator();
80 validator.validate(new StreamSource(input));
81 } catch (SAXException e) {
82 throw new SAXException(
83 Messages.TracePackageExtractManifestOperation_ErrorManifestNotValid,
84 e);
85 } catch (IOException e) {
86 throw new IOException(
87 Messages.TracePackageExtractManifestOperation_ErrorManifestNotValid,
88 e);
89 }
90 }
91
92 /**
93 * Load profile model elements the profiles file (input stream).
94 *
95 * The file format looks like this:
96 * <pre>
97 * &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?>
98 * &lt;profiles>
99 * &lt;version>0.1&lt;/version>
100 * &lt;profile name="myProfile">
101 * &lt;node name="myhost">
102 * &lt;uri>ssh://user@127.0.0.1:22&lt;/uri>
103 * &lt;traceGroup root="/home/user/lttng-traces/" recursive="true">
104 * &lt;trace name="" type="org.eclipse.linuxtools.tmf.ui.type.ctf">
105 * &lt;file name=".*" />
106 * &lt;/trace>
107 * &lt;/traceGroup>
108 * &lt;/node>
109 * &lt;/profile>
110 * &lt;/profiles>
111 * </pre>
112 * See schema/remote-profile.xsd for details.
113 *
114 * @param inputStream
115 * the input stream that contains the profiles
116 * @return the loaded elements
117 * @throws IOException
118 * when an error occurs when parsing
119 * @throws SAXException
120 * when an error occurs when parsing
121 * @throws ParserConfigurationException
122 * when an error occurs when parsing
123 */
124 public static TracePackageElement[] loadElementsFromProfiles(InputStream inputStream)
125 throws IOException, SAXException, ParserConfigurationException {
126
127 List<TracePackageElement> packageElements = new ArrayList<>();
128 RemoteImportProfileElement profile = null;
129 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
130 inputStream);
131
132 NodeList profileNodes = doc.getDocumentElement().getElementsByTagName(
133 RemoteImportProfileConstants.PROFILE_ELEMENT);
134 for (int i = 0; i < profileNodes.getLength(); i++) {
135 Node profileNode = profileNodes.item(i);
136 if (profileNode.getNodeType() == Node.ELEMENT_NODE) {
137 Element profileElement = (Element) profileNode;
138 String traceName = profileElement.getAttribute(RemoteImportProfileConstants.PROFILE_NAME_ATTRIB);
139 profile = new RemoteImportProfileElement(null, traceName);
140
141 NodeList nodeNodes = profileElement.getElementsByTagName(RemoteImportProfileConstants.NODE_ELEMENT);
142 for (int j = 0; j < nodeNodes.getLength(); j++) {
143 Node nodeNode = nodeNodes.item(j);
144 if (nodeNode.getNodeType() == Node.ELEMENT_NODE) {
145 Element nodeElement = (Element) nodeNode;
146 String nameAttr = nodeElement.getAttribute(RemoteImportProfileConstants.NODE_NAME_ATTRIB);
147
148 NodeList uriNodes = nodeElement.getElementsByTagName(RemoteImportProfileConstants.NODE_URI_ELEMENT);
149 String uri = ""; //$NON-NLS-1$
150 for (int k = 0; k < uriNodes.getLength(); k++) {
151 Node uriNode = uriNodes.item(k);
152 if (uriNode.getNodeType() == Node.ELEMENT_NODE) {
153 Element uriElement = (Element) uriNode;
154 uri = uriElement.getFirstChild().getNodeValue();
155 break;
156 }
157 }
158
159 RemoteImportConnectionNodeElement connectionNode = new RemoteImportConnectionNodeElement(profile,
160 nameAttr, uri);
161
162 NodeList traceGroupNodes = nodeElement.getElementsByTagName(RemoteImportProfileConstants.TRACE_GROUP_ELEMENT);
163 for (int k = 0; k < traceGroupNodes.getLength(); k++) {
164 Node traceGroupNode = traceGroupNodes.item(k);
165 if (traceGroupNode.getNodeType() == Node.ELEMENT_NODE) {
166 Element traceGroupElement = (Element) traceGroupNode;
167 String rootAttr = traceGroupElement.getAttribute(RemoteImportProfileConstants.TRACE_GROUP_ROOT_ATTRIB);
168 String recursiveAttr = traceGroupElement.getAttribute(RemoteImportProfileConstants.TRACE_GROUP_RECURSIVE_ATTRIB);
169 RemoteImportTraceGroupElement traceGroup = new RemoteImportTraceGroupElement(
170 connectionNode, rootAttr);
171 traceGroup.setRecursive(Boolean.TRUE.toString().equals(
172 recursiveAttr));
173 TracePackageElement[] e = ManifestReader.loadElementsFromNode(traceGroupElement);
174 for (TracePackageElement a : e) {
175 traceGroup.addChild(a);
176 }
177 }
178 }
179
180 }
181 }
182
183 packageElements.add(profile);
184 }
185 }
186 return packageElements.toArray(EMPTY_ARRAY);
187 }
188 }
This page took 0.035465 seconds and 4 git commands to generate.