01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.util.parse.metapattern.parsers;
18:
19: import org.apache.wicket.util.parse.metapattern.Group;
20: import org.apache.wicket.util.parse.metapattern.MetaPattern;
21: import org.apache.wicket.util.parse.metapattern.OptionalMetaPattern;
22:
23: /**
24: * Parses XML tag names and attribute names which may include optional
25: * namespaces like "namespace:name" or "name". Both ":name" and "namespace:" are
26: * not allowed. Both, the namespace and the name have to follow naming rules for
27: * variable names (identifier).
28: *
29: * @author Jonathan Locke
30: * @author Juergen Donnerstag
31: */
32: public final class TagNameParser extends MetaPatternParser {
33: /** Namespaces must comply with variable name guidelines */
34: private static final Group namespaceGroup = new Group(
35: MetaPattern.VARIABLE_NAME);
36:
37: /** Tag names must comply with XML NCName guidelines */
38: private static final Group nameGroup = new Group(
39: MetaPattern.XML_ELEMENT_NAME);
40:
41: /** Pattern for tag names with optional namespace: (namespace:)?name */
42: private static final MetaPattern pattern = new MetaPattern(
43: new MetaPattern[] {
44: new OptionalMetaPattern(new MetaPattern[] {
45: namespaceGroup, MetaPattern.COLON }),
46: nameGroup });
47:
48: /**
49: * Constructs a tag name parser for a given input character sequence.
50: *
51: * @param input
52: * The input to parse
53: */
54: public TagNameParser(final CharSequence input) {
55: super (pattern, input);
56: }
57:
58: /**
59: * Get the namespace part (eg 'html' in 'html:form') converted to all lower
60: * case characters.
61: *
62: * @return the namespace part. Will be null, if optonal namespace was not
63: * found
64: */
65: public String getNamespace() {
66: final String namespace = namespaceGroup.get(matcher());
67: if (namespace != null) {
68: return namespace.toLowerCase();
69: }
70: return namespace;
71: }
72:
73: /**
74: * Gets the tag name part (eg 'form' in 'html:form' or 'form')
75: *
76: * @return the name part
77: */
78: public String getName() {
79: return nameGroup.get(matcher());
80: }
81: }
|