1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
.ui
.project
.wizards
.tracepkg
.importexport
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
18 import java
.text
.MessageFormat
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
24 import javax
.xml
.XMLConstants
;
25 import javax
.xml
.parsers
.DocumentBuilderFactory
;
26 import javax
.xml
.parsers
.ParserConfigurationException
;
27 import javax
.xml
.transform
.stream
.StreamSource
;
28 import javax
.xml
.validation
.Schema
;
29 import javax
.xml
.validation
.SchemaFactory
;
30 import javax
.xml
.validation
.Validator
;
32 import org
.eclipse
.core
.runtime
.FileLocator
;
33 import org
.eclipse
.core
.runtime
.Path
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.ITracePackageConstants
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageBookmarkElement
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
40 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFileElement
;
41 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFilesElement
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
43 import org
.w3c
.dom
.Document
;
44 import org
.w3c
.dom
.Element
;
45 import org
.w3c
.dom
.NamedNodeMap
;
46 import org
.w3c
.dom
.Node
;
47 import org
.w3c
.dom
.NodeList
;
48 import org
.xml
.sax
.SAXException
;
51 * Reads a manifest from an input stream
53 * @author Marc-Andre Laperle
55 public class ManifestReader
{
57 private static final String SCHEMA_FOLDER_NAME
= "schema"; //$NON-NLS-1$
58 private static final String EXPORT_MANIFEST_SCHEMA_FILE_NAME
= "export-manifest.xsd"; //$NON-NLS-1$
59 private static final @NonNull TracePackageElement
@NonNull [] EMPTY_ARRAY
=
60 new @NonNull TracePackageElement
[0];
63 * Validate the content of a manifest from an input stream
65 * @param input the input stream to validate from
66 * @throws IOException on error
68 public static void validateManifest(InputStream input
) throws IOException
70 URL schemaFileUrl
= FileLocator
.find(Activator
.getDefault().getBundle(), new Path(SCHEMA_FOLDER_NAME
).append(EXPORT_MANIFEST_SCHEMA_FILE_NAME
), null);
71 if (schemaFileUrl
== null) {
72 throw new IOException(MessageFormat
.format(Messages
.TracePackageExtractManifestOperation_SchemaFileNotFound
, EXPORT_MANIFEST_SCHEMA_FILE_NAME
));
76 SchemaFactory factory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
77 Schema schema
= factory
.newSchema(new StreamSource(schemaFileUrl
.openStream()));
78 Validator validator
= schema
.newValidator();
79 validator
.validate(new StreamSource(input
));
80 } catch (SAXException e
) {
81 throw new IOException(Messages
.TracePackageExtractManifestOperation_ErrorManifestNotValid
, e
);
82 } catch (IOException e
) {
83 throw new IOException(Messages
.TracePackageExtractManifestOperation_ErrorManifestNotValid
, e
);
88 * Load package elements from a manifest (input stream)
90 * The manifest looks like this:
92 * <?xml version="1.0" encoding="UTF-8" standalone="no"?>
94 * <trace name="trace2" type="org.eclipse.linuxtools.lttng2.kernel.tracetype">
95 * <file name="Traces/trace2"/> <supplementary-file name=".tracing/trace2/stateHistory.ht"/>
97 * <bookmark location="4" message= "15:50:47.314 069 885, channel0_0, sys_recvmsg, fd=16, msg=0x7faada7d1ae0, flags=256" />
102 * See schema/export-manifest.xsd for details.
105 * the input stream that contains the manifest
106 * @return the loaded elements
107 * @throws IOException
108 * when an error occurs when parsing
109 * @throws SAXException
110 * when an error occurs when parsing
111 * @throws ParserConfigurationException
112 * when an error occurs when parsing
114 public static TracePackageElement
[] loadElementsFromManifest(InputStream inputStream
) throws IOException
, SAXException
, ParserConfigurationException
{
115 Document doc
= DocumentBuilderFactory
.newInstance().newDocumentBuilder().parse(inputStream
);
116 Element rootElement
= doc
.getDocumentElement();
117 return loadElementsFromNode(rootElement
);
121 * Load package elements from a manifest (XML element)
124 * the root element to start loading from
125 * @return the loaded elements
127 public static TracePackageElement
[] loadElementsFromNode(Element rootElement
) {
128 List
<TracePackageElement
> packageElements
= new ArrayList
<>();
129 NodeList traceElements
= rootElement
.getElementsByTagName(ITracePackageConstants
.TRACE_ELEMENT
);
130 for (int i
= 0; i
< traceElements
.getLength(); i
++) {
131 Node traceNode
= traceElements
.item(i
);
132 if (traceNode
.getNodeType() == Node
.ELEMENT_NODE
) {
133 Element traceElement
= (Element
) traceNode
;
134 String traceName
= traceElement
.getAttribute(ITracePackageConstants
.TRACE_NAME_ATTRIB
);
135 String traceType
= traceElement
.getAttribute(ITracePackageConstants
.TRACE_TYPE_ATTRIB
);
136 TracePackageElement element
= new TracePackageTraceElement(null, traceName
, traceType
);
137 NodeList fileElements
= traceElement
.getElementsByTagName(ITracePackageConstants
.TRACE_FILE_ELEMENT
);
138 for (int j
= 0; j
< fileElements
.getLength(); j
++) {
139 Node fileNode
= fileElements
.item(j
);
140 if (fileNode
.getNodeType() == Node
.ELEMENT_NODE
) {
141 Element fileElement
= (Element
) fileNode
;
142 String fileName
= fileElement
.getAttribute(ITracePackageConstants
.TRACE_FILE_NAME_ATTRIB
);
143 new TracePackageFilesElement(element
, fileName
);
147 // Supplementary files
148 NodeList suppFilesElements
= traceElement
.getElementsByTagName(ITracePackageConstants
.SUPPLEMENTARY_FILE_ELEMENT
);
149 if (suppFilesElements
.getLength() > 0) {
150 TracePackageSupplFilesElement supplFilesElement
= new TracePackageSupplFilesElement(element
);
151 for (int j
= 0; j
< suppFilesElements
.getLength(); j
++) {
152 Node suppFileNode
= suppFilesElements
.item(j
);
153 if (suppFileNode
.getNodeType() == Node
.ELEMENT_NODE
) {
154 Element suppFileElement
= (Element
) suppFileNode
;
155 String fileName
= suppFileElement
.getAttribute(ITracePackageConstants
.SUPPLEMENTARY_FILE_NAME_ATTRIB
);
156 new TracePackageSupplFileElement(fileName
, supplFilesElement
);
162 List
<Map
<String
, String
>> bookmarkAttribs
= new ArrayList
<>();
163 NodeList bookmarksElements
= traceElement
.getElementsByTagName(ITracePackageConstants
.BOOKMARKS_ELEMENT
);
164 for (int j
= 0; j
< bookmarksElements
.getLength(); j
++) {
165 Node bookmarksNode
= bookmarksElements
.item(j
);
166 if (bookmarksNode
.getNodeType() == Node
.ELEMENT_NODE
) {
167 NodeList bookmarkElements
= traceElement
.getElementsByTagName(ITracePackageConstants
.BOOKMARK_ELEMENT
);
168 for (int k
= 0; k
< bookmarkElements
.getLength(); k
++) {
169 Node bookmarkNode
= bookmarkElements
.item(k
);
170 if (bookmarkNode
.getNodeType() == Node
.ELEMENT_NODE
) {
171 Element bookmarkElement
= (Element
) bookmarkNode
;
172 NamedNodeMap attributesMap
= bookmarkElement
.getAttributes();
173 Map
<String
, String
> attribs
= new HashMap
<>();
174 for (int l
= 0; l
< attributesMap
.getLength(); l
++) {
175 Node item
= attributesMap
.item(l
);
176 attribs
.put(item
.getNodeName(), item
.getNodeValue());
178 bookmarkAttribs
.add(attribs
);
183 if (!bookmarkAttribs
.isEmpty()) {
184 new TracePackageBookmarkElement(element
, bookmarkAttribs
);
187 packageElements
.add(element
);
190 return packageElements
.toArray(EMPTY_ARRAY
);