Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bb65bc61ea76906b99dd855e83cea43da6556797 (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) 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:
 *     Soyatec - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.xwt.internal.xml;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;

import org.eclipse.papyrus.xwt.IConstants;
import org.eclipse.papyrus.xwt.XWT;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.DefaultHandler;

/**
 * XAML-DOM handler.
 * 
 * @author yyang
 * @version 1.0
 */
class ElementHandler extends DefaultHandler implements ContentHandler {

	private Stack<StringBuffer> bufferStack = new Stack<StringBuffer>();

	private static final String[] BINDING_PROPERTIES = new String[]{ "path", "validationRule", "converter" };


	/**
	 * Temporary element hierarchy
	 */
	private Stack<DocumentObject> elementStack = new Stack<DocumentObject>();

	private Stack<Integer> elementIndexStack;

	private Stack<Boolean> switchStack;

	private StringBuffer textBuffer = null;

	private Locator locator;

	private boolean ignoreSystemProcession;

	private boolean hasSetDefaultEncoding;

	private ElementManager elementManager;

	private String defaultNamespace;

	private ExtensionParser extensionParser = new ExtensionParser();

	private HashMap<String, String> namespaceMapping = new HashMap<String, String>();

	private boolean needNormalizeName = true;

	/**
	 * Parse the XAML extension Markup: {StaticResource test} {StaticResource RessourceKey=test} {DynamicResource {x:Static
	 * SystemColors.ControlBrushKey}}
	 * 
	 * @author yyang
	 * 
	 */
	private class ExtensionParser {

		private Element root;

		private DocumentObject current;

		public void parse(DocumentObject element, String text) {
			if(root != null) {
				ExtensionParser parser = new ExtensionParser();
				parser.parse(element, text);
				return;
			}

			StringTokenizer stringTokenizer = new StringTokenizer(text, "{}", true);
			String previous = null;
			String nextPrevious = null;
			while(stringTokenizer.hasMoreTokens()) {
				String token = stringTokenizer.nextToken();

				if(previous != null) {
					if(previous.equals("{")) {
						if(token.equals("}")) {
							// escape sequence
							if(stringTokenizer.hasMoreTokens()) {
								handleBlock(stringTokenizer.nextToken(" \t\n\r\f"), false);
							}
						} else {
							startBlock();
						}
					} else if(previous.equals("}")) {
						endBlock();
					} else {
						StringBuilder builder = new StringBuilder();
						builder.append(previous);
						if(token.equals("{")) {
							int level = 1;
							builder.append(token);
							while(stringTokenizer.hasMoreTokens() && level >= 0) {
								String value = stringTokenizer.nextToken();
								if(value.equals("{")) {
									level++;
								} else if(value.equals("}")) {
									level--;
								}
								if(level >= 0) {
									builder.append(value);
								}
							}
						}
						handleBlock(builder.toString(), (nextPrevious == null || !nextPrevious.equals("}")));
					}
				}
				nextPrevious = previous;
				previous = token;
			}
			element.addChild(root);
			root = null;
			current = null;
		}

		protected void startBlock() {
		}

		protected void endBlock() {
			if(current != null) {
				current = current.getParent().getParent();
			}
		}

		protected Element createElement(String token) {
			int index = token.indexOf(':');
			String namespace = null;
			String name = token;
			if(index != -1) {
				String prefix = token.substring(0, index);
				name = token.substring(index + 1);
				namespace = namespaceMapping.get(prefix);
			}
			if(namespace == null) {
				namespace = defaultNamespace;
			}
			Element element = new Element(namespace, name, namespaceMapping);
			element.setId(ElementManager.generateID(name));
			if(current != null) {
				current.addChild(element);
			} else {
				if(root == null) {
					root = element;
				}
			}
			current = element;
			return element;
		}

		protected void handleBlock(String content, boolean newElement) {
			String rootPattern = " \t\n\r\f=,";
			StringTokenizer tokenizer = new StringTokenizer(content, rootPattern, true);
			String attributeName = null;
			String attributeValue = null;
			boolean equals = false;
			Element element = null;
			if(!newElement && current instanceof Element) {
				element = (Element)current;
			}

			boolean skip = false;
			String token = null;
			while(skip || tokenizer.hasMoreTokens()) {
				if(!skip) {
					token = tokenizer.nextToken(rootPattern).trim();
				}
				skip = false;
				if(token.length() == 0) {
					continue;
				}
				if(element == null) {
					element = createElement(token);
				} else {
					if(token.equals("=")) {
						equals = true;
						if("xpath".equalsIgnoreCase(attributeName)) {
							attributeValue = tokenizer.nextToken(",");
						}
						continue;
					}
					if(token.equals(",")) {
						if(attributeName != null) {
							if(attributeValue != null) {
								Attribute attribute = new Attribute(normalizeAttrNamespace(current.getNamespace()), attributeName, ElementManager.generateID(current.getName()));
								if(isExpendedProperty(attributeName) && "Binding".equalsIgnoreCase(element.getName())) {
									attributeValue = expandNamespaces(element, attributeValue);
								}
								handleContent(attribute, attributeValue);
								element.setAttribute(attribute);
								current = attribute;
							} else {
								element.setContent(attributeName);
							}
							attributeName = null;
							attributeValue = null;
							equals = false;
						}
					} else {
						if(attributeName == null) {
							attributeName = token;
						} else {
							StringBuilder builder = new StringBuilder();
							builder.append(token);
							if(token.startsWith("{")) {
								int level = 1;
								while(tokenizer.hasMoreTokens() && level > 0) {
									String value = tokenizer.nextToken("{}");
									if(value.equals("{")) {
										level++;
									} else if(value.equals("}")) {
										level--;
									}
									builder.append(value);
								}
							}
							attributeValue = builder.toString();

							try {
								token = tokenizer.nextToken(rootPattern).trim();
								skip = true;
								continue;
							} catch (NoSuchElementException e) {
							}
						}
					}
				}
				skip = false;
			}

			if(equals) {
				Attribute attribute = new Attribute(normalizeAttrNamespace(current.getNamespace()), attributeName, ElementManager.generateID(current.getName()));
				if(isExpendedProperty(attributeName) && "Binding".equalsIgnoreCase(element.getName())) {
					attributeValue = expandNamespaces(element, attributeValue);
				}

				if(attributeValue != null) {
					handleContent(attribute, attributeValue);
					element.setAttribute(attribute);
				} else {
					element.setAttribute(attribute);
					current = attribute;
				}
			} else if(attributeName != null) {
				int index = attributeName.indexOf(":");
				if(index != -1) {
					element = createElement(attributeName);
					current = current.getParent();
				} else {
					current.setContent(attributeName);
				}
			}
		}
	};

	static boolean isExpendedProperty(String name) {
		// TODO need a generic solution for each property of Binding 
		for(String propertyName : BINDING_PROPERTIES) {
			if(propertyName.equalsIgnoreCase(name)) {
				return true;
			}
		}
		return false;
	}

	protected String expandNamespaces(DocumentObject element, String value) {
		if(value.startsWith("{") || (value.indexOf(':') == -1)) {
			return value;
		}
		int length = IConstants.XAML_CLR_NAMESPACE_PROTO.length();
		for(String prefix : namespaceMapping.keySet()) {
			String namespace = namespaceMapping.get(prefix);
			if(namespace.startsWith(IConstants.XAML_CLR_NAMESPACE_PROTO)) {
				String packageName = namespace.substring(length);
				value = value.replace(prefix + ":", packageName + '.');
			}
		}
		return value;
	}

	/**
	 * Default constructor.
	 * 
	 * @param manager
	 *        loader instance.
	 */
	public ElementHandler(ElementManager elementManager) {
		this.ignoreSystemProcession = false;
		this.hasSetDefaultEncoding = false;
		this.elementManager = elementManager;
	}

	/**
	 * Internal constructor.
	 * 
	 * @param loader
	 *        loader instance.
	 * @param parent
	 *        the parent element of loading stream.
	 */
	public ElementHandler(DocumentObject parent, ElementManager elementManager) {
		if(parent != null) {
			elementStack.push(parent);
			bufferStack.push(textBuffer);
		}
		ignoreSystemProcession = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
	 */
	@Override
	public void setDocumentLocator(Locator locator) {
		this.locator = locator;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
		elementIndexStack = new Stack<Integer>();
		elementIndexStack.push(0);
		switchStack = new Stack<Boolean>();
		defaultNamespace = IConstants.XWT_NAMESPACE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String)
	 */
	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		if(prefix.length() == 0 && !ignoreSystemProcession) {
			defaultNamespace = uri;
		}
		namespaceMapping.put(prefix, uri);
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		namespaceMapping.remove(prefix);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
		elementStack.clear();
		bufferStack.clear();
		switchStack.clear();
		switchStack = null;
		defaultNamespace = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {

		if(!switchStack.isEmpty() && !switchStack.peek()) {
			return;
		}

		String string = new String(arg0, arg1, arg2);
		if(string.length() > 0) {
			if(textBuffer == null) {
				textBuffer = new StringBuffer(string);
			} else {
				textBuffer.append(string);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#skippedEntity(java.lang.String)
	 */
	@Override
	public void skippedEntity(String name) throws SAXException {
		super.skippedEntity(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {

		// Set xml's encoding to default.
		if(!ignoreSystemProcession && !hasSetDefaultEncoding) {
			if(locator instanceof Locator2) {
				Locator2 locator2 = (Locator2)locator;
				String encoding = locator2.getEncoding();
				if(encoding != null && encoding.length() > 0) {
					elementManager.setEncoding(encoding);
				}
			}
			hasSetDefaultEncoding = true;
		}

		uri = normalizeNamespace(uri);

		// If super condition does not allow parse children, ignore it.
		if(!switchStack.isEmpty() && !switchStack.peek()) {
			if(uri.equalsIgnoreCase(IConstants.XWT_NAMESPACE)) {
				if("Switch".equals(localName)) {
					// If children is switch element, than set false to it.
					switchStack.push(false);
				}
			}
			return;
		}

		if(localName.indexOf('.') > 0) {
			preProperty(uri, localName, attrs);
		} else {
			preElement(uri, localName, attrs);
		}

		elementIndexStack.push(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		elementIndexStack.pop();
		if(!elementIndexStack.isEmpty()) {
			int nextIndex = elementIndexStack.pop();
			elementIndexStack.push(nextIndex + 1);
		}

		uri = normalizeNamespace(uri);

		// If super condition does not allow parse children, ignore it.
		if(!switchStack.isEmpty() && !switchStack.peek()) {
			if(uri.equalsIgnoreCase(IConstants.XWT_NAMESPACE)) {
				if("Switch".equals(localName)) {
					switchStack.pop();
				}
			}
			return;
		}

		if(!elementStack.isEmpty()) {
			// remove current node from stack
			DocumentObject element = elementStack.peek();

			if(element instanceof Attribute) {
				postProperty(uri, localName);
			} else {
				postElement(uri, localName);
			}
		}
	}

	private void preElement(String namespace, String name, Attributes attrs) throws SAXException {

		// Process element ID

		namespace = normalizeNamespace(namespace);
		name = normalizeName(name);
		if(name.equalsIgnoreCase("xdata")) {
			needNormalizeName = false;
		}
		// Check the reference element.
		String id = ElementManager.generateID(name);

		// Check the same id elements.
		if(elementManager.hasElement(id)) {
			throw new SAXException(getFormattedMessage("Element already exists: ", id));
		}

		// Process attributes: original, external
		Set<Attribute> attributes = new HashSet<Attribute>();
		for(int i = 0, len = attrs.getLength(); i < len; i++) {
			String attrUri = normalizeAttrNamespace(attrs.getURI(i));
			String attrName = attrs.getLocalName(i);

			Attribute attribute;
			int index = attrName.lastIndexOf('.');
			if(index != -1) {
				String prefix = normalizeName(attrName.substring(0, index));
				String attributeName = (XWT.isXWTNamespace(attrUri) ? normalizeName(attrName.substring(index + 1)) : attrName);

				attribute = new Attribute(attrUri, attributeName, id);
				attribute.setNamePrefix(prefix);
			} else {
				attribute = new Attribute(attrUri, (XWT.isXWTNamespace(attrUri) ? normalizeName(attrName) : attrName), id);
			}
			handleContent(attribute, attrs.getValue(i));
			attributes.add(attribute);
		}

		// Create or reference element.
		Element element = new Element(namespace, name, getIndexPath(), attributes, namespaceMapping);
		element.setId(id);

		// Add current node to stack
		if(!elementStack.isEmpty()) {
			elementStack.peek().addChild(element);
		}

		elementStack.push(element);
		bufferStack.push(textBuffer);
		textBuffer = null;

		// Register to the element registry
		elementManager.preElement(element);
	}

	private void postElement(String namespace, String name) throws SAXException {

		// : The same as postReference : //

		if(!elementStack.isEmpty()) {
			// remove current node from stack
			DocumentObject element = elementStack.pop();

			// Setting element text content
			if(textBuffer != null) {
				String text = textBuffer.toString().trim();
				if(text.length() > 0) {
					handleContent(element, text);
				}
			}
			textBuffer = bufferStack.pop();

			// Post the element registration.
			elementManager.postElement((Element)element);
		}

		if(name.equalsIgnoreCase("xdata")) {
			needNormalizeName = true;
		}
	}

	protected void handleContent(DocumentObject element, String text) {
		if(text.startsWith("{") && text.endsWith("}")) {
			extensionParser.parse(element, text);
			return;
		} else {
			// handle the case: <x:Array Type="ns:Type" >
			if(IConstants.XAML_X_TYPE.equals(element.getName()) || IConstants.XAML_X_STATIC.equals(element.getName())) {
				int index = text.indexOf(':');
				if(index != -1) {
					String ns = text.substring(0, index);
					String content = text.substring(index + 1);
					String namespace = namespaceMapping.get(ns);
					if(namespace != null) {
						Element childElement = new Element(namespace, content, namespaceMapping);
						childElement.setId(ElementManager.generateID(element.getName()));
						element.addChild(childElement);
						return;
					}
				}
			}
		}
		if(element instanceof Attribute && IConstants.XWT_X_NAMESPACE.equals(element.getNamespace())) {
			if(IConstants.XAML_STYLE.equalsIgnoreCase(element.getName()) || IConstants.XAML_X_CLASS.equalsIgnoreCase(element.getName()) || IConstants.XAML_X_CLASS_FACTORY.equalsIgnoreCase(element.getName())) {
				// handle the expansion of x:Style = "(j:class).variable"
				// and handle the expansion of x:Class = "(j:class).variable"
				text = expandNamespaces(element, text);
			}
		}
		element.setContent(text);
	}

	/**
	 * It is compatible with XAML.
	 * <p>
	 * It must support the following features:
	 * 
	 * 1. <Panel> <Button> <Button.Content>Hello World</Button.Content> </Button> </Panel>
	 * 
	 * 2. <Panel> <Panel.Composite> <Button Content="Hello World" /> </Panel.Composite> </Panel>
	 * 
	 * 3. <Panel> <Panel.Composite> <Button> <Button.Content>Hello World</Button.Content> </Button> </Panel.Composite> </Panel>
	 * 
	 * 4. <Panel> <Panel.Composite> <Button> <Button.Content>Hello</Button.Content> </Button> <Button> <Button.Content>World</Button.Content>
	 * </Button> </Panel.Composite> </Panel>
	 * </p>
	 * 
	 * @param namespace
	 *        attribute namespace
	 * @param name
	 *        attribute full name. like: Button.Content
	 */
	private void preProperty(String namespace, String name, Attributes attrs) throws SAXException {
		String elementTag = normalizeName(name.substring(0, name.indexOf('.')));
		String attributeName = (XWT.isXWTNamespace(namespace) ? normalizeName(name.substring(name.indexOf('.') + 1)) : name);
		String elementId = null;

		// Search the owner element and get the ID.
		for(int i = elementStack.size() - 1; i >= 0; i--) {
			DocumentObject element = elementStack.get(i);
			if(element instanceof Element && element.getName().equals(elementTag)) {
				elementId = element.getId();
				break;
			}
		}

		if(elementId == null) {
			preElement(namespace, name, attrs);
		} else {
			namespace = normalizeNamespace(namespace);
			// Process attributes: original, external
			Collection<Attribute> collection = new ArrayList<Attribute>();
			for(int i = 0, len = attrs.getLength(); i < len; i++) {

				String attrUri = normalizeNamespace(attrs.getURI(i));
				String attrName = attrs.getLocalName(i);

				Attribute attr;
				int index = attrName.lastIndexOf('.');
				if(index != -1) {
					String prefix = normalizeName(attrName.substring(0, index));
					String aName = (XWT.isXWTNamespace(namespace) ? normalizeName(attrName.substring(index + 1)) : attrName);
					attr = new Attribute(attrUri, aName, elementId);
					attr.setNamePrefix(prefix);
				} else {
					attr = new Attribute(attrUri, (XWT.isXWTNamespace(namespace) ? normalizeName(attrName) : attrName), elementId);
				}
				handleContent(attr, attrs.getValue(i));
				collection.add(attr);
			}

			Attribute attribute = new Attribute(normalizeAttrNamespace(namespace), attributeName, getIndexPath(), elementId, collection);
			elementStack.push(attribute);
			bufferStack.push(textBuffer);
			textBuffer = null;
		}
	}

	/**
	 * @see #preProperty(String, String)
	 */
	private void postProperty(String namespace, String name) throws SAXException {
		if(!elementStack.isEmpty()) {
			// remove current node from stack
			Attribute attribute = (Attribute)elementStack.pop();

			// Setting attribute content
			if(textBuffer != null) {
				String text = textBuffer.toString().trim();
				if(text.length() > 0) {
					handleContent(attribute, text);
				}
			}
			textBuffer = bufferStack.pop();

			Element element = elementManager.getElement(attribute.getId());
			element.setAttribute(attribute);
		}
	}

	/**
	 * Normalize the namespace string. If uri is null or length is zero, returns default namespace.
	 * 
	 * @param uri
	 *        The namespace which to normalize.
	 * @return Returns the normalized namespace.
	 */
	private String normalizeNamespace(String uri) {
		if(uri == null || uri.length() == 0) {
			uri = defaultNamespace;
		}
		if(uri.startsWith(IConstants.XAML_CLR_NAMESPACE_PROTO)) {
			return uri;
		}
		return uri.toLowerCase();
	}

	/**
	 * Normalize the namespace string. If uri is null or length is zero, returns default namespace.
	 * 
	 * @param uri
	 *        The namespace which to normalize.
	 * @return Returns the normalized namespace.
	 */
	private String normalizeAttrNamespace(String uri) {
		if(uri == null || uri.length() == 0) {
			uri = defaultNamespace;
		}
		if(uri.startsWith(IConstants.XAML_CLR_NAMESPACE_PROTO)) {
			uri = defaultNamespace;
		}
		return uri.toLowerCase();
	}

	/**
	 * <p>
	 * Normalize the tag name to class-formal format.
	 * </p>
	 * <p>
	 * e.g. Normalize &quot;font-face-name&quot; to &quot;FontFaceName&quot;
	 * </p>
	 * 
	 * @param tagName
	 *        The tag name which to normalize.
	 * @return the normalized tag name.
	 */
	private String normalizeName(String tagName) {
		if(!needNormalizeName) {
			return tagName;
		}
		StringBuffer buffer = new StringBuffer();

		boolean isH = false;
		for(int i = 0, len = tagName.length(); i < len; i++) {
			char c = tagName.charAt(i);
			if(i == 0) {
				buffer.append(Character.toUpperCase(c));
			} else {
				switch(c) {
				case '-':
					isH = true;
					break;
				case '.':
					isH = true;
					buffer.append(c);
					break;
				default:
					if(isH) {
						buffer.append(Character.toUpperCase(c));
						isH = false;
					} else {
						buffer.append(c);
					}
					break;
				}
			}
		}
		return buffer.toString();
	}

	private String getFormattedMessage(String msg, Object... args) {
		StringBuffer buffer = new StringBuffer("[" + locator.getLineNumber() + "," + locator.getColumnNumber() + "] ");
		buffer.append(msg);
		return MessageFormat.format(buffer.toString(), args);
	}

	/**
	 * @return the '/' separated, zero based, path of current {@link Element}.
	 */
	private String getIndexPath() {
		StringBuilder sb = new StringBuilder();
		for(Integer index : elementIndexStack) {
			if(sb.length() != 0) {
				sb.append('/');
			}
			sb.append(index.intValue());
		}
		return sb.toString();
	}
}

Back to the top