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