1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved. 2 * 3 * This program and the accompanying materials are made available under 4 * the terms of the Common Public License v1.0 which accompanies this distribution, 5 * and is available at http://www.eclipse.org/legal/cpl-v10.html 6 * 7 * $Id: FilterCfg.java,v 1.2 2004/05/20 02:28:07 vlad_r Exp $ 8 */ 9package com.vladium.emma.instr; 10 11import java.io.File; 12import java.io.IOException; 13import java.util.ArrayList; 14import java.util.Iterator; 15import java.util.List; 16 17import org.apache.tools.ant.BuildException; 18import org.apache.tools.ant.Task; 19 20import com.vladium.util.IConstants; 21import com.vladium.util.Strings; 22import com.vladium.emma.ant.StringValue; 23import com.vladium.emma.ant.SuppressableTask; 24import com.vladium.emma.filter.IInclExclFilter; 25 26// ---------------------------------------------------------------------------- 27/** 28 * @author Vlad Roubtsov, (C) 2003 29 */ 30public 31class FilterCfg 32{ 33 // public: ................................................................ 34 35 36 public static final class filterElement extends StringValue 37 { 38 public filterElement (final Task task) 39 { 40 super (task); 41 } 42 43 public void setValue (final String value) 44 { 45 final String [] specs = Strings.merge (new String [] {value}, COMMA_DELIMITERS, true); 46 47 for (int i = 0; i < specs.length; ++ i) 48 { 49 final String spec = specs [i]; 50 51 if (spec.startsWith (IInclExclFilter.INCLUSION_PREFIX_STRING) || 52 spec.startsWith (IInclExclFilter.EXCLUSION_PREFIX_STRING)) 53 { 54 appendValue (spec, COMMA); 55 } 56 else 57 { 58 appendValue (IInclExclFilter.INCLUSION_PREFIX + spec, COMMA); // default to inclusion 59 } 60 } 61 } 62 63 /** 64 * Set the 'file' attribute. 65 */ 66 public void setFile (final File file) 67 { 68 appendValue ("@".concat (file.getAbsolutePath ()), COMMA); // actual file I/O delayed until getFilterSpecs() 69 } 70 71 public void setIncludes (final String value) 72 { 73 final String [] specs = Strings.merge (new String [] {value}, COMMA_DELIMITERS, true); 74 75 for (int i = 0; i < specs.length; ++ i) 76 { 77 final String spec = specs [i]; 78 79 if (spec.startsWith (IInclExclFilter.INCLUSION_PREFIX_STRING)) 80 { 81 appendValue (spec, COMMA); 82 } 83 else 84 { 85 if (spec.startsWith (IInclExclFilter.EXCLUSION_PREFIX_STRING)) 86 appendValue (IInclExclFilter.INCLUSION_PREFIX + spec.substring (1), COMMA); // override 87 else 88 appendValue (IInclExclFilter.INCLUSION_PREFIX + spec, COMMA); 89 } 90 } 91 } 92 93 public void setExcludes (final String value) 94 { 95 final String [] specs = Strings.merge (new String [] {value}, COMMA_DELIMITERS, true); 96 97 for (int i = 0; i < specs.length; ++ i) 98 { 99 final String spec = specs [i]; 100 101 if (spec.startsWith (IInclExclFilter.EXCLUSION_PREFIX_STRING)) 102 { 103 appendValue (spec, COMMA); 104 } 105 else 106 { 107 if (spec.startsWith (IInclExclFilter.INCLUSION_PREFIX_STRING)) 108 appendValue (IInclExclFilter.EXCLUSION_PREFIX + spec.substring (1), COMMA); // override 109 else 110 appendValue (IInclExclFilter.EXCLUSION_PREFIX + spec, COMMA); 111 } 112 } 113 } 114 115 } // end of nested class 116 117 118 public FilterCfg (final Task task) 119 { 120 if (task == null) throw new IllegalArgumentException ("null input: task"); 121 122 m_task = task; 123 m_filterList = new ArrayList (); 124 } 125 126 127 // filter attribute/element: 128 129 public void setFilter (final String filter) 130 { 131 createFilter ().appendValue (filter, COMMA); 132 } 133 134 public filterElement createFilter () 135 { 136 final filterElement result = new filterElement (m_task); 137 m_filterList.add (result); 138 139 return result; 140 } 141 142 // ACCESSORS: 143 144 public String [] getFilterSpecs () 145 { 146 if (m_specs != null) 147 return m_specs; 148 else 149 { 150 if ((m_filterList == null) || m_filterList.isEmpty ()) 151 { 152 m_specs = IConstants.EMPTY_STRING_ARRAY; 153 } 154 else 155 { 156 final String [] values = new String [m_filterList.size ()]; 157 158 int j = 0; 159 for (Iterator i = m_filterList.iterator (); i.hasNext (); ++ j) 160 values [j] = ((StringValue) i.next ()).getValue (); 161 162 try 163 { 164 m_specs = Strings.mergeAT (values, COMMA_DELIMITERS, true); 165 } 166 catch (IOException ioe) 167 { 168 throw (BuildException) SuppressableTask.newBuildException (m_task.getTaskName () 169 + ": I/O exception while processing input" , ioe, m_task.getLocation ()).fillInStackTrace (); 170 } 171 } 172 173 return m_specs; 174 } 175 } 176 177 // protected: ............................................................. 178 179 180 protected static final String COMMA = ","; 181 protected static final String COMMA_DELIMITERS = COMMA + Strings.WHITE_SPACE; 182 protected static final String PATH_DELIMITERS = COMMA.concat (File.pathSeparator); 183 184 // package: ............................................................... 185 186 // private: ............................................................... 187 188 189 private final Task m_task; // never null 190 private final List /* filterElement */ m_filterList; // never null 191 192 private transient String [] m_specs; 193 194} // end of class 195// ----------------------------------------------------------------------------