001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.parse.metapattern.parsers; 018 019import java.util.Locale; 020 021import org.apache.wicket.util.parse.metapattern.Group; 022import org.apache.wicket.util.parse.metapattern.MetaPattern; 023import org.apache.wicket.util.parse.metapattern.OptionalMetaPattern; 024 025/** 026 * Parses XML tag names and attribute names which may include optional namespaces like 027 * "namespace:name" or "name". Both ":name" and "namespace:" are not allowed. Both, the namespace 028 * and the name have to follow naming rules for variable names (identifier). 029 * 030 * @author Jonathan Locke 031 * @author Juergen Donnerstag 032 */ 033public final class TagNameParser extends MetaPatternParser 034{ 035 /** Namespaces must comply with variable name guidelines */ 036 private static final Group namespaceGroup = new Group(MetaPattern.VARIABLE_NAME); 037 038 /** Tag names must comply with XML NCName guidelines */ 039 private static final Group nameGroup = new Group(MetaPattern.XML_ELEMENT_NAME); 040 041 /** Pattern for tag names with optional namespace: (namespace:)?name */ 042 private static final MetaPattern pattern = new MetaPattern(new OptionalMetaPattern( 043 new MetaPattern[] { namespaceGroup, MetaPattern.COLON }), nameGroup); 044 045 /** 046 * Constructs a tag name parser for a given input character sequence. 047 * 048 * @param input 049 * The input to parse 050 */ 051 public TagNameParser(final CharSequence input) 052 { 053 super(pattern, input); 054 } 055 056 /** 057 * Get the namespace part (eg 'html' in 'html:form') converted to all lower case characters. 058 * 059 * @return the namespace part. Will be null, if optional namespace was not found 060 */ 061 public String getNamespace() 062 { 063 final String namespace = namespaceGroup.get(matcher()); 064 if (namespace != null) 065 { 066 return namespace.toLowerCase(Locale.ROOT); 067 } 068 return namespace; 069 } 070 071 /** 072 * Gets the tag name part (eg 'form' in 'html:form' or 'form') 073 * 074 * @return the name part 075 */ 076 public String getName() 077 { 078 return nameGroup.get(matcher()); 079 } 080}