Java tutorial
/* Copyright 2010-2011 Zhengmao HU (James) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package net.sf.jabb.util.text; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import net.sf.jabb.util.col.MapValueFactory; import net.sf.jabb.util.col.PutIfAbsentMap; /** * Definition of how the matching will should be done. <br> * ?? * <p> * It consists of three parts: * <ul> * <li><code>regularExpression</code>: The regular expression used for matching.</li> * <li><code>exactMatchExamples</code>: A list of example strings that could and * only could match the regular expression. These example strings will * be used to exercise the matching engine, so they should cover all the * possibilities of matching.</li> * <li><code>exactMatchExample</code>: An example string that could and only could * match the regular expression. The example string will be used to exercise the * matching engine, so it should cover all the possibilities of matching.</li> * <li><code>attachment</code>: An attachment object that should be associated * with this matching. If matched, this object will be returned.</li> * </ul> * Only one of <code>exactMatchExamples</code> and <code>exactMatchExample</code> * need to be not null. * * <p> * ?? * <ul> * <li><code>regularExpression</code>: ??</li> * <li><code>exactMatchExamples</code>: ?? * ??????</li> * <li><code>exactMatchExample</code>: ?? * ??? ???</li> * <li><code>attachment</code>: ????? * ??</li> * </ul> * <code>exactMatchExamples</code><code>exactMatchExample</code>???null * * * @author Zhengmao HU (James) * */ public class MatchingDefinition { /** * The regular expression used for matching. * <br>?? */ private String regularExpression; /** * A list of example strings that could and only could match the regular expression.<br> * ?? * <p> * These example strings will be used to exercise the matching engine, so they should * cover all the possibilities of matching. * <p> * ?????? */ private List<String> exactMatchExamples; /** * An example string that could and only could match the regular expression.<br> * ?? * <p> * The example string will be used to exercise the matching engine, so it should * cover all the possibilities of matching. * <p> * ?????? */ private String exactMatchExample; /** * An attachment object that should be associated with this matching.<br> * ????? * <p> * If matched, this object will be returned. * <p> * ?? */ private Object attachment; public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); sb.append(regularExpression); sb.append(" -> "); sb.append(attachment); sb.append(" | "); if (exactMatchExample != null) { sb.append(exactMatchExample); } List<String> exampleList = exactMatchExamples; if (exampleList != null) { sb.append(exactMatchExamples); } sb.append("]\n"); return sb.toString(); } /** * Loads definitions from properties. For example: * <br>name1.expression=... * <br>name1.example.1=... * <br>name1.example.2=... * <br>... * <br>name1.attachment=... * <br> * <br>name2.expression=... * <br>name2.example.1=... * <br>name2.example.2=... * <br>... * <br>name2.attachment=... * <br>... * @param props * @return */ static public List<MatchingDefinition> load(Map<? extends Object, ? extends Object> props) { List<MatchingDefinition> result = new LinkedList<MatchingDefinition>(); Map<String, Map<String, Object>> defs = new PutIfAbsentMap<String, Map<String, Object>>( new HashMap<String, Map<String, Object>>(), new MapValueFactory<String, Map<String, Object>>() { @Override public Map<String, Object> createValue(String key) { return new HashMap<String, Object>(); } }); // classify for (Map.Entry<? extends Object, ? extends Object> entry : props.entrySet()) { String key = (String) entry.getKey(); String name = StringUtils.substringBefore(key, "."); String postfix = StringUtils.substringAfter(key, "."); defs.get(name).put(postfix, entry.getValue()); } // process for (Map.Entry<String, Map<String, Object>> entry : defs.entrySet()) { Map<String, Object> def = entry.getValue(); String expression = null; Object attachment = null; List<String> examples = new LinkedList<String>(); for (Map.Entry<String, Object> defEntry : def.entrySet()) { String key = defEntry.getKey(); Object value = defEntry.getValue(); if (key != null) { if (key.equalsIgnoreCase("expression")) { expression = (String) value; } else if (key.startsWith("example")) { examples.add((String) value); } else if (key.equalsIgnoreCase("attachment")) { attachment = value; } else { // ignore } } } if (expression != null) { MatchingDefinition mDef = new MatchingDefinition(); mDef.setRegularExpression(expression); mDef.setExactMatchExamples(examples); mDef.setAttachment(attachment); result.add(mDef); } } return result; } public String getRegularExpression() { return regularExpression; } public void setRegularExpression(String regularExpression) { this.regularExpression = regularExpression; } public List<String> getExactMatchExamples() { return exactMatchExamples; } public void setExactMatchExamples(List<String> exactExample) { this.exactMatchExamples = exactExample; } public String getExactMatchExample() { return exactMatchExample; } public void setExactMatchExample(String exactMatchExample) { this.exactMatchExample = exactMatchExample; } public Object getAttachment() { return attachment; } public void setAttachment(Object attachment) { this.attachment = attachment; } }