01 /*
02 * CreoleXmlUpperCaseFilter.java
03 *
04 * Copyright (c) 1995-2010, The University of Sheffield. See the file
05 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06 *
07 * This file is part of GATE (see http://gate.ac.uk/), and is free
08 * software, licenced under the GNU Library General Public License,
09 * Version 2, June 1991 (in the distribution as file licence.html,
10 * and also available at http://gate.ac.uk/gate/licence.html).
11 *
12 * Hamish Cunningham, 1/Sept/2000
13 *
14 * $Id: CreoleXmlUpperCaseFilter.java 12006 2009-12-01 17:24:28Z thomas_heitz $
15 */
16
17 package gate.util;
18
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.Set;
22
23 import org.xml.sax.Attributes;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.XMLFilter;
26 import org.xml.sax.helpers.AttributesImpl;
27 import org.xml.sax.helpers.XMLFilterImpl;
28
29 /**
30 * SAX {@link XMLFilter} implementation used when reading a creole.xml
31 * file to ensure that all the standard creole elements and their
32 * attribute names are converted to upper case. All the creole.xml files
33 * built into GATE use upper case for their elements and attributes, but
34 * historically the files have been treated case-insensitively.
35 * Non-standard elements (which are added as features of the resource)
36 * are untouched.
37 */
38 public class CreoleXmlUpperCaseFilter extends XMLFilterImpl {
39 private Set<String> knownElements = new HashSet<String>(Arrays.asList(
40 "CREOLE-DIRECTORY", "CREOLE", "RESOURCE", "AUTOINSTANCE",
41 "HIDDEN-AUTOINSTANCE", "PARAM", "PARAMETER", "GUI", "OR", "NAME",
42 "JAR", "CLASS", "COMMENT", "INTERFACE", "ICON", "PRIVATE", "TOOL",
43 "MAIN_VIEWER", "RESOURCE_DISPLAYED", "ANNOTATION_TYPE_DISPLAYED",
44 "HELPURL"));
45
46 /**
47 * Process the end of an element. If the element is a standard
48 * creole.xml element then its name is converted to upper case,
49 * otherwise it is passed through untouched.
50 */
51 @Override
52 public void endElement(String uri, String localName, String name)
53 throws SAXException {
54 String upperCaseName = localName.toUpperCase();
55 if(knownElements.contains(upperCaseName)) {
56 super.endElement(uri, upperCaseName, name.substring(0,
57 name.indexOf(':') + 1)
58 + upperCaseName);
59 }
60 else {
61 super.endElement(uri, localName, name);
62 }
63 }
64
65 /**
66 * Process the start of an element. If the element is a standard
67 * creole.xml element then it and all its attributes have their names
68 * converted to upper case. Other elements are passed through
69 * untouched.
70 */
71 @Override
72 public void startElement(String uri, String localName, String name,
73 Attributes atts) throws SAXException {
74 String upperCaseName = localName.toUpperCase();
75 if(knownElements.contains(upperCaseName)) {
76 AttributesImpl newAtts = new AttributesImpl();
77 for(int i = 0; i < atts.getLength(); i++) {
78 String upperCaseAttrName = atts.getLocalName(i).toUpperCase();
79 String attrQName = atts.getQName(i);
80 newAtts.addAttribute(atts.getURI(i), upperCaseAttrName, attrQName
81 .substring(0, attrQName.indexOf(':') + 1)
82 + upperCaseAttrName, atts.getType(i), atts.getValue(i));
83 }
84 super.startElement(uri, upperCaseName, name.substring(0, name
85 .indexOf(':') + 1)
86 + upperCaseName, newAtts);
87 }
88 else {
89 super.startElement(uri, localName, name, atts);
90 }
91 }
92 }
|