1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*******************************************************************************
* Copyright (c) 2007 Innoopract Informationssysteme GmbH
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innoopract - initial API and implementation
*******************************************************************************/
package org.eclipse.epp.packaging.core.configuration.xml;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.epp.packaging.core.configuration.IPackagerConfiguration;
import org.eclipse.epp.packaging.core.configuration.PackagerConfiguration;
import org.eclipse.epp.packaging.core.configuration.Platform;
import org.xml.sax.SAXException;
/**
* Parses an Eclipse Packager configuration in XML format.
*/
public class ConfigurationParser {
private static final String LATEST = "latest"; //$NON-NLS-1$
private static final String ATTRIB_ARCH = "arch"; //$NON-NLS-1$
private static final String ATTRIB_ECLIPSE_INI_PATH = "path"; //$NON-NLS-1$
private static final String ATTRIB_ECLIPSE_PRODUCT_ID = "eclipseProductId"; //$NON-NLS-1$
private static final String ATTRIB_FOLDER = "folder"; //$NON-NLS-1$
private static final String ATTRIB_FORMAT = "format"; //$NON-NLS-1$
private static final String ATTRIB_ID = "id"; //$NON-NLS-1$
private static final String ATTRIB_INITIAL_PERSPECTIVE_ID = "initialPerspectiveId"; //$NON-NLS-1$
private static final String ATTRIB_NAME = "name"; //$NON-NLS-1$
private static final String ATTRIB_OS = "os"; //$NON-NLS-1$
private static final String ATTRIB_RELATIVE_FOLDER = "relativeFolder"; //$NON-NLS-1$
private static final String ATTRIB_URL = "url"; //$NON-NLS-1$
private static final String ATTRIB_VERSION = "version"; //$NON-NLS-1$
private static final String ATTRIB_WS = "ws"; //$NON-NLS-1$
private static final String TAG_ARCHIVE_FORMAT = "archiveFormat"; //$NON-NLS-1$
private static final String TAG_ECLIPSE_INI_FILE = "eclipseIniFileContent"; //$NON-NLS-1$
private static final String TAG_EXTENSION_SITE = "extensionSite"; //$NON-NLS-1$
private static final String TAG_FEATURE = "feature"; //$NON-NLS-1$
private static final String TAG_PLATFORM = "platform"; //$NON-NLS-1$
private static final String TAG_PRODUCT = "product"; //$NON-NLS-1$
private static final String TAG_RCP = "rcp"; //$NON-NLS-1$
private static final String TAG_REQUIRED_FEATURES = "requiredFeatures"; //$NON-NLS-1$
private static final String TAG_ROOT_FILE_FOLDER = "rootFileFolder"; //$NON-NLS-1$
private static final String TAG_TARGET_PLATFORMS = "targetPlatforms"; //$NON-NLS-1$
private static final String TAG_UPDATE_SITE = "updateSite"; //$NON-NLS-1$
private static final String TAG_UPDATE_SITES = "updateSites"; //$NON-NLS-1$
private final File xmlFile;
public ConfigurationParser( final File xmlFile ) {
this.xmlFile = xmlFile;
}
/**
* Parses the configuration contained in xmlFile.
*/
public IPackagerConfiguration parseConfiguration()
throws SAXException, IOException, ParserConfigurationException
{
return parseConfiguration( new XMLDocument( xmlFile ).getRootElement() );
}
/**
* Parses the configuration-string xml. Used in tests only.
*/
public IPackagerConfiguration parseConfiguration( final String xml )
throws MalformedURLException, SAXException, IOException,
ParserConfigurationException
{
return parseConfiguration( new XMLDocument( xml ).getRootElement() );
}
private IXmlElement[] getElements( final IXmlElement element,
final String name )
{
return element.getElements( name );
}
private String getFolderName( final IXmlElement element ) {
return element.getAttributeValue( ATTRIB_FOLDER );
}
/**
* Reads the various elements of the configuration from the root element.
*
* @throws MalformedURLException
*/
private IPackagerConfiguration parseConfiguration( final IXmlElement root )
throws MalformedURLException
{
PackagerConfiguration configuration = new PackagerConfiguration();
parseRcp( configuration, root );
parseProduct( configuration, root );
parseUpdateSites( configuration, root );
parseRequiredFeatures( configuration, root );
parseRootFolder( configuration, root );
parseExtensionSite( configuration, root );
parsePlatforms( configuration, root );
return configuration;
}
/** Loads and sets the extension site to use. */
private void parseExtensionSite( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement element = parent.getElement( TAG_EXTENSION_SITE );
configuration.setExtensionSiteRelative( element.getAttributeValue( ATTRIB_RELATIVE_FOLDER ) );
}
/** Loads and sets the target platforms. */
private void parsePlatforms( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement element = parent.getElement( TAG_TARGET_PLATFORMS );
for( IXmlElement platformElement : getElements( element, TAG_PLATFORM ) ) {
String os = platformElement.getAttributeValue( ATTRIB_OS );
String ws = platformElement.getAttributeValue( ATTRIB_WS );
String arch = platformElement.getAttributeValue( ATTRIB_ARCH );
IXmlElement eclipseIniFileElement = platformElement.getElement( TAG_ECLIPSE_INI_FILE );
String eclipseIniFileContent = eclipseIniFileElement.getText();
String eclipseIniFilePath = eclipseIniFileElement.getAttributeValue( ATTRIB_ECLIPSE_INI_PATH );
Platform platform = configuration.addTargetPlatform( os, ws, arch, eclipseIniFileContent, eclipseIniFilePath );
IXmlElement archiveFormat = platformElement.getElement( TAG_ARCHIVE_FORMAT );
if( archiveFormat != null ) {
platform.setArchiveFormat( archiveFormat.getAttributeValue( ATTRIB_FORMAT ) );
}
}
}
private void parseProduct( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement productElement = parent.getElement( TAG_PRODUCT );
String productName = productElement.getAttributeValue( ATTRIB_NAME );
configuration.setProductName( productName );
String eclipseProductId = productElement.getAttributeValue( ATTRIB_ECLIPSE_PRODUCT_ID );
configuration.setEclipseProductId( eclipseProductId );
String initialPerspectiveId = productElement.getAttributeValue( ATTRIB_INITIAL_PERSPECTIVE_ID );
configuration.setInitialPerspectiveId( initialPerspectiveId );
}
private void parseRcp( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement rcpElement = parent.getElement( TAG_RCP );
String rcpVersion = rcpElement.getAttributeValue( ATTRIB_VERSION );
configuration.setRcpVersion( rcpVersion );
}
/**
* Loads and sets the required features.
*/
private void parseRequiredFeatures( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement element = parent.getElement( TAG_REQUIRED_FEATURES );
for( IXmlElement featureElement : getElements( element, TAG_FEATURE ) ) {
String version = featureElement.getAttributeValue( ATTRIB_VERSION );
if( version != null ) {
version = version.trim();
if( version.length() == 0 || LATEST.equalsIgnoreCase( version ) ) {
version = null;
}
}
configuration.addRequiredFeature( featureElement.getAttributeValue( ATTRIB_ID ),
version );
}
}
/** Loads and sets the folder containing the root files. */
private void parseRootFolder( final PackagerConfiguration configuration,
final IXmlElement parent )
{
IXmlElement element = parent.getElement( TAG_ROOT_FILE_FOLDER );
String folder = resolveRelativeFileName( getFolderName( element ) );
configuration.setRootFileFolder( folder );
}
/**
* Loads and sets the available update sites.
*
* @throws MalformedURLException
*/
private void parseUpdateSites( final PackagerConfiguration configuration,
final IXmlElement parent )
throws MalformedURLException
{
IXmlElement element = parent.getElement( TAG_UPDATE_SITES );
for( IXmlElement siteElement : getElements( element, TAG_UPDATE_SITE ) ) {
configuration.addUpdateSite( siteElement.getAttributeValue( ATTRIB_URL ) );
}
}
/*
* This method resolves a potentially relative file name. If the path is
* specified relative, then it is considered relative to the parent of the
* configuration (XML) file.
*/
private String resolveRelativeFileName( final String path ) {
if( new File( path ).isAbsolute() )
return path;
return new File( this.xmlFile.getParent(), path ).toString();
}
}
|