Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005, 2011 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.equinox.metatype.impl; |
12 |
|
13 |
import java.io.*; |
14 |
import java.math.BigDecimal; |
15 |
import java.math.BigInteger; |
16 |
import java.net.URL; |
17 |
import java.util.*; |
18 |
import javax.xml.parsers.SAXParser; |
19 |
import org.eclipse.osgi.util.NLS; |
20 |
import org.osgi.framework.Bundle; |
21 |
import org.osgi.service.log.LogService; |
22 |
import org.osgi.service.metatype.AttributeDefinition; |
23 |
import org.xml.sax.*; |
24 |
import org.xml.sax.helpers.DefaultHandler; |
25 |
|
26 |
/** |
27 |
* Meta XML Data Parser |
28 |
*/ |
29 |
public class DataParser { |
30 |
private static final String METADATA = "MetaData"; //$NON-NLS-1$ |
31 |
private static final String LOCALIZATION = "localization"; //$NON-NLS-1$ |
32 |
private static final String OCD = "OCD"; //$NON-NLS-1$ |
33 |
private static final String ICON = "Icon"; //$NON-NLS-1$ |
34 |
private static final String AD = "AD"; //$NON-NLS-1$ |
35 |
private static final String CARDINALITY = "cardinality"; //$NON-NLS-1$ |
36 |
private static final String OPTION = "Option"; //$NON-NLS-1$ |
37 |
private static final String LABEL = "label"; //$NON-NLS-1$ |
38 |
private static final String VALUE = "value"; //$NON-NLS-1$ |
39 |
private static final String MIN = "min"; //$NON-NLS-1$ |
40 |
private static final String MAX = "max"; //$NON-NLS-1$ |
41 |
private static final String TYPE = "type"; //$NON-NLS-1$ |
42 |
private static final String SIZE = "size"; //$NON-NLS-1$ |
43 |
private static final String ID = "id"; //$NON-NLS-1$ |
44 |
private static final String NAME = "name"; //$NON-NLS-1$ |
45 |
private static final String DESCRIPTION = "description"; //$NON-NLS-1$ |
46 |
private static final String RESOURCE = "resource"; //$NON-NLS-1$ |
47 |
private static final String PID = "pid"; //$NON-NLS-1$ |
48 |
private static final String DEFAULT = "default"; //$NON-NLS-1$ |
49 |
private static final String ADREF = "adref"; //$NON-NLS-1$ |
50 |
private static final String CONTENT = "content"; //$NON-NLS-1$ |
51 |
private static final String FACTORY = "factoryPid"; //$NON-NLS-1$ |
52 |
private static final String BUNDLE = "bundle"; //$NON-NLS-1$ |
53 |
private static final String OPTIONAL = "optional"; //$NON-NLS-1$ |
54 |
private static final String OBJECT = "Object"; //$NON-NLS-1$ |
55 |
private static final String OCDREF = "ocdref"; //$NON-NLS-1$ |
56 |
private static final String ATTRIBUTE = "Attribute"; //$NON-NLS-1$ |
57 |
private static final String DESIGNATE = "Designate"; //$NON-NLS-1$ |
58 |
private static final String MERGE = "merge"; //$NON-NLS-1$ |
59 |
private static final String REQUIRED = "required"; //$NON-NLS-1$ |
60 |
|
61 |
private static final String INTEGER = "Integer"; //$NON-NLS-1$ |
62 |
private static final String STRING = "String"; //$NON-NLS-1$ |
63 |
private static final String FLOAT = "Float"; //$NON-NLS-1$ |
64 |
private static final String DOUBLE = "Double"; //$NON-NLS-1$ |
65 |
private static final String BYTE = "Byte"; //$NON-NLS-1$ |
66 |
private static final String LONG = "Long"; //$NON-NLS-1$ |
67 |
private static final String CHAR = "Char"; //$NON-NLS-1$ |
68 |
private static final String BOOLEAN = "Boolean"; //$NON-NLS-1$ |
69 |
private static final String SHORT = "Short"; //$NON-NLS-1$ |
70 |
private static final String PASSWORD = "Password"; //$NON-NLS-1$ |
71 |
|
72 |
protected Bundle _dp_bundle; |
73 |
protected URL _dp_url; |
74 |
protected SAXParser _dp_parser; |
75 |
protected XMLReader _dp_xmlReader; |
76 |
|
77 |
// DesignateHanders in DataParser class |
78 |
Vector<DesignateHandler> _dp_designateHandlers = new Vector<DesignateHandler>(7); |
79 |
// ObjectClassDefinitions in DataParser class w/ corresponding reference keys |
80 |
Hashtable<String, ObjectClassDefinitionImpl> _dp_OCDs = new Hashtable<String, ObjectClassDefinitionImpl>(7); |
81 |
// Localization in DataParser class |
82 |
String _dp_localization; |
83 |
|
84 |
// Default visibility to avoid a plethora of synthetic accessor method warnings. |
85 |
final LogService logger; |
86 |
final Collection<Designate> designates = new ArrayList<Designate>(7); |
87 |
|
88 |
/* |
89 |
* Constructor of class DataParser. |
90 |
*/ |
91 |
public DataParser(Bundle bundle, URL url, SAXParser parser, LogService logger) { |
92 |
|
93 |
this._dp_bundle = bundle; |
94 |
this._dp_url = url; |
95 |
this._dp_parser = parser; |
96 |
this.logger = logger; |
97 |
} |
98 |
|
99 |
/* |
100 |
* Main method to parse specific MetaData file. |
101 |
*/ |
102 |
public Collection<Designate> doParse() throws IOException, SAXException { |
103 |
_dp_xmlReader = _dp_parser.getXMLReader(); |
104 |
_dp_xmlReader.setContentHandler(new RootHandler()); |
105 |
_dp_xmlReader.setErrorHandler(new MyErrorHandler(System.err)); |
106 |
InputStream is = _dp_url.openStream(); |
107 |
InputSource isource = new InputSource(is); |
108 |
logger.log(LogService.LOG_DEBUG, "Starting to parse " + _dp_url); //$NON-NLS-1$ |
109 |
_dp_xmlReader.parse(isource); |
110 |
return designates; |
111 |
} |
112 |
|
113 |
/* |
114 |
* Convert String for expected data type. |
115 |
*/ |
116 |
static Object convert(String value, int type) { |
117 |
|
118 |
if (value == null) { |
119 |
return null; |
120 |
} |
121 |
|
122 |
switch (type) { |
123 |
// PASSWORD should be treated like STRING. |
124 |
case AttributeDefinition.PASSWORD : |
125 |
case AttributeDefinition.STRING : |
126 |
// Both the min and max of STRING are Integers. |
127 |
return new Integer(value); |
128 |
case AttributeDefinition.LONG : |
129 |
return new Long(value); |
130 |
case AttributeDefinition.INTEGER : |
131 |
return new Integer(value); |
132 |
case AttributeDefinition.SHORT : |
133 |
return new Short(value); |
134 |
case AttributeDefinition.CHARACTER : |
135 |
return new Character(value.charAt(0)); |
136 |
case AttributeDefinition.BYTE : |
137 |
return new Byte(value); |
138 |
case AttributeDefinition.DOUBLE : |
139 |
return new Double(value); |
140 |
case AttributeDefinition.FLOAT : |
141 |
return new Float(value); |
142 |
case AttributeDefinition.BIGINTEGER : |
143 |
return new BigInteger(value); |
144 |
case AttributeDefinition.BIGDECIMAL : |
145 |
return new BigDecimal(value); |
146 |
case AttributeDefinition.BOOLEAN : |
147 |
return new Boolean(value); |
148 |
default : |
149 |
// Unknown data type |
150 |
return null; |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* Abstract of all Handlers. |
156 |
*/ |
157 |
private class AbstractHandler extends DefaultHandler { |
158 |
protected ContentHandler _doc_handler; |
159 |
protected boolean _isParsedDataValid = true; |
160 |
protected Map<String, Map<String, String>> extensionAttributes = new HashMap<String, Map<String, String>>(); |
161 |
|
162 |
public AbstractHandler(ContentHandler parentHandler) { |
163 |
this._doc_handler = parentHandler; |
164 |
_dp_xmlReader.setContentHandler(this); |
165 |
} |
166 |
|
167 |
public void endElement(String namespaceURI, String localName, String qName) { |
168 |
finished(); |
169 |
// Let parent resume handling SAX events |
170 |
_dp_xmlReader.setContentHandler(_doc_handler); |
171 |
} |
172 |
|
173 |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
174 |
throw new SAXException(NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, qName)); |
175 |
} |
176 |
|
177 |
public void characters(char[] buf, int start, int end) throws SAXException { |
178 |
String s = new String(buf, start, end).trim(); |
179 |
if (s.length() > 0) { |
180 |
throw new SAXException(NLS.bind(MetaTypeMsg.UNEXPECTED_TEXT, s)); |
181 |
} |
182 |
} |
183 |
|
184 |
protected void collectExtensionAttributes(Attributes attributes) { |
185 |
for (int i = 0; i < attributes.getLength(); i++) { |
186 |
String key = attributes.getURI(i); |
187 |
if (key.length() == 0 || key.startsWith("http://www.osgi.org/xmlns/metatype/v")) //$NON-NLS-1$ |
188 |
continue; |
189 |
Map<String, String> value = extensionAttributes.get(key); |
190 |
if (value == null) { |
191 |
value = new HashMap<String, String>(); |
192 |
extensionAttributes.put(key, value); |
193 |
} |
194 |
value.put(getName(attributes.getLocalName(i), attributes.getQName(i)), attributes.getValue(i)); |
195 |
} |
196 |
} |
197 |
|
198 |
/** |
199 |
* Called when this element and all elements nested into it have been |
200 |
* handled. |
201 |
*/ |
202 |
protected void finished() { |
203 |
// do nothing by default |
204 |
} |
205 |
} |
206 |
|
207 |
/** |
208 |
* Handler for the root element. |
209 |
*/ |
210 |
private class RootHandler extends DefaultHandler { |
211 |
|
212 |
public RootHandler() { |
213 |
super(); |
214 |
} |
215 |
|
216 |
public void startElement(String uri, String localName, String qName, Attributes attributes) { |
217 |
|
218 |
logger.log(LogService.LOG_DEBUG, "Here is AbstractHandler:startElement():" //$NON-NLS-1$ |
219 |
+ qName); |
220 |
String name = getName(localName, qName); |
221 |
if (name.equalsIgnoreCase(METADATA)) { |
222 |
new MetaDataHandler(this).init(name, attributes); |
223 |
} else { |
224 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
225 |
} |
226 |
} |
227 |
|
228 |
public void setDocumentLocator(Locator locator) { |
229 |
// do nothing |
230 |
} |
231 |
} |
232 |
|
233 |
/** |
234 |
* Handler for the MetaData element. |
235 |
*/ |
236 |
private class MetaDataHandler extends AbstractHandler { |
237 |
|
238 |
public MetaDataHandler(ContentHandler handler) { |
239 |
super(handler); |
240 |
} |
241 |
|
242 |
public void init(String name, Attributes attributes) { |
243 |
|
244 |
logger.log(LogService.LOG_DEBUG, "Here is MetaDataHandler():init()"); //$NON-NLS-1$ |
245 |
_dp_localization = attributes.getValue(LOCALIZATION); |
246 |
if (_dp_localization == null) { |
247 |
// Not a problem, because LOCALIZATION is an optional attribute. |
248 |
} |
249 |
// The global variable "_dp_localization" will be used within |
250 |
// OcdHandler and AttributeDefinitionHandler later. |
251 |
} |
252 |
|
253 |
public void startElement(String uri, String localName, String qName, Attributes atts) { |
254 |
|
255 |
logger.log(LogService.LOG_DEBUG, "Here is MetaDataHandler:startElement():" //$NON-NLS-1$ |
256 |
+ qName); |
257 |
String name = getName(localName, qName); |
258 |
if (name.equalsIgnoreCase(DESIGNATE)) { |
259 |
DesignateHandler designateHandler = new DesignateHandler(this); |
260 |
designateHandler.init(name, atts); |
261 |
if (designateHandler._isParsedDataValid) { |
262 |
_dp_designateHandlers.addElement(designateHandler); |
263 |
} |
264 |
} else if (name.equalsIgnoreCase(OCD)) { |
265 |
OcdHandler ocdHandler = new OcdHandler(this); |
266 |
ocdHandler.init(name, atts, _dp_OCDs); |
267 |
} else { |
268 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
269 |
} |
270 |
} |
271 |
|
272 |
protected void finished() { |
273 |
|
274 |
logger.log(LogService.LOG_DEBUG, "Here is MetaDataHandler():finished()"); //$NON-NLS-1$ |
275 |
if (_dp_designateHandlers.size() == 0) { |
276 |
// Schema defines at least one DESIGNATE is required. |
277 |
_isParsedDataValid = false; |
278 |
logger.log(LogService.LOG_WARNING, "DataParser.finished() " + NLS.bind(MetaTypeMsg.MISSING_ELEMENT, DESIGNATE)); //$NON-NLS-1$ |
279 |
return; |
280 |
} |
281 |
Enumeration<DesignateHandler> designateHandlerKeys = _dp_designateHandlers.elements(); |
282 |
while (designateHandlerKeys.hasMoreElements()) { |
283 |
DesignateHandler dh = designateHandlerKeys.nextElement(); |
284 |
|
285 |
ObjectClassDefinitionImpl ocd = _dp_OCDs.get(dh._ocdref); |
286 |
if (ocd != null) { |
287 |
designates.add(new Designate.Builder(ocd).bundle(dh._bundle_val).factoryPid(dh._factory_val).merge(dh._merge_val).pid(dh._pid_val).optional(dh._optional_val).build()); |
288 |
} else { |
289 |
logger.log(LogService.LOG_ERROR, "DataParser.finished() " + NLS.bind(MetaTypeMsg.OCD_ID_NOT_FOUND, dh._ocdref)); //$NON-NLS-1$ |
290 |
|
291 |
} |
292 |
} |
293 |
} |
294 |
} |
295 |
|
296 |
/** |
297 |
* Handler for the ObjectClassDefinition element. |
298 |
*/ |
299 |
private class OcdHandler extends AbstractHandler { |
300 |
|
301 |
Hashtable<String, ObjectClassDefinitionImpl> _parent_OCDs_hashtable; |
302 |
// This ID "_refID" is only used for reference by Designate element, |
303 |
// not the PID or FPID of this OCD. |
304 |
String _refID; |
305 |
ObjectClassDefinitionImpl _ocd; |
306 |
Vector<AttributeDefinitionImpl> _ad_vector = new Vector<AttributeDefinitionImpl>(7); |
307 |
|
308 |
public OcdHandler(ContentHandler handler) { |
309 |
super(handler); |
310 |
} |
311 |
|
312 |
public void init(String name, Attributes atts, Hashtable<String, ObjectClassDefinitionImpl> ocds_hashtable) { |
313 |
|
314 |
logger.log(LogService.LOG_DEBUG, "Here is OcdHandler():init()"); //$NON-NLS-1$ |
315 |
_parent_OCDs_hashtable = ocds_hashtable; |
316 |
collectExtensionAttributes(atts); |
317 |
String ocd_name_val = atts.getValue(NAME); |
318 |
if (ocd_name_val == null) { |
319 |
_isParsedDataValid = false; |
320 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes, Hashtable) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, NAME, name)); //$NON-NLS-1$ |
321 |
return; |
322 |
} |
323 |
|
324 |
String ocd_description_val = atts.getValue(DESCRIPTION); |
325 |
if (ocd_description_val == null) { |
326 |
// Not a problem, because DESCRIPTION is an optional attribute. |
327 |
} |
328 |
|
329 |
_refID = atts.getValue(ID); |
330 |
if (_refID == null) { |
331 |
_isParsedDataValid = false; |
332 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes, Hashtable) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, ID, name)); //$NON-NLS-1$ |
333 |
return; |
334 |
} |
335 |
|
336 |
_ocd = new ObjectClassDefinitionImpl(ocd_name_val, ocd_description_val, _refID, _dp_localization, extensionAttributes); |
337 |
} |
338 |
|
339 |
public void startElement(String uri, String localName, String qName, Attributes atts) { |
340 |
|
341 |
logger.log(LogService.LOG_DEBUG, "Here is OcdHandler:startElement():" //$NON-NLS-1$ |
342 |
+ qName); |
343 |
if (!_isParsedDataValid) |
344 |
return; |
345 |
|
346 |
String name = getName(localName, qName); |
347 |
if (name.equalsIgnoreCase(AD)) { |
348 |
AttributeDefinitionHandler attributeDefHandler = new AttributeDefinitionHandler(this); |
349 |
attributeDefHandler.init(name, atts, _ad_vector); |
350 |
} else if (name.equalsIgnoreCase(ICON)) { |
351 |
IconHandler iconHandler = new IconHandler(this); |
352 |
iconHandler.init(name, atts); |
353 |
if (iconHandler._isParsedDataValid) { |
354 |
// Because XML schema allows at most one icon for |
355 |
// one OCD, if more than one icons are read from |
356 |
// MetaData, then only the final icon will be kept. |
357 |
_ocd.setIcon(iconHandler._icon); |
358 |
} |
359 |
} else { |
360 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
361 |
} |
362 |
} |
363 |
|
364 |
protected void finished() { |
365 |
|
366 |
logger.log(LogService.LOG_DEBUG, "Here is OcdHandler():finished()"); //$NON-NLS-1$ |
367 |
if (!_isParsedDataValid) |
368 |
return; |
369 |
|
370 |
if (_ad_vector.size() == 0) { |
371 |
// Schema defines at least one AD is required. |
372 |
_isParsedDataValid = false; |
373 |
logger.log(LogService.LOG_ERROR, "DataParser.finished() " + NLS.bind(MetaTypeMsg.MISSING_ELEMENT, AD, _refID)); //$NON-NLS-1$ |
374 |
return; |
375 |
} |
376 |
// OCD gets all parsed ADs. |
377 |
Enumeration<AttributeDefinitionImpl> adKey = _ad_vector.elements(); |
378 |
while (adKey.hasMoreElements()) { |
379 |
AttributeDefinitionImpl ad = adKey.nextElement(); |
380 |
_ocd.addAttributeDefinition(ad, ad._isRequired); |
381 |
} |
382 |
|
383 |
_parent_OCDs_hashtable.put(_refID, _ocd); |
384 |
} |
385 |
} |
386 |
|
387 |
/** |
388 |
* Handler for the Icon element. |
389 |
*/ |
390 |
private class IconHandler extends AbstractHandler { |
391 |
|
392 |
Icon _icon; |
393 |
|
394 |
public IconHandler(ContentHandler handler) { |
395 |
super(handler); |
396 |
} |
397 |
|
398 |
public void init(String name, Attributes atts) { |
399 |
|
400 |
logger.log(LogService.LOG_DEBUG, "Here is IconHandler:init()"); //$NON-NLS-1$ |
401 |
String icon_resource_val = atts.getValue(RESOURCE); |
402 |
if (icon_resource_val == null) { |
403 |
_isParsedDataValid = false; |
404 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, RESOURCE, name)); //$NON-NLS-1$ |
405 |
return; |
406 |
} |
407 |
|
408 |
String icon_size_val = atts.getValue(SIZE); |
409 |
if (icon_size_val == null) { |
410 |
// Not a problem, because SIZE is an optional attribute. |
411 |
icon_size_val = "0"; //$NON-NLS-1$ |
412 |
} else if (icon_size_val.equalsIgnoreCase("")) { //$NON-NLS-1$ |
413 |
icon_size_val = "0"; //$NON-NLS-1$ |
414 |
} |
415 |
|
416 |
_icon = new Icon(icon_resource_val, Integer.parseInt(icon_size_val), _dp_bundle); |
417 |
} |
418 |
} |
419 |
|
420 |
/** |
421 |
* Handler for the Attribute element. |
422 |
*/ |
423 |
private class AttributeDefinitionHandler extends AbstractHandler { |
424 |
|
425 |
AttributeDefinitionImpl _ad; |
426 |
int _dataType; |
427 |
|
428 |
Vector<AttributeDefinitionImpl> _parent_ADs_vector; |
429 |
Vector<String> _optionLabel_vector = new Vector<String>(7); |
430 |
Vector<String> _optionValue_vector = new Vector<String>(7); |
431 |
|
432 |
public AttributeDefinitionHandler(ContentHandler handler) { |
433 |
super(handler); |
434 |
} |
435 |
|
436 |
public void init(String name, Attributes atts, Vector<AttributeDefinitionImpl> ad_vector) { |
437 |
|
438 |
logger.log(LogService.LOG_DEBUG, "Here is AttributeDefinitionHandler():init()"); //$NON-NLS-1$ |
439 |
_parent_ADs_vector = ad_vector; |
440 |
collectExtensionAttributes(atts); |
441 |
String ad_name_val = atts.getValue(NAME); |
442 |
if (ad_name_val == null) { |
443 |
// Not a problem, because NAME is an optional attribute. |
444 |
} |
445 |
|
446 |
String ad_description_val = atts.getValue(DESCRIPTION); |
447 |
if (ad_description_val == null) { |
448 |
// Not a problem, because DESCRIPTION is an optional attribute. |
449 |
} |
450 |
|
451 |
String ad_id_val = atts.getValue(ID); |
452 |
if (ad_id_val == null) { |
453 |
_isParsedDataValid = false; |
454 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes, Vector) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, ID, name)); //$NON-NLS-1$ |
455 |
return; |
456 |
} |
457 |
|
458 |
String ad_type_val = atts.getValue(TYPE); |
459 |
if (ad_type_val == null) { |
460 |
_isParsedDataValid = false; |
461 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes, Vector) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, TYPE, name)); //$NON-NLS-1$ |
462 |
return; |
463 |
} |
464 |
if (ad_type_val.equalsIgnoreCase(STRING)) { |
465 |
_dataType = AttributeDefinition.STRING; |
466 |
} else if (ad_type_val.equalsIgnoreCase(LONG)) { |
467 |
_dataType = AttributeDefinition.LONG; |
468 |
} else if (ad_type_val.equalsIgnoreCase(DOUBLE)) { |
469 |
_dataType = AttributeDefinition.DOUBLE; |
470 |
} else if (ad_type_val.equalsIgnoreCase(FLOAT)) { |
471 |
_dataType = AttributeDefinition.FLOAT; |
472 |
} else if (ad_type_val.equalsIgnoreCase(INTEGER)) { |
473 |
_dataType = AttributeDefinition.INTEGER; |
474 |
} else if (ad_type_val.equalsIgnoreCase(BYTE)) { |
475 |
_dataType = AttributeDefinition.BYTE; |
476 |
} else if (ad_type_val.equalsIgnoreCase(CHAR)) { |
477 |
_dataType = AttributeDefinition.CHARACTER; |
478 |
} else if (ad_type_val.equalsIgnoreCase(BOOLEAN)) { |
479 |
_dataType = AttributeDefinition.BOOLEAN; |
480 |
} else if (ad_type_val.equalsIgnoreCase(SHORT)) { |
481 |
_dataType = AttributeDefinition.SHORT; |
482 |
} else if (ad_type_val.equalsIgnoreCase(PASSWORD)) { |
483 |
_dataType = AttributeDefinition.PASSWORD; |
484 |
} else { |
485 |
_isParsedDataValid = false; |
486 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes, Vector) " + NLS.bind(MetaTypeMsg.INVALID_TYPE, new Object[] {ad_type_val, _dp_url, _dp_bundle.getBundleId()})); //$NON-NLS-1$ |
487 |
return; |
488 |
} |
489 |
|
490 |
String ad_cardinality_str = atts.getValue(CARDINALITY); |
491 |
int ad_cardinality_val = 0; |
492 |
if (ad_cardinality_str == null) { |
493 |
// Not a problem, because CARDINALITY is an optional attribute. |
494 |
// And the default value is 0. |
495 |
} else { |
496 |
ad_cardinality_val = Integer.parseInt(ad_cardinality_str); |
497 |
} |
498 |
|
499 |
String ad_min_val = atts.getValue(MIN); |
500 |
if (ad_min_val == null) { |
501 |
// Not a problem, because MIN is an optional attribute. |
502 |
} |
503 |
|
504 |
String ad_max_val = atts.getValue(MAX); |
505 |
if (ad_max_val == null) { |
506 |
// Not a problem, because MAX is an optional attribute. |
507 |
} |
508 |
|
509 |
String ad_defaults_str = atts.getValue(DEFAULT); |
510 |
if (ad_defaults_str == null) { |
511 |
// Not a problem, because DEFAULT is an optional attribute. |
512 |
} |
513 |
|
514 |
String ad_required_val = atts.getValue(REQUIRED); |
515 |
if (ad_required_val == null) { |
516 |
// Not a problem, because REQUIRED is an optional attribute. |
517 |
// And the default value is 'true'. |
518 |
ad_required_val = Boolean.TRUE.toString(); |
519 |
} |
520 |
|
521 |
_ad = new AttributeDefinitionImpl(ad_id_val, ad_name_val, ad_description_val, _dataType, ad_cardinality_val, convert(ad_min_val, _dataType), convert(ad_max_val, _dataType), Boolean.valueOf(ad_required_val).booleanValue(), _dp_localization, logger, extensionAttributes); |
522 |
|
523 |
if (ad_defaults_str != null) { |
524 |
_ad.setDefaultValue(ad_defaults_str, true); |
525 |
} |
526 |
} |
527 |
|
528 |
public void startElement(String uri, String localName, String qName, Attributes atts) { |
529 |
|
530 |
logger.log(LogService.LOG_DEBUG, "Here is AttributeDefinitionHandler:startElement():" //$NON-NLS-1$ |
531 |
+ qName); |
532 |
if (!_isParsedDataValid) |
533 |
return; |
534 |
|
535 |
String name = getName(localName, qName); |
536 |
if (name.equalsIgnoreCase(OPTION)) { |
537 |
OptionHandler optionHandler = new OptionHandler(this); |
538 |
optionHandler.init(name, atts); |
539 |
if (optionHandler._isParsedDataValid) { |
540 |
// Only add valid Option |
541 |
_optionLabel_vector.addElement(optionHandler._label_val); |
542 |
_optionValue_vector.addElement(optionHandler._value_val); |
543 |
} |
544 |
} else { |
545 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
546 |
} |
547 |
} |
548 |
|
549 |
protected void finished() { |
550 |
|
551 |
logger.log(LogService.LOG_DEBUG, "Here is AttributeDefinitionHandler():finished()"); //$NON-NLS-1$ |
552 |
if (!_isParsedDataValid) |
553 |
return; |
554 |
|
555 |
_ad.setOption(_optionLabel_vector, _optionValue_vector, true); |
556 |
_parent_ADs_vector.addElement(_ad); |
557 |
} |
558 |
} |
559 |
|
560 |
/** |
561 |
* Handler for the Option element. |
562 |
*/ |
563 |
private class OptionHandler extends AbstractHandler { |
564 |
|
565 |
String _label_val; |
566 |
String _value_val; |
567 |
|
568 |
public OptionHandler(ContentHandler handler) { |
569 |
super(handler); |
570 |
} |
571 |
|
572 |
public void init(String name, Attributes atts) { |
573 |
|
574 |
logger.log(LogService.LOG_DEBUG, "Here is OptionHandler:init()"); //$NON-NLS-1$ |
575 |
_label_val = atts.getValue(LABEL); |
576 |
if (_label_val == null) { |
577 |
_isParsedDataValid = false; |
578 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, LABEL, name)); //$NON-NLS-1$ |
579 |
return; |
580 |
} |
581 |
|
582 |
_value_val = atts.getValue(VALUE); |
583 |
if (_value_val == null) { |
584 |
_isParsedDataValid = false; |
585 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, VALUE, name)); //$NON-NLS-1$ |
586 |
return; |
587 |
} |
588 |
} |
589 |
} |
590 |
|
591 |
// /** |
592 |
// * Handler for the Simple Value element. |
593 |
// */ |
594 |
// private class SimpleValueHandler extends AbstractHandler { |
595 |
// |
596 |
// StringBuffer _buffer = new StringBuffer(); |
597 |
// Vector _parent_value_vector; |
598 |
// String _elementName; |
599 |
// |
600 |
// public SimpleValueHandler(ContentHandler handler) { |
601 |
// super(handler); |
602 |
// } |
603 |
// |
604 |
// public void init(String name, Attributes atts, Vector value_vector) |
605 |
// throws SAXException { |
606 |
// |
607 |
// Logging.log(LogService.LOG_DEBUG, |
608 |
// "Here is SimpleValueHandler():init()"); //$NON-NLS-1$ |
609 |
// _elementName = name; |
610 |
// _parent_value_vector = value_vector; |
611 |
// } |
612 |
// |
613 |
// protected void finished() throws SAXException { |
614 |
// |
615 |
// Logging.log(LogService.LOG_DEBUG, |
616 |
// "Here is SimpleValueHandler():finished()"); //$NON-NLS-1$ |
617 |
// if (_parent_value_vector != null) { |
618 |
// _parent_value_vector.addElement(_buffer.toString()); |
619 |
// } |
620 |
// } |
621 |
// |
622 |
// public void characters(char buf[], int offset, int len) |
623 |
// throws SAXException { |
624 |
// |
625 |
// Logging.log(LogService.LOG_DEBUG, |
626 |
// "Here is SimpleValueHandler(" //$NON-NLS-1$ |
627 |
// + _elementName |
628 |
// + "):characters():[" //$NON-NLS-1$ |
629 |
// + new String(buf, offset, len) |
630 |
// + "]"); //$NON-NLS-1$ |
631 |
// _buffer.append(new String(buf, offset, len)); |
632 |
// } |
633 |
// } |
634 |
|
635 |
/** |
636 |
* Handler for the Designate element. |
637 |
*/ |
638 |
class DesignateHandler extends AbstractHandler { |
639 |
|
640 |
String _pid_val = null; |
641 |
String _factory_val = null; |
642 |
String _bundle_val = null; // Only used by RFC94 |
643 |
boolean _optional_val = false; // Only used by RFC94 |
644 |
boolean _merge_val = false; // Only used by RFC94 |
645 |
|
646 |
// Referenced OCD ID |
647 |
String _ocdref; |
648 |
|
649 |
public DesignateHandler(ContentHandler handler) { |
650 |
super(handler); |
651 |
} |
652 |
|
653 |
public void init(String name, Attributes atts) { |
654 |
|
655 |
logger.log(LogService.LOG_DEBUG, "Here is DesignateHandler():init()"); //$NON-NLS-1$ |
656 |
_pid_val = atts.getValue(PID); |
657 |
_factory_val = atts.getValue(FACTORY); |
658 |
if (_pid_val == null && _factory_val == null) { |
659 |
_isParsedDataValid = false; |
660 |
logger.log(LogService.LOG_ERROR, MetaTypeMsg.MISSING_DESIGNATE_PID_AND_FACTORYPID); |
661 |
return; |
662 |
} |
663 |
|
664 |
_bundle_val = atts.getValue(BUNDLE); |
665 |
if (_bundle_val == null) { |
666 |
// Not a problem because BUNDLE is an optional attribute. |
667 |
} |
668 |
|
669 |
String optional_str = atts.getValue(OPTIONAL); |
670 |
if (optional_str == null) { |
671 |
// Not a problem, because OPTIONAL is an optional attribute. |
672 |
// The default value is "false". |
673 |
_optional_val = false; |
674 |
} else { |
675 |
_optional_val = Boolean.valueOf(optional_str).booleanValue(); |
676 |
} |
677 |
|
678 |
String merge_str = atts.getValue(MERGE); |
679 |
if (merge_str == null) { |
680 |
// Not a problem, because MERGE is an optional attribute. |
681 |
// The default value is "false". |
682 |
_merge_val = false; |
683 |
} else { |
684 |
_merge_val = Boolean.valueOf(merge_str).booleanValue(); |
685 |
} |
686 |
} |
687 |
|
688 |
public void startElement(String uri, String localName, String qName, Attributes atts) { |
689 |
|
690 |
logger.log(LogService.LOG_DEBUG, "Here is DesignateHandler:startElement():" //$NON-NLS-1$ |
691 |
+ qName); |
692 |
if (!_isParsedDataValid) |
693 |
return; |
694 |
|
695 |
String name = getName(localName, qName); |
696 |
if (name.equalsIgnoreCase(OBJECT)) { |
697 |
ObjectHandler objectHandler = new ObjectHandler(this); |
698 |
objectHandler.init(name, atts); |
699 |
if (objectHandler._isParsedDataValid) { |
700 |
_ocdref = objectHandler._ocdref; |
701 |
} |
702 |
} else { |
703 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
704 |
} |
705 |
} |
706 |
|
707 |
protected void finished() { |
708 |
|
709 |
logger.log(LogService.LOG_DEBUG, "Here is DesignateHandler():finished()"); //$NON-NLS-1$ |
710 |
if (!_isParsedDataValid) |
711 |
return; |
712 |
|
713 |
if (_ocdref == null) { |
714 |
_isParsedDataValid = false; |
715 |
// Schema defines at least one OBJECT is required. |
716 |
logger.log(LogService.LOG_ERROR, "DataParser.finished() " + NLS.bind(MetaTypeMsg.MISSING_ELEMENT, OBJECT, _pid_val)); //$NON-NLS-1$ |
717 |
return; |
718 |
|
719 |
} |
720 |
} |
721 |
} |
722 |
|
723 |
/** |
724 |
* Handler for the Object element. |
725 |
*/ |
726 |
private class ObjectHandler extends AbstractHandler { |
727 |
|
728 |
String _ocdref; |
729 |
|
730 |
public ObjectHandler(ContentHandler handler) { |
731 |
super(handler); |
732 |
} |
733 |
|
734 |
public void init(String name, Attributes atts) { |
735 |
|
736 |
logger.log(LogService.LOG_DEBUG, "Here is ObjectHandler():init()"); //$NON-NLS-1$ |
737 |
_ocdref = atts.getValue(OCDREF); |
738 |
if (_ocdref == null) { |
739 |
_isParsedDataValid = false; |
740 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, OCDREF, name)); //$NON-NLS-1$ |
741 |
return; |
742 |
} |
743 |
} |
744 |
|
745 |
public void startElement(String uri, String localName, String qName, Attributes atts) { |
746 |
|
747 |
logger.log(LogService.LOG_DEBUG, "Here is ObjectHandler:startElement():" //$NON-NLS-1$ |
748 |
+ qName); |
749 |
if (!_isParsedDataValid) |
750 |
return; |
751 |
|
752 |
String name = getName(localName, qName); |
753 |
if (name.equalsIgnoreCase(ATTRIBUTE)) { |
754 |
AttributeHandler attributeHandler = new AttributeHandler(this); |
755 |
attributeHandler.init(name, atts); |
756 |
// The ATTRIBUTE element is only used by RFC94, do nothing for it here. |
757 |
} else { |
758 |
logger.log(LogService.LOG_WARNING, NLS.bind(MetaTypeMsg.UNEXPECTED_ELEMENT, name)); |
759 |
} |
760 |
} |
761 |
} |
762 |
|
763 |
/** |
764 |
* Handler for the Attribute element. |
765 |
* |
766 |
* This Handler is only used by RFC94. |
767 |
*/ |
768 |
private class AttributeHandler extends AbstractHandler { |
769 |
|
770 |
String _adref_val; |
771 |
String _content_val; |
772 |
|
773 |
public AttributeHandler(ContentHandler handler) { |
774 |
super(handler); |
775 |
} |
776 |
|
777 |
public void init(String name, Attributes atts) { |
778 |
|
779 |
logger.log(LogService.LOG_DEBUG, "Here is AttributeHandler():init()"); //$NON-NLS-1$ |
780 |
_adref_val = atts.getValue(ADREF); |
781 |
if (_adref_val == null) { |
782 |
_isParsedDataValid = false; |
783 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, ADREF, name)); //$NON-NLS-1$ |
784 |
return; |
785 |
} |
786 |
|
787 |
_content_val = atts.getValue(CONTENT); |
788 |
if (_content_val == null) { |
789 |
_isParsedDataValid = false; |
790 |
logger.log(LogService.LOG_ERROR, "DataParser.init(String, Attributes) " + NLS.bind(MetaTypeMsg.MISSING_ATTRIBUTE, CONTENT, name)); //$NON-NLS-1$ |
791 |
return; |
792 |
} |
793 |
} |
794 |
} |
795 |
|
796 |
/** |
797 |
* Error Handler to report errors and warnings |
798 |
*/ |
799 |
private static class MyErrorHandler implements ErrorHandler { |
800 |
|
801 |
/** Error handler output goes here */ |
802 |
private PrintStream _out; |
803 |
|
804 |
MyErrorHandler(PrintStream out) { |
805 |
this._out = out; |
806 |
} |
807 |
|
808 |
/** |
809 |
* Returns a string describing parse exception details |
810 |
*/ |
811 |
private String getParseExceptionInfo(SAXParseException spe) { |
812 |
String systemId = spe.getSystemId(); |
813 |
if (systemId == null) { |
814 |
systemId = "null"; //$NON-NLS-1$ |
815 |
} |
816 |
String info = "URI=" + systemId + //$NON-NLS-1$ |
817 |
" Line=" + spe.getLineNumber() + //$NON-NLS-1$ |
818 |
": " + spe.getMessage(); //$NON-NLS-1$ |
819 |
|
820 |
return info; |
821 |
} |
822 |
|
823 |
// The following methods are standard SAX ErrorHandler methods. |
824 |
// See SAX documentation for more info. |
825 |
|
826 |
public void warning(SAXParseException spe) { |
827 |
_out.println("Warning: " + getParseExceptionInfo(spe)); //$NON-NLS-1$ |
828 |
} |
829 |
|
830 |
public void error(SAXParseException spe) throws SAXException { |
831 |
String message = "Error: " + getParseExceptionInfo(spe); //$NON-NLS-1$ |
832 |
throw new SAXException(message); |
833 |
} |
834 |
|
835 |
public void fatalError(SAXParseException spe) throws SAXException { |
836 |
String message = "Fatal Error: " + getParseExceptionInfo(spe); //$NON-NLS-1$ |
837 |
throw new SAXException(message); |
838 |
} |
839 |
} |
840 |
|
841 |
public static String getName(String localName, String qName) { |
842 |
if (localName != null && localName.length() > 0) { |
843 |
return localName; |
844 |
} |
845 |
|
846 |
int nameSpaceIndex = qName.indexOf(":"); //$NON-NLS-1$ |
847 |
return nameSpaceIndex == -1 ? qName : qName.substring(nameSpaceIndex + 1); |
848 |
} |
849 |
} |