View Javadoc
1 // $Id: Region.java,v 1.3 2004/02/07 11:24:11 powerpete Exp $ 2 // [JMP, 01.02.2004] Created this file. 3 package org.jface.stepmt.core; 4 5 import java.util.ArrayList; 6 import java.util.Collection; 7 import java.util.Iterator; 8 9 import org.apache.commons.lang.StringUtils; 10 import org.jface.stepmt.core.util.PatternSet; 11 import org.jface.stepmt.core.util.TextRegion; 12 13 /*** 14 * Used to define a {@link Region} of a document template. 15 * 16 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a> 17 * @version $Id: Region.java,v 1.3 2004/02/07 11:24:11 powerpete Exp $ 18 */ 19 public class Region 20 { 21 private String name; 22 private final Collection regions = new ArrayList(); 23 private final Collection separators = new ArrayList(); 24 private final Collection identificators = new ArrayList(); 25 private int position = -1; 26 27 /*** 28 * Adds a new {@link Region} to this region as sub-region. 29 * 30 * @param region The {@link Region}. 31 */ 32 public void addRegion(Region region) 33 { 34 regions.add(region); 35 } 36 37 /*** 38 * Adds a new identificator pattern. 39 * 40 * @param identificatorStr The string containing the identificator patterns. 41 */ 42 public void addIdentificator(String identificatorStr) 43 { 44 identificators.add(new PatternSet(identificatorStr)); 45 } 46 47 /*** 48 * Adds a new separator pattern. 49 * 50 * @param separatorStr The string containing the separator patterns. 51 */ 52 public void addSeparator(String separatorStr) 53 { 54 separators.add(new PatternSet(separatorStr)); 55 } 56 57 /*** 58 * Sets the position of this {@link Region}. Must be parseable to 59 * <tt>int</tt>. 60 * 61 * @param position The position. 62 */ 63 public void setPosition(String position) 64 { 65 this.position = Integer.parseInt(position); 66 } 67 68 final String getName() 69 { 70 return name; 71 } 72 73 /*** 74 * Sets the name of this {@link Region}. 75 * 76 * @param name The name. 77 */ 78 public void setName(String name) 79 { 80 this.name = name; 81 } 82 83 TextRegion findBestTextRegion(final String text, final int position) 84 { 85 if (StringUtils.isBlank(text)) 86 { 87 return null; 88 } 89 TextRegion bestTextRegion = null; 90 for (Iterator it = regions.iterator(); it.hasNext();) 91 { 92 final Region region = (Region) it.next(); 93 final TextRegion textRegion = 94 region.createTextRegion(text, position); 95 if (textRegion != null && textRegion.isBetterThan(bestTextRegion)) 96 { 97 bestTextRegion = textRegion; 98 } 99 } 100 return bestTextRegion; 101 } 102 103 private final TextRegion createTextRegion(String text, final int position) 104 { 105 boolean matchesPosition = false; 106 if (this.position != -1) 107 { 108 if (this.position != position) 109 { 110 return null; 111 } 112 matchesPosition = true; 113 } 114 boolean matchesIdentificator = false; 115 if (!identificators.isEmpty()) 116 { 117 Interval interval = null; 118 for (Iterator it = identificators.iterator(); it.hasNext();) 119 { 120 final PatternSet identificator = (PatternSet) it.next(); 121 interval = identificator.matchesAtStart(text); 122 if (interval != null) 123 { 124 break; 125 } 126 } 127 if (interval == null) 128 { 129 return null; 130 } 131 text = interval.textAfter(text); 132 matchesIdentificator = true; 133 } 134 Interval result = new Interval(text.length(), 0); 135 for (Iterator it = separators.iterator(); it.hasNext();) 136 { 137 final PatternSet separator = (PatternSet) it.next(); 138 final Interval interval = separator.match(text); 139 if (interval != null && interval.isBefore(result)) 140 { 141 result = interval; 142 } 143 } 144 return new TextRegion( 145 this, 146 text, 147 result, 148 matchesIdentificator, 149 matchesPosition); 150 } 151 152 /*** 153 * Resolves all patterns with the given {@link PatternMap}. Also resolves 154 * the patterns of the sub-regions. 155 * 156 * @param patternMap The {@link PatternMap} containing {@link Pattern}s 157 * to resolve. 158 */ 159 public void resolvePatterns(final PatternMap patternMap) 160 { 161 resolvePatterns(identificators, patternMap); 162 resolvePatterns(separators, patternMap); 163 for (Iterator it = regions.iterator(); it.hasNext();) 164 { 165 final Region region = (Region) it.next(); 166 region.resolvePatterns(patternMap); 167 } 168 } 169 170 private static final void resolvePatterns(Collection col, PatternMap map) 171 { 172 for (Iterator it = col.iterator(); it.hasNext();) 173 { 174 ((PatternSet) it.next()).resolve(map); 175 } 176 } 177 }

This page was automatically generated by Maven