Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a1081f11c8bc8c067935ed51d9472d94ed5ce560 (plain) (blame)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*******************************************************************************
 * Copyright (c) 2010-2011 Composent, Inc. and others. 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:
 *   Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.internal.osgi.services.remoteserviceadmin;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class EndpointDescriptionParser {

	private static List<String> multiValueTypes;

	static {
		multiValueTypes = Arrays.asList(new String[] { "String", "Long", //$NON-NLS-1$ //$NON-NLS-2$
				"long", "Double", "double", "float", "Float", "int", "Integer", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
				"byte", "Byte", "char", "Character", "boolean", "Boolean", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
				"short", "Short" }); //$NON-NLS-1$ //$NON-NLS-2$
	}

	private static final String ENDPOINT_DESCRIPTIONS = "endpoint-descriptions"; //$NON-NLS-1$
	private static final String ENDPOINT_DESCRIPTION = "endpoint-description"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY = "property"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_NAME = "name"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_VALUE = "value"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_VALUETYPE = "value-type"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_ARRAY = "array"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_LIST = "list"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_SET = "set"; //$NON-NLS-1$
	private static final String ENDPOINT_PROPERTY_XML = "xml"; //$NON-NLS-1$

	public static String[] noAttributes = new String[0];

	private XMLReader xmlReader;
	protected Locator locator = null; // document locator, if supported by the
										// parser

	class IgnoringHandler extends AbstractHandler {

		public IgnoringHandler(AbstractHandler parent) {
			super(parent);
			this.elementHandled = "IgnoringAll"; //$NON-NLS-1$
		}

		public void startElement(String name, Attributes attributes) {
			noSubElements(name, attributes);
		}

	}

	/**
	 * Abstract base class for content handlers
	 */
	abstract class AbstractHandler extends DefaultHandler {

		protected ContentHandler parentHandler = null;
		protected String elementHandled = null;

		protected StringBuffer characters = null; // character data inside an
													// element

		public AbstractHandler() {
			// Empty constructor for a root handler
		}

		public AbstractHandler(ContentHandler parentHandler) {
			this.parentHandler = parentHandler;
			xmlReader.setContentHandler(this);
		}

		public AbstractHandler(ContentHandler parentHandler,
				String elementHandled) {
			this.parentHandler = parentHandler;
			xmlReader.setContentHandler(this);
			this.elementHandled = elementHandled;
		}

		/**
		 * Set the document locator for the parser
		 * 
		 * @see org.xml.sax.ContentHandler#setDocumentLocator
		 */
		public void setDocumentLocator(Locator docLocator) {
			locator = docLocator;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			finishCharacters();
			String name = makeSimpleName(localName, qName);
			startElement(name, attributes);
		}

		public abstract void startElement(String name, Attributes attributes)
				throws SAXException;

		public void invalidElement(String name, Attributes attributes) {
			unexpectedElement(this, name, attributes);
			new IgnoringHandler(this);
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			finishCharacters();
			finished();
			// Restore the parent content handler
			xmlReader.setContentHandler(parentHandler);
		}

		/**
		 * An implementation for startElement when there are no sub-elements
		 */
		protected void noSubElements(String name, Attributes attributes) {
			unexpectedElement(this, name, attributes);
			// Create a new handler to ignore subsequent nested elements
			new IgnoringHandler(this);
		}

		/*
		 * Save up character data until endElement or nested startElement
		 * 
		 * @see org.xml.sax.ContentHandler#characters
		 */
		public void characters(char[] chars, int start, int length) {
			if (this.characters == null) {
				this.characters = new StringBuffer();
			}
			this.characters.append(chars, start, length);
		}

		// Consume the characters accumulated in this.characters.
		// Called before startElement or endElement
		private String finishCharacters() {
			// common case -- no characters or only whitespace
			if (this.characters == null || this.characters.length() == 0) {
				return null;
			}
			if (allWhiteSpace(this.characters)) {
				this.characters.setLength(0);
				return null;
			}

			// process the characters
			try {
				String trimmedChars = this.characters.toString().trim();
				if (trimmedChars.length() == 0) {
					// this shouldn't happen due to the test for allWhiteSpace
					// above
					System.err.println("Unexpected non-whitespace characters: " //$NON-NLS-1$
							+ trimmedChars);
					return null;
				}
				processCharacters(trimmedChars);
				return trimmedChars;
			} finally {
				this.characters.setLength(0);
			}
		}

		// Method to override in the handler of an element with CDATA.
		protected void processCharacters(String data) {
			if (data.length() > 0) {
				unexpectedCharacterData(this, data);
			}
		}

		private boolean allWhiteSpace(StringBuffer sb) {
			int length = sb.length();
			for (int i = 0; i < length; i += 1) {
				if (!Character.isWhitespace(sb.charAt(i))) {
					return false;
				}
			}
			return true;
		}

		/**
		 * Called when this element and all elements nested into it have been
		 * handled.
		 */
		protected void finished() {
			// Do nothing by default
		}

		/*
		 * A name used to identify the handler.
		 */
		public String getName() {
			return (elementHandled != null ? elementHandled : "NoName"); //$NON-NLS-1$
		}

		/**
		 * Parse the attributes of an element with only required attributes.
		 */
		protected String[] parseRequiredAttributes(Attributes attributes,
				String[] required) {
			return parseAttributes(attributes, required, noAttributes);
		}

		/**
		 * Parse the attributes of an element with a single optional attribute.
		 */
		protected String parseOptionalAttribute(Attributes attributes,
				String name) {
			return parseAttributes(attributes, noAttributes,
					new String[] { name })[0];
		}

		/**
		 * Parse the attributes of an element, given the list of required and
		 * optional ones. Return values in same order, null for those not
		 * present. Log warnings for extra attributes or missing required
		 * attributes.
		 */
		protected String[] parseAttributes(Attributes attributes,
				String[] required, String[] optional) {
			String[] result = new String[required.length + optional.length];
			for (int i = 0; i < attributes.getLength(); i += 1) {
				String name = attributes.getLocalName(i);
				String value = attributes.getValue(i).trim();
				int j;
				if ((j = indexOf(required, name)) >= 0) {
					result[j] = value;
				} else if ((j = indexOf(optional, name)) >= 0) {
					result[required.length + j] = value;
				} else {
					unexpectedAttribute(elementHandled, name, value);
				}
			}
			for (int i = 0; i < required.length; i += 1) {
				checkRequiredAttribute(elementHandled, required[i], result[i]);
			}
			return result;
		}

	}

	SAXParser getParser() throws ParserConfigurationException, SAXException {
		Activator a = Activator.getDefault();
		if (a == null)
			return null;

		SAXParserFactory factory = a.getSAXParserFactory();
		if (factory == null) {
			throw new SAXException("Unable to acquire sax parser"); //$NON-NLS-1$
		}
		factory.setNamespaceAware(true);
		factory.setValidating(false);
		try {
			factory.setFeature(
					"http://xml.org/sax/features/string-interning", true); //$NON-NLS-1$
		} catch (SAXException se) {
			// some parsers may not support string interning
		}
		SAXParser theParser = factory.newSAXParser();
		if (theParser == null) {
			throw new SAXException("Unable to create sax parser"); //$NON-NLS-1$
		}
		xmlReader = theParser.getXMLReader();
		return theParser;
	}

	abstract class RootHandler extends AbstractHandler {

		public RootHandler() {
			super();
		}

		public void initialize(DocHandler document, String rootName,
				Attributes attributes) {
			this.parentHandler = document;
			this.elementHandled = rootName;
			handleRootAttributes(attributes);
		}

		protected abstract void handleRootAttributes(Attributes attributes);

	}

	class DocHandler extends AbstractHandler {

		RootHandler rootHandler;

		public DocHandler(String rootName, RootHandler rootHandler) {
			super(null, rootName);
			this.rootHandler = rootHandler;
		}

		public void startElement(String name, Attributes attributes) {
			if (name.equals(elementHandled)) {
				rootHandler.initialize(this, name, attributes);
				xmlReader.setContentHandler(rootHandler);
			} else {
				this.noSubElements(name, attributes);
			}
		}

	}

	class EndpointDescriptionDocHandler extends DocHandler {

		public EndpointDescriptionDocHandler(String rootName,
				RootHandler rootHandler) {
			super(rootName, rootHandler);
		}

		public void processingInstruction(String target, String data)
				throws SAXException {
			// do nothing
		}
	}

	class EndpointDescriptionsHandler extends RootHandler {

		private List<EndpointDescription> endpointDescriptions = new ArrayList<EndpointDescription>();
		private EndpointDescriptionHandler endpointDescriptionHandler;

		protected void handleRootAttributes(Attributes attributes) {
		}

		public void startElement(String name, Attributes attributes)
				throws SAXException {
			if (ENDPOINT_DESCRIPTION.equals(name)) {
				if (endpointDescriptionHandler == null) {
					endpointDescriptionHandler = new EndpointDescriptionHandler(
							this, attributes, endpointDescriptions);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else {
				invalidElement(name, attributes);
			}
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			if (elementHandled.equals(localName)) {
				endpointDescriptionHandler = null;
				super.endElement(namespaceURI, localName, qName);
			}
		}

		public List<EndpointDescription> getEndpointDescriptions() {
			return endpointDescriptions;
		}
	}

	class EndpointDescriptionHandler extends AbstractHandler {

		private Map<String, Object> properties;
		private List<EndpointDescription> descriptions;

		public EndpointDescriptionHandler(ContentHandler parentHandler,
				Attributes attributes, List<EndpointDescription> descriptions) {
			super(parentHandler, ENDPOINT_DESCRIPTION);
			this.properties = new TreeMap<String, Object>(
					String.CASE_INSENSITIVE_ORDER);
			this.descriptions = descriptions;
		}

		public void startElement(String name, Attributes attributes)
				throws SAXException {
			if (ENDPOINT_PROPERTY.equals(name)) {
				new EndpointPropertyHandler(this, attributes, properties);
			}
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			if (elementHandled.equals(localName)) {
				this.descriptions.add(new EndpointDescription(properties));
				super.endElement(namespaceURI, localName, qName);
			}
		}

	}

	abstract class MultiValueHandler extends AbstractHandler {

		protected String valueType;

		public MultiValueHandler(ContentHandler parentHandler,
				String elementHandled, String valueType) {
			super(parentHandler, elementHandled);
			this.valueType = valueType;
		}

		protected Object createValue(String value) {
			if (value == null)
				return null;
			if (valueType.equals("String")) { //$NON-NLS-1$
				return value;
			} else if (valueType.equals("long") || valueType.equals("Long")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Long.valueOf(value);
			} else if (valueType.equals("double") || valueType.equals("Double")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Double.valueOf(value);
			} else if (valueType.equals("float") || valueType.equals("Float")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Float.valueOf(value);
			} else if (valueType.equals("int") || valueType.equals("Integer")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Integer.valueOf(value);
			} else if (valueType.equals("byte") || valueType.equals("Byte")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Byte.valueOf(value);
			} else if (valueType.equals("char") //$NON-NLS-1$
					|| valueType.equals("Character")) { //$NON-NLS-1$
				char[] chars = new char[1];
				value.getChars(0, 1, chars, 0);
				return Character.valueOf(chars[0]);
			} else if (valueType.equals("boolean") //$NON-NLS-1$
					|| valueType.equals("Boolean")) { //$NON-NLS-1$
				return Boolean.valueOf(value);
			} else if (valueType.equals("short") || valueType.equals("Short")) { //$NON-NLS-1$ //$NON-NLS-2$
				return Short.valueOf(value);
			}
			return null;
		}

		public void startElement(String name, Attributes attributes)
				throws SAXException {
			if (ENDPOINT_PROPERTY_VALUE.equals(name)) {
				characters = new StringBuffer();
			}
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			if (ENDPOINT_PROPERTY_VALUE.equals(localName)) {
				Object value = createValue(processValue((characters == null) ? null
						: characters.toString()));
				if (value != null)
					addValue(value);
				characters = null;
			} else if (elementHandled.equals(localName)) {
				super.endElement(namespaceURI, localName, qName);
			}
		}

		private String processValue(String characters) {
			if (characters == null || characters.length() == 0)
				return null;
			if (valueType.equals("String")) //$NON-NLS-1$
				return characters;
			int startIndex = 0;
			while (Character.isWhitespace(characters.charAt(startIndex)))
				startIndex++;
			int endIndex = characters.length() - 1;
			while (Character.isWhitespace(characters.charAt(endIndex)))
				endIndex--;
			return characters.substring(startIndex, endIndex + 1);
		}

		protected abstract void addValue(Object value);

		public abstract Object getValues();
	}

	class ArrayMultiValueHandler extends MultiValueHandler {

		private List<Object> values = new ArrayList<Object>();

		public ArrayMultiValueHandler(ContentHandler parentHandler,
				String elementHandled, String valueType) {
			super(parentHandler, elementHandled, valueType);
		}

		protected Object[] createEmptyArrayOfType() {
			if (valueType.equals("String")) { //$NON-NLS-1$
				return new String[] {};
			} else if (valueType.equals("long") || valueType.equals("Long")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Long[] {};
			} else if (valueType.equals("double") || valueType.equals("Double")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Double[] {};
			} else if (valueType.equals("float") || valueType.equals("Float")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Double[] {};
			} else if (valueType.equals("int") || valueType.equals("Integer")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Integer[] {};
			} else if (valueType.equals("byte") || valueType.equals("Byte")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Byte[] {};
			} else if (valueType.equals("char") //$NON-NLS-1$
					|| valueType.equals("Character")) { //$NON-NLS-1$
				return new Character[] {};
			} else if (valueType.equals("boolean") //$NON-NLS-1$
					|| valueType.equals("Boolean")) { //$NON-NLS-1$
				return new Boolean[] {};
			} else if (valueType.equals("short") || valueType.equals("Short")) { //$NON-NLS-1$ //$NON-NLS-2$
				return new Short[] {};
			} else
				return null;
		}

		public Object getValues() {
			return values.toArray(createEmptyArrayOfType());
		}

		protected void addValue(Object value) {
			values.add(value);
		}
	}

	class ListMultiValueHandler extends MultiValueHandler {

		private List<Object> values = new ArrayList<Object>();

		public ListMultiValueHandler(ContentHandler parentHandler,
				String elementHandled, String valueType) {
			super(parentHandler, elementHandled, valueType);
		}

		public Object getValues() {
			return values;
		}

		protected void addValue(Object value) {
			values.add(value);
		}
	}

	class SetMultiValueHandler extends MultiValueHandler {

		private Set<Object> values = new HashSet<Object>();

		public SetMultiValueHandler(ContentHandler parentHandler,
				String elementHandled, String valueType) {
			super(parentHandler, elementHandled, valueType);
		}

		public Object getValues() {
			return values;
		}

		protected void addValue(Object value) {
			values.add(value);
		}
	}

	class XMLValueHandler extends AbstractHandler {

		private Map<String, String> nsPrefixMap = new HashMap<String, String>();
		private StringBuffer buf;

		public XMLValueHandler(ContentHandler parentHandler) {
			super(parentHandler, ENDPOINT_PROPERTY_XML);
			buf = new StringBuffer();
		}

		public void startPrefixMapping(String prefix, String uri)
				throws SAXException {
			nsPrefixMap.put(uri, prefix);
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			buf.append("<").append(qName); //$NON-NLS-1$
			for (Iterator<String> i = nsPrefixMap.keySet().iterator(); i
					.hasNext();) {
				String nsURI = (String) i.next();
				String prefix = (String) nsPrefixMap.get(nsURI);
				i.remove();
				if (nsURI != null) {
					buf.append(" xmlns"); //$NON-NLS-1$
					if (prefix != null)
						buf.append(":").append(prefix); //$NON-NLS-1$
					buf.append("=\"").append(nsURI).append("\""); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			for (int i = 0; i < attributes.getLength(); i++) {
				buf.append(" "); //$NON-NLS-1$
				buf.append(attributes.getQName(i))
						.append("=\"").append(attributes.getValue(i)).append("\""); //$NON-NLS-1$ //$NON-NLS-2$
			}
			buf.append(">"); //$NON-NLS-1$
			characters = new StringBuffer();
		}

		public void startElement(String name, Attributes attributes)
				throws SAXException {
			// not used
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			if (elementHandled.equals(localName)) {
				super.endElement(namespaceURI, localName, qName);
			} else {
				if (characters != null)
					buf.append(characters);
				buf.append("</").append(qName).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
				characters = null;
			}
		}

		public String getXML() {
			return buf.toString();
		}
	}

	class EndpointPropertyHandler extends AbstractHandler {

		private Map<String, Object> properties;
		private String name;
		private String valueType = "String"; //$NON-NLS-1$
		private String value;
		private MultiValueHandler multiValueHandler;
		private XMLValueHandler xmlValueHandler;

		public EndpointPropertyHandler(ContentHandler parentHandler,
				Attributes attributes, Map<String, Object> properties)
				throws SAXException {
			super(parentHandler, ENDPOINT_PROPERTY);
			name = parseRequiredAttributes(attributes,
					new String[] { ENDPOINT_PROPERTY_NAME })[0];
			value = parseOptionalAttribute(attributes, ENDPOINT_PROPERTY_VALUE);
			String vt = parseOptionalAttribute(attributes,
					ENDPOINT_PROPERTY_VALUETYPE);
			if (vt != null) {
				if (!multiValueTypes.contains(vt))
					throw new SAXException("property element valueType=" + vt //$NON-NLS-1$
							+ " not allowed"); //$NON-NLS-1$
				this.valueType = vt;
			}
			this.properties = properties;
			if (value != null) {
				String[] property = new String[] { name, value };
				if (isValidProperty(property)) {
					this.properties.put(property[0], property[1]);
				}
			}
		}

		public void startElement(String name, Attributes attributes)
				throws SAXException {
			// Should not happen if value is non-null
			if (value != null)
				throw new SAXException(
						"property element has both value attribute and sub-element"); //$NON-NLS-1$
			if (ENDPOINT_PROPERTY_ARRAY.equals(name)) {
				if (multiValueHandler == null) {
					multiValueHandler = new ArrayMultiValueHandler(this,
							ENDPOINT_PROPERTY_ARRAY, valueType);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else if (ENDPOINT_PROPERTY_LIST.equals(name)) {
				if (multiValueHandler == null) {
					multiValueHandler = new ListMultiValueHandler(this,
							ENDPOINT_PROPERTY_LIST, valueType);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else if (ENDPOINT_PROPERTY_SET.equals(name)) {
				if (multiValueHandler == null) {
					multiValueHandler = new SetMultiValueHandler(this,
							ENDPOINT_PROPERTY_SET, valueType);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else if (ENDPOINT_PROPERTY_XML.equals(name)) {
				// xml
				if (xmlValueHandler == null) {
					xmlValueHandler = new XMLValueHandler(this);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else {
				invalidElement(name, attributes);
			}
		}

		public void endElement(String namespaceURI, String localName,
				String qName) {
			if (elementHandled.equals(localName)) {
				if (multiValueHandler != null) {
					properties.put(name, multiValueHandler.getValues());
					multiValueHandler = null;
				} else if (xmlValueHandler != null) {
					properties.put(name, xmlValueHandler.getXML());
					xmlValueHandler = null;
				}
				super.endElement(namespaceURI, localName, qName);
			}
		}

		private boolean isValidProperty(String[] property) {
			return (property.length == 2 && property[0] != null && property[1] != null);
		}
	}

	public class EndpointDescription {
		private Map<String, Object> properties;

		public EndpointDescription(Map<String, Object> properties) {
			this.properties = properties;
		}

		public Map<String, Object> getProperties() {
			return properties;
		}

		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("EndpointDescription [properties="); //$NON-NLS-1$
			builder.append(properties);
			builder.append("]"); //$NON-NLS-1$
			return builder.toString();
		}

	}

	public synchronized void parse(InputStream input) throws IOException {
		try {
			getParser();
			EndpointDescriptionsHandler endpointDescriptionsHandler = new EndpointDescriptionsHandler();
			xmlReader.setContentHandler(new EndpointDescriptionDocHandler(
					ENDPOINT_DESCRIPTIONS, endpointDescriptionsHandler));
			xmlReader.parse(new InputSource(input));
			endpointDescriptions = endpointDescriptionsHandler
					.getEndpointDescriptions();
		} catch (SAXException e) {
			throw new IOException(e.getMessage());
		} catch (ParserConfigurationException e) {
			throw new IOException(e.getMessage());
		} finally {
			input.close();
		}

	}

	public static String makeSimpleName(String localName, String qualifiedName) {
		if (localName != null && localName.length() > 0) {
			return localName;
		}
		int nameSpaceIndex = qualifiedName.indexOf(":"); //$NON-NLS-1$
		return (nameSpaceIndex == -1 ? qualifiedName : qualifiedName
				.substring(nameSpaceIndex + 1));
	}

	public void unexpectedElement(AbstractHandler handler, String element,
			Attributes attributes) {
	}

	public void unexpectedCharacterData(AbstractHandler handler, String cdata) {
	}

	public void unexpectedAttribute(String element, String attribute,
			String value) {
	}

	static int indexOf(String[] array, String value) {
		for (int i = 0; i < array.length; i += 1) {
			if (value == null ? array[i] == null : value.equals(array[i])) {
				return i;
			}
		}
		return -1;
	}

	public void checkRequiredAttribute(String element, String name, Object value) {
	}

	public void duplicateElement(AbstractHandler handler, String element,
			Attributes attributes) {
		// ignore the duplicate element entirely because we have already logged
		// it
		new IgnoringHandler(handler);
	}

	private List<EndpointDescription> endpointDescriptions;

	public List<EndpointDescription> getEndpointDescriptions() {
		return endpointDescriptions;
	}

}

Back to the top