001: /*
002: * $Id: VariableAssignmentParser.java,v 1.6 2005/01/15 19:24:03 jonathanlocke
003: * Exp $ $Revision: 461718 $ $Date: 2006-08-05 23:42:40 +0200 (Sat, 05 Aug 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.parse.metapattern.parsers;
019:
020: import wicket.util.parse.metapattern.Group;
021: import wicket.util.parse.metapattern.MetaPattern;
022: import wicket.util.parse.metapattern.OptionalMetaPattern;
023:
024: /**
025: * Parses key value assignment statements like "foo=bar" but also supporting
026: * namespaces like "wicket:foo=bar". However the 'key' value returned will
027: * contain "wicket:foo". It does not separate namespace and name.
028: *
029: * @author Jonathan Locke
030: */
031: public final class VariableAssignmentParser extends MetaPatternParser {
032: /** The optional namespace like "namespace:*" */
033: private static final MetaPattern namespace = new OptionalMetaPattern(
034: new MetaPattern[] { MetaPattern.VARIABLE_NAME,
035: MetaPattern.COLON });
036:
037: /** The key (lvalue) like "name" or "namespace:name" */
038: private final Group key = new Group(new MetaPattern(
039: new MetaPattern[] { namespace,
040: MetaPattern.XML_ATTRIBUTE_NAME }));
041:
042: /** The rvalue of the assignment */
043: private final Group value;
044:
045: /**
046: * Construct a variable assignment parser against a given input character
047: * sequence
048: *
049: * @param input
050: * The input to parse
051: * @param valuePattern
052: * Value pattern
053: */
054: public VariableAssignmentParser(final CharSequence input) {
055: this (input, MetaPattern.STRING);
056: }
057:
058: /**
059: * Construct a variable assignment parser against a given input character
060: * sequence
061: *
062: * @param input
063: * The input to parse
064: * @param valuePattern
065: * Value pattern
066: */
067: public VariableAssignmentParser(final CharSequence input,
068: final MetaPattern valuePattern) {
069: super (input);
070:
071: // Create group for value pattern
072: value = new Group(valuePattern);
073:
074: // Pattern for =<value>
075: final MetaPattern variableAssignment = new MetaPattern(
076: new MetaPattern[] { MetaPattern.OPTIONAL_WHITESPACE,
077: MetaPattern.EQUALS,
078: MetaPattern.OPTIONAL_WHITESPACE, value });
079:
080: // Set parse pattern to <key>=<value>?
081: setPattern(new MetaPattern(new MetaPattern[] {
082: MetaPattern.OPTIONAL_WHITESPACE, key,
083: new OptionalMetaPattern(variableAssignment),
084: MetaPattern.OPTIONAL_WHITESPACE }));
085: }
086:
087: /**
088: * Gets the key part (eg 'foo' in 'foo=bar'). The key will include the
089: * optional namespace (eg 'html:foo' in 'html:foo=bar').
090: *
091: * @return The key part
092: */
093: public String getKey() {
094: return key.get(matcher());
095: }
096:
097: /**
098: * Gets the value part (eg 'bar' in 'foo=bar').
099: *
100: * @return The value part
101: */
102: public String getValue() {
103: return value.get(matcher());
104: }
105: }
|