001: /**
002: * ToCSharp.java
003: *
004: Copyright (c) 2007, Innovatics Inc.
005:
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
009:
010: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
011: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
012:
013: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
017: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
018: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
019: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
020: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
021: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
022: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
023: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: */package util;
025:
026: import java.lang.*;
027: import java.io.*;
028: import java.util.*;
029:
030: /**
031: * ToCSharp.java
032: * Converts the Java source files to C# source files.
033: * The mappings are in java-to-c-sharp.code
034: */
035: public class ToCSharp {
036:
037: public ToCSharp(String fileName) {
038:
039: // Load the Java to C# dictionary
040: List<Pair> dict = new ArrayList<Pair>();
041: try {
042: BufferedReader in = new BufferedReader(new FileReader(
043: "java-to-c-sharp.code"));
044: String line = null;
045: while ((line = in.readLine()) != null) {
046: if (line.equals(""))
047: continue;
048: if (line.startsWith("#"))
049: continue;
050: Pair pair = new Pair();
051: pair.key = line.substring(0, line.indexOf('='));
052: pair.value = line.substring(line.indexOf('=') + 1);
053: dict.add(pair);
054: }
055: } catch (IOException ioe) {
056: System.err.println(ioe);
057: }
058:
059: StringBuffer data = new StringBuffer();
060: try {
061: BufferedReader in = new BufferedReader(new FileReader(
062: fileName));
063: int ch;
064: while ((ch = in.read()) != -1) {
065: data.append((char) ch);
066: }
067:
068: BufferedWriter out = null;
069: if (fileName.indexOf('/') != -1) {
070: out = new BufferedWriter(new FileWriter("net"
071: + fileName.substring(fileName.indexOf('/'),
072: fileName.lastIndexOf('.')) + ".cs"));
073: } else {
074: out = new BufferedWriter(new FileWriter(fileName
075: .substring(0, fileName.lastIndexOf('.'))
076: + ".cs"));
077: }
078:
079: for (int i = 0; i < dict.size(); i++) {
080: Pair pair = dict.get(i);
081: searchAndReplace(data, pair.key, pair.value);
082: }
083:
084: out.write(data.toString(), 0, data.length());
085: out.close();
086:
087: in.close();
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: // Searches the buffer for 'key' and replaces it with 'value'
094: private void searchAndReplace(StringBuffer buf, String key,
095: String value) {
096:
097: String str = buf.toString();
098: buf.setLength(0);
099:
100: int i = 0;
101: int j = 0;
102:
103: while (true) {
104: j = str.indexOf(key, i);
105: if (j == -1) {
106: buf.append(str.substring(i));
107: break;
108: }
109:
110: buf.append(str.substring(i, j));
111: buf.append(value);
112:
113: j += key.length();
114: i = j;
115: }
116: }
117:
118: public static void main(String[] args) {
119: if (args.length == 0) {
120: System.err.println("Usage:");
121: System.err.println(" java ToCSharp Test01.java");
122: System.err.println("or:");
123: System.err.println(" java ToCSharp com/pdfjet/");
124: System.err
125: .println("To convert all .java files in the com/pdfjet/ directory");
126: System.exit(1);
127: } else if (args.length == 1) {
128: if (args[0].indexOf('/') != -1) {
129: File file = new File(args[0]);
130: String[] fileNames = file
131: .list(new JavaFilenameFilter());
132: for (int i = 0; i < fileNames.length; i++) {
133: if (fileNames[i].startsWith("Courier")
134: || fileNames[i].startsWith("Helvetica")
135: || fileNames[i].startsWith("Times")
136: || fileNames[i].startsWith("Symbol")
137: || fileNames[i].startsWith("ZapfDingbats")) {
138: // skip it ...
139: } else {
140: new ToCSharp(args[0] + fileNames[i]);
141: }
142: }
143: } else {
144: new ToCSharp(args[0]);
145: }
146: }
147: System.exit(0);
148: }
149:
150: } // End of ToCSharp.java
151:
152: class JavaFilenameFilter implements FilenameFilter {
153:
154: public boolean accept(File dir, String name) {
155: if (name.endsWith(".java")) {
156: return true;
157: }
158: return false;
159: }
160:
161: } // End of ToCSharp.java
|