KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oroToApache


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
27  * must not be used to endorse or promote products derived from this
28  * software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
33  * name, without prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon software originally written
55  * by Daniel F. Savarese. We appreciate his contributions.
56  */

57
58 import java.io.*;
59 import org.apache.oro.text.regex.*;
60
61 /**
62  * This is a program you can use to convert older source code that uses
63  * the com.oroinc prefixes for the ORO text processing Java classes
64  * to org.apache. It assumes source files are small enough to store in
65  * memory and perform the substitutions. A small effort is made to not
66  * blindly substitute com.oroinc so that code using NetComponents or other
67  * ORO software will not have packages like com.oroinc.net become
68  * org.apache.net. However, you will still have to manually fix some
69  * code if you use the com.oroinc.io classes from NetComponents.
70
71  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
72  @version $Id: oroToApache.java,v 1.1.1.1 2000/07/23 23:08:54 jon Exp $
73  */

74 public final class oroToApache {
75   public static final String JavaDoc PACKAGE_PATTERN = "com\\.oroinc\\.(io|text|util)";
76   public static final String JavaDoc PACKAGE_SUBSTITUTION = "org.apache.oro.$1";
77   public static final String JavaDoc OLD_FILE_EXTENSION = "_old";
78
79   public static final class RenameException extends IOException {
80     public RenameException() { }
81
82     public RenameException(String JavaDoc message) {
83       super(message);
84     }
85   }
86
87   public static final class Converter {
88     Pattern _sourcePattern;
89     Perl5Matcher _matcher;
90     Perl5Substitution _substitution;
91
92     public static final int readFully(Reader reader, char[] buffer)
93       throws IOException
94     {
95       int offset, length, charsRead;
96
97       offset = 0;
98       length = buffer.length;
99
100       while(offset < buffer.length) {
101     charsRead = reader.read(buffer, offset, length);
102     if(charsRead == -1)
103       break;
104     offset+=charsRead;
105     length-=charsRead;
106       }
107
108       return offset;
109     }
110
111     public Converter(String JavaDoc patternString) throws MalformedPatternException {
112       Perl5Compiler compiler;
113
114       _matcher = new Perl5Matcher();
115       compiler = new Perl5Compiler();
116       _sourcePattern = compiler.compile(patternString);
117       _substitution = new Perl5Substitution(PACKAGE_SUBSTITUTION);
118     }
119
120     public void convertFile(String JavaDoc filename, String JavaDoc oldExtension)
121       throws FileNotFoundException, RenameException, SecurityException JavaDoc,
122          IOException
123     {
124       char[] inputBuffer;
125       int inputLength;
126       File srcFile, outputFile;
127       FileReader input;
128       FileWriter output;
129       String JavaDoc outputData;
130
131       srcFile = new File(filename);
132       input = new FileReader(srcFile);
133       outputFile =
134     File.createTempFile(srcFile.getName(), null,
135                 srcFile.getAbsoluteFile().getParentFile());
136       output = new FileWriter(outputFile);
137
138       inputBuffer = new char[(int)srcFile.length()];
139
140       inputLength = readFully(input, inputBuffer);
141       input.close();
142
143       // new String(inputBuffer) is terribly inefficient because the
144
// string ultimately gets converted back to a char[], but if we've
145
// got the memory it's expedient.
146
outputData =
147     Util.substitute(_matcher, _sourcePattern, _substitution,
148             new String JavaDoc(inputBuffer), Util.SUBSTITUTE_ALL);
149       output.write(outputData);
150       output.close();
151
152       if(!srcFile.renameTo(new File(srcFile.getAbsolutePath() +
153                     OLD_FILE_EXTENSION)))
154     throw new RenameException("Could not rename " + srcFile.getPath() +
155                   ".");
156
157       if(!outputFile.renameTo(srcFile))
158     throw new RenameException("Could not rename temporary output file. " +
159                   "Original file is in " +
160                   srcFile.getAbsolutePath() +
161                   OLD_FILE_EXTENSION);
162     }
163   }
164
165   public static final void main(String JavaDoc[] args) {
166     int file;
167     Converter converter;
168
169     if(args.length < 1) {
170       System.err.println("usage: oroToApache [file ...]");
171       return;
172     }
173
174     try {
175       converter = new Converter(PACKAGE_PATTERN);
176     } catch(MalformedPatternException mpe) {
177       // Shouldn''t happen
178
mpe.printStackTrace();
179       return;
180     }
181
182     for(file = 0; file < args.length; file++) {
183       try {
184     System.out.println("Converting " + args[file]);
185     converter.convertFile(args[file], OLD_FILE_EXTENSION);
186       } catch(FileNotFoundException fnfe) {
187     System.err.println("Error: Could not open file. Skipping " +
188                args[file]);
189       } catch(RenameException re) {
190     System.err.println("Error: " + re.getMessage());
191       } catch(SecurityException JavaDoc se) {
192     System.err.println("Error: Could not rename a file while processing" +
193                args[file] + ". Insufficient permission. " +
194                "File may not have been converted.");
195       } catch(IOException ioe) {
196     ioe.printStackTrace();
197     System.err.println("Error: I/O exception while converting " +
198                args[file] + ". File not converted.");
199       }
200     }
201   }
202 }
203
Popular Tags