001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.tools.common;
051:
052: import java.io.*;
053: import java.net.*;
054: import java.util.*;
055: import java.util.regex.Pattern;
056: import java.util.regex.PatternSyntaxException;
057: import org.jaffa.tools.common.SourceDecomposer;
058: import org.jaffa.tools.common.SourceDecomposerException;
059: import org.jaffa.util.StringHelper;
060: import org.jaffa.util.URLHelper;
061:
062: /** Utilities class for the SourceDecomposer.
063: */
064: public class SourceDecomposerUtils {
065:
066: private static final String JAFFA_OVERWRITE = "//JAFFA-OVERWRITE";
067:
068: /** This method will list the customizations added to code generated files.
069: * @param dir the directory containing the the code generated files.
070: * @param fileFilter the files will be selected based on this filter. if no value is specified, then all files will be picked up.
071: * @param recursive if true, then the sub-folders will be checked too.
072: * @param writer the customizations will be written to this writer.
073: * @param customizationFilter the customizations will be filtered based on this paramter. If no value is passed, then all the customizations will be listed.
074: * @throws IOException if any IO error occurs.
075: * @throws SourceDecomposerException if the file is malformed or if it cannot be decomposed.
076: */
077: public static void listCustomizations(String dir,
078: String fileFilter, boolean recursive,
079: BufferedWriter writer, String customizationFilter)
080: throws IOException, SourceDecomposerException {
081: listCustomizations(new File(dir), fileFilter, recursive,
082: writer, customizationFilter);
083: }
084:
085: /** This method will list the customizations added to code generated files.
086: * @param dir the directory containing the the code generated files.
087: * @param fileFilter the files will be selected based on this filter. if no value is specified, then all files will be picked up.
088: * @param recursive if true, then the sub-folders will be checked too.
089: * @param writer the customizations will be written to this writer.
090: * @param customizationFilter the customizations will be filtered based on this paramter. If no value is passed, then all the customizations will be listed.
091: * @throws IOException if any IO error occurs.
092: * @throws SourceDecomposerException if the file is malformed or if it cannot be decomposed.
093: */
094: public static void listCustomizations(File dir,
095: final String fileFilter, boolean recursive,
096: BufferedWriter writer, String customizationFilter)
097: throws IOException, SourceDecomposerException {
098: if (!dir.exists() || !dir.isDirectory())
099: throw new IllegalArgumentException(
100: "Invalid directory passed: " + dir);
101:
102: File[] files = null;
103: if (fileFilter != null && fileFilter.length() > 0) {
104: // create an anonymous class that implements the FileFilter interface, to obtain the files from the directory
105: files = dir.listFiles(new FileFilter() {
106: public boolean accept(File pathname) {
107: if (pathname.isDirectory())
108: return true;
109: else {
110: try {
111: return Pattern.matches(fileFilter, pathname
112: .getPath());
113: } catch (PatternSyntaxException e) {
114: e.printStackTrace();
115: throw new IllegalArgumentException(
116: "Invalid fileFilter passed: "
117: + fileFilter);
118: }
119: }
120: }
121:
122: });
123: } else {
124: files = dir.listFiles();
125: }
126:
127: for (int i = 0; i < files.length; i++) {
128: File file = files[i];
129: if (file.isFile() && !file.isHidden())
130: listCustomizations(file, writer, customizationFilter);
131: else if (recursive && file.isDirectory()
132: && !file.isHidden())
133: listCustomizations(file, fileFilter, recursive, writer,
134: customizationFilter);
135: }
136: }
137:
138: /** This method will list the customizations added to a code generated file.
139: * @param file the code generated file.
140: * @param writer the customizations will be written to this writer.
141: * @param customizationFilter the customizations will be filtered based on this paramter. If no value is passed, then all the customizations will be listed.
142: * @throws IOException if any IO error occurs.
143: * @throws SourceDecomposerException if the file is malformed or if it cannot be decomposed.
144: */
145: public static void listCustomizations(File file,
146: BufferedWriter writer, String customizationFilter)
147: throws IOException, SourceDecomposerException {
148: System.out.println("Processing file... " + file);
149: SourceDecomposer sd = new SourceDecomposer(new BufferedReader(
150: new FileReader(file)));
151: Collection elements = sd.getCollection();
152: boolean headerWritten = false;
153: if (elements != null) {
154: for (Iterator itr = elements.iterator(); itr.hasNext();) {
155: Object obj = itr.next();
156: if (obj instanceof SourceDecomposer.GuardedBorder) {
157: SourceDecomposer.GuardedBorder gb = (SourceDecomposer.GuardedBorder) obj;
158:
159: // Check the name of the customization block against the filter
160: if (customizationFilter != null
161: && customizationFilter.length() > 0) {
162: try {
163: // Ignore this customization, if it does not match the filter
164: if (!Pattern.matches(customizationFilter,
165: gb.getKey()))
166: continue;
167: } catch (PatternSyntaxException e) {
168: e.printStackTrace();
169: throw new IllegalArgumentException(
170: "Invalid customizationFilter passed: "
171: + customizationFilter);
172: }
173: }
174:
175: // Strip the borders from the contents
176: StringHelper.Line line = null;
177: StringBuffer buf = new StringBuffer();
178: PushbackReader reader = new PushbackReader(
179: new StringReader(gb.getContents()));
180: while ((line = StringHelper.readLine(reader)) != null) {
181: if (line.getContents().indexOf(
182: SourceDecomposer.FIRST) < 0
183: && line.getContents().indexOf(
184: SourceDecomposer.LAST) < 0)
185: buf.append(line);
186: }
187: String contents = buf.toString().trim();
188:
189: // Only write if the contents are not empty
190: if (contents.length() > 0) {
191: if (!headerWritten) {
192: writer
193: .write("===============================================================================\n");
194: writer.write(file.getPath());
195: writer
196: .write("\n===============================================================================\n");
197: headerWritten = true;
198: }
199: writer.write("* " + gb.getKey() + '\n');
200: writer.write(contents);
201: writer.write("\n\n");
202: writer.flush();
203: }
204: }
205: }
206: }
207: }
208:
209: /** This method will check the code generated file for the '//JAFFA-OVERWRITE' marker.
210: * Returns a true if the marker is found.
211: * @param file the code generated file.
212: * @throws IOException if any IO error occurs.
213: * @return true if the '//JAFFA-OVERWRITE' marker is found in the input file.
214: */
215: public static boolean isJaffaOverwriteMarkerPresent(File file)
216: throws IOException {
217: BufferedReader reader = null;
218: try {
219: reader = new BufferedReader(new FileReader(file));
220: String line = null;
221: while ((line = reader.readLine()) != null) {
222: if (line.indexOf(JAFFA_OVERWRITE) >= 0)
223: return true;
224: }
225: return false;
226: } finally {
227: if (reader != null)
228: reader.close();
229: }
230: }
231:
232: /*
233: public static void main(String[] args) {
234: try {
235: // example usage of the 'listCustomizations' utility
236: String dir = "C:/Sandbox/MyProject/source/java";
237: String fileFilter = ".*finder.*java";
238: boolean recursive = true;
239: BufferedWriter writer = new BufferedWriter(new FileWriter("C:/listCustomizations.txt"));
240: String customizationFilter = ".*query.*";
241: listCustomizations(dir, fileFilter, recursive, writer, customizationFilter);
242: } catch (Throwable e) {
243: e.printStackTrace();
244: }
245: }
246: */
247: }
|