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 org.apache.wicket.util.parse.metapattern.Group; 020import org.apache.wicket.util.parse.metapattern.MetaPattern; 021import org.apache.wicket.util.parse.metapattern.OptionalMetaPattern; 022 023/** 024 * Parses key value assignment statements like "foo=bar" but also supporting namespaces like 025 * "wicket:foo=bar". However the 'key' value returned will contain "wicket:foo". It does not 026 * separate namespace and name. 027 * 028 * @author Jonathan Locke 029 */ 030public final class VariableAssignmentParser extends MetaPatternParser 031{ 032 /** The optional namespace like "namespace:*[:*]" */ 033 private static final MetaPattern namespace = new OptionalMetaPattern(new MetaPattern[] { 034 MetaPattern.VARIABLE_NAME, MetaPattern.COLON, new OptionalMetaPattern(new MetaPattern[] {MetaPattern.VARIABLE_NAME, MetaPattern.COLON })}); 035 036 /** The key (lvalue) like "name" or "namespace:name" or "namespace:name:subname" */ 037 private final Group key = new Group(new MetaPattern(namespace, MetaPattern.XML_ATTRIBUTE_NAME)); 038 039 /** The rvalue of the assignment */ 040 private final Group value; 041 042 /** 043 * Construct a variable assignment parser against a given input character sequence 044 * 045 * @param input 046 * The input to parse 047 */ 048 public VariableAssignmentParser(final CharSequence input) 049 { 050 this(input, MetaPattern.STRING); 051 } 052 053 /** 054 * Construct a variable assignment parser against a given input character sequence 055 * 056 * @param input 057 * The input to parse 058 * @param valuePattern 059 * Value pattern 060 */ 061 public VariableAssignmentParser(final CharSequence input, final MetaPattern valuePattern) 062 { 063 super(input); 064 065 // Create group for value pattern 066 value = new Group(valuePattern); 067 068 // Pattern for =<value> 069 final MetaPattern variableAssignment = new MetaPattern(MetaPattern.OPTIONAL_WHITESPACE, 070 MetaPattern.EQUALS, MetaPattern.OPTIONAL_WHITESPACE, value); 071 072 // Set parse pattern to <key>=<value>? 073 setPattern(new MetaPattern(MetaPattern.OPTIONAL_WHITESPACE, key, new OptionalMetaPattern( 074 variableAssignment), MetaPattern.OPTIONAL_WHITESPACE)); 075 } 076 077 /** 078 * Gets the key part (eg 'foo' in 'foo=bar'). The key will include the optional namespace (eg 079 * 'html:foo' in 'html:foo=bar'). 080 * 081 * @return The key part 082 */ 083 public String getKey() 084 { 085 return key.get(matcher()); 086 } 087 088 /** 089 * Gets the value part (eg 'bar' in 'foo=bar'). 090 * 091 * @return The value part 092 */ 093 public String getValue() 094 { 095 return value.get(matcher()); 096 } 097}