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
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.wicket.util.parse.metapattern.Group;
024import org.apache.wicket.util.parse.metapattern.MetaPattern;
025
026
027/**
028 * Parses an arbitrary list format with a pattern for list entries and a pattern for list
029 * separators.
030 * 
031 * @author Jonathan Locke
032 */
033public class ListParser extends MetaPatternParser
034{
035        /** The pattern in between the separators */
036        private final Group entryGroup;
037
038        /** The separator */
039        private final MetaPattern separatorPattern;
040
041        /** The list elements parsed */
042        private final List<String> values = new ArrayList<>();
043
044        /**
045         * Constructs a list parser from an entry MetaPattern, a separator MetaPattern and an input
046         * character sequence.
047         * 
048         * @param entryPattern
049         *            The pattern in between the separators
050         * @param separatorPattern
051         *            The separator pattern
052         * @param input
053         *            The input to parse
054         */
055        public ListParser(final MetaPattern entryPattern, final MetaPattern separatorPattern,
056                final CharSequence input)
057        {
058                super(input);
059                entryGroup = new Group(entryPattern);
060                this.separatorPattern = separatorPattern;
061        }
062
063        /**
064         * Parse the input and add the elements to an internal list to be accessed by
065         * 
066         * @see #getValues()
067         * @see org.apache.wicket.util.parse.metapattern.parsers.MetaPatternParser#matches()
068         */
069        @Override
070        public final boolean matches()
071        {
072                // Are there any more elements
073                if (advance(entryGroup))
074                {
075                        // Add the first element
076                        final String value = entryGroup.get(matcher());
077                        values.add(value);
078
079                        // All remaining elements must be preceded by the separator pattern
080                        while (advance(separatorPattern) && advance(entryGroup))
081                        {
082                                // Add the value not including the separator
083                                values.add(entryGroup.get(matcher()));
084                        }
085
086                        // Yes, we found at least on element
087                        return true;
088                }
089
090                // Nothing found, not even one element without separator
091                return false;
092        }
093
094        /**
095         * Gets the parsed values. It depends on the elements pattern, whether empty elements, double or
096         * single quotes or escape characters are supported.
097         * 
098         * @return the parsed values
099         */
100        public final List<String> getValues()
101        {
102                return values;
103        }
104}