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