1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
18 import java
.text
.MessageFormat
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
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
;
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
;
43 * Reads a profiles file from an input stream
45 * @author Marc-Andre Laperle
47 public class RemoteImportProfilesReader
{
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];
54 * Validate the content of the profiles file from an input stream
57 * the input stream to validate from
60 * @throws SAXException
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
),
68 if (schemaFileUrl
== null) {
69 throw new IOException(
71 Messages
.TracePackageExtractManifestOperation_SchemaFileNotFound
,
72 PROFILES_SCHEMA_FILE_NAME
));
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
,
85 } catch (IOException e
) {
86 throw new IOException(
87 Messages
.TracePackageExtractManifestOperation_ErrorManifestNotValid
,
93 * Load profile model elements the profiles file (input stream).
95 * The file format looks like this:
97 * <?xml version="1.0" encoding="UTF-8" standalone="no"?>
99 * <version>0.1</version>
100 * <profile name="myProfile">
101 * <node name="myhost">
102 * <uri>ssh://user@127.0.0.1:22</uri>
103 * <traceGroup root="/home/user/lttng-traces/" recursive="true">
104 * <trace name="" type="org.eclipse.linuxtools.tmf.ui.type.ctf">
105 * <file name=".*" />
112 * See schema/remote-profile.xsd for details.
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
124 public static TracePackageElement
[] loadElementsFromProfiles(InputStream inputStream
)
125 throws IOException
, SAXException
, ParserConfigurationException
{
127 List
<TracePackageElement
> packageElements
= new ArrayList
<>();
128 RemoteImportProfileElement profile
= null;
129 Document doc
= DocumentBuilderFactory
.newInstance().newDocumentBuilder().parse(
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
);
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
);
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();
159 RemoteImportConnectionNodeElement connectionNode
= new RemoteImportConnectionNodeElement(profile
,
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(
173 TracePackageElement
[] e
= ManifestReader
.loadElementsFromNode(traceGroupElement
);
174 for (TracePackageElement a
: e
) {
175 traceGroup
.addChild(a
);
183 packageElements
.add(profile
);
186 return packageElements
.toArray(EMPTY_ARRAY
);