1810
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.internal.discovery.ui.wizard;
|
|
18 |
|
|
19 |
import java.io.IOException;
|
|
20 |
import java.io.InputStream;
|
|
21 |
import java.io.OutputStream;
|
|
22 |
import java.net.URI;
|
1818
|
23 |
import java.net.URISyntaxException;
|
1810
|
24 |
import java.text.MessageFormat;
|
1818
|
25 |
|
|
26 |
import javax.xml.parsers.ParserConfigurationException;
|
|
27 |
import javax.xml.parsers.SAXParser;
|
|
28 |
import javax.xml.parsers.SAXParserFactory;
|
1810
|
29 |
|
1818
|
30 |
import org.eclipse.equinox.p2.metadata.Version;
|
|
31 |
import org.xml.sax.Attributes;
|
|
32 |
import org.xml.sax.SAXException;
|
|
33 |
import org.xml.sax.helpers.DefaultHandler;
|
|
34 |
|
|
35 |
import com.nokia.carbide.discovery.ui.Activator;
|
1824
|
36 |
import com.nokia.carbide.discovery.ui.Messages;
|
1810
|
37 |
|
|
38 |
/**
|
|
39 |
* Serializes feature infos and repository URIs into output stream as XML
|
|
40 |
* Example serialized single test connection:
|
|
41 |
*
|
|
42 |
* <blockquote><pre>
|
|
43 |
*<featuresConfiguration version="1">
|
|
44 |
* <autoImportOriginalVersions value="false"/>
|
|
45 |
* <repository uri="http://cdn.symbian.org/carbide/updates/3.0/discovery"/>
|
|
46 |
* <feature id="com.nokia.example.feature.group" version="1.0.0"/>
|
|
47 |
*</featuresConfiguration>
|
|
48 |
* </pre></blockquote>
|
|
49 |
*/
|
|
50 |
class Streamer {
|
|
51 |
|
|
52 |
private static final String CURRENT_VERSION = "1"; //$NON-NLS-1$
|
|
53 |
|
1818
|
54 |
private static final String ROOT_ELEMENT = "featuresConfiguration"; //$NON-NLS-1$
|
|
55 |
private static final String WANTS_VERSIONS_ELEMENT = "wantsOriginalVersions"; //$NON-NLS-1$
|
|
56 |
private static final String REPOSITORY_ELEMENT = "repository"; //$NON-NLS-1$
|
|
57 |
private static final String FEATURE_ELEMENT = "feature"; //$NON-NLS-1$
|
|
58 |
|
|
59 |
private static final String VERSION_ATTR = "version"; //$NON-NLS-1$
|
|
60 |
private static final String VALUE_ATTR = "value"; //$NON-NLS-1$
|
|
61 |
private static final String URI_ATTR = "uri"; //$NON-NLS-1$
|
|
62 |
private static final String ID_ATTR = "id"; //$NON-NLS-1$
|
|
63 |
|
1810
|
64 |
private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"; //$NON-NLS-1$
|
1818
|
65 |
|
1824
|
66 |
private static final String ROOT_START = "<" + ROOT_ELEMENT + " " + VERSION_ATTR + "=\"" + CURRENT_VERSION + "\">\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
|
67 |
private static final String ROOT_END = "</" + ROOT_ELEMENT + ">\n"; //$NON-NLS-1$ //$NON-NLS-2$
|
1818
|
68 |
|
1824
|
69 |
private static final String ORIGINAL_VERSION_FMT = "\t<" + WANTS_VERSIONS_ELEMENT + " " + VALUE_ATTR + "=\"{0}\"/>\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
70 |
private static final String REPOSITORY_FMT = "\t<" + REPOSITORY_ELEMENT + " " + URI_ATTR + "=\"{0}\"/>\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
71 |
private static final String FEATURE_FMT = "\t<" + FEATURE_ELEMENT + " " + ID_ATTR + "=\"{0}\" " + VERSION_ATTR + "=\"{1}\"/>\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
1810
|
72 |
|
1818
|
73 |
public static void writeToXML(OutputStream os, ImportExportData data) throws IOException {
|
1810
|
74 |
os.write(XML_HEADER.getBytes());
|
1818
|
75 |
os.write(ROOT_START.getBytes());
|
1810
|
76 |
|
|
77 |
// write auto-import original versions
|
1818
|
78 |
String originalVersionElement = MessageFormat.format(ORIGINAL_VERSION_FMT, data.getWantsVersions());
|
1810
|
79 |
os.write(originalVersionElement.getBytes());
|
|
80 |
|
|
81 |
// write the repositories
|
1818
|
82 |
for (URI uri : data.getURIs()) {
|
1810
|
83 |
String repositoryElement = MessageFormat.format(REPOSITORY_FMT, uri);
|
|
84 |
os.write(repositoryElement.getBytes());
|
|
85 |
}
|
|
86 |
|
|
87 |
// write the featureInfos
|
1818
|
88 |
for (FeatureInfo info : data.getFeatureInfos()) {
|
1810
|
89 |
String featureElement = MessageFormat.format(FEATURE_FMT, info.getId(), info.getVersion());
|
|
90 |
os.write(featureElement.getBytes());
|
|
91 |
}
|
|
92 |
|
1818
|
93 |
os.write(ROOT_END.getBytes());
|
1810
|
94 |
os.close();
|
|
95 |
}
|
|
96 |
|
1818
|
97 |
private static class ReadHandler extends DefaultHandler {
|
|
98 |
|
|
99 |
private final ImportExportData data;
|
|
100 |
|
|
101 |
public ReadHandler(ImportExportData data) {
|
|
102 |
this.data = data;
|
|
103 |
}
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
107 |
if (ROOT_ELEMENT.equals(qName)) {
|
|
108 |
String versionStr = attributes.getValue(VERSION_ATTR);
|
|
109 |
if (!CURRENT_VERSION.equals(versionStr))
|
|
110 |
throw new IllegalArgumentException(
|
1824
|
111 |
MessageFormat.format(Messages.Streamer_BadVersionError, CURRENT_VERSION, ROOT_ELEMENT));
|
1818
|
112 |
}
|
|
113 |
else if (WANTS_VERSIONS_ELEMENT.equals(qName)) {
|
|
114 |
String wantsVersions = attributes.getValue(VALUE_ATTR);
|
|
115 |
data.setWantsVersions(Boolean.parseBoolean(wantsVersions));
|
|
116 |
}
|
|
117 |
else if (REPOSITORY_ELEMENT.equals(qName)) {
|
|
118 |
String uriStr = attributes.getValue(URI_ATTR);
|
|
119 |
try {
|
|
120 |
data.addURI(new URI(uriStr));
|
|
121 |
} catch (URISyntaxException e) {
|
1824
|
122 |
Activator.logError(MessageFormat.format(Messages.Streamer_BadURIError, uriStr), e);
|
1818
|
123 |
}
|
|
124 |
}
|
|
125 |
else if (FEATURE_ELEMENT.equals(qName)) {
|
|
126 |
String id = attributes.getValue(ID_ATTR);
|
|
127 |
String versionStr = attributes.getValue(VERSION_ATTR);
|
|
128 |
try {
|
|
129 |
Version version = Version.create(versionStr);
|
|
130 |
data.addFeatureInfo(new FeatureInfo(id, version));
|
|
131 |
} catch (IllegalArgumentException e) {
|
1824
|
132 |
Activator.logError(MessageFormat.format(Messages.Streamer_VersionParseError, versionStr), e);
|
1818
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
1810
|
136 |
}
|
1818
|
137 |
|
|
138 |
public static ImportExportData readFromXML(InputStream is) throws SAXException, IOException, ParserConfigurationException {
|
|
139 |
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
|
140 |
ImportExportData data = new ImportExportData();
|
|
141 |
parser.parse(is, new ReadHandler(data));
|
|
142 |
return data;
|
|
143 |
}
|
|
144 |
|
1810
|
145 |
}
|