001: /**
002: * Translate.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: * Translate.java
032: * Translates the documentation created by JavaDoc to match the C# code.
033: */
034: public class Translate {
035:
036: public Translate(String readDir, String fileName, String writeDir) {
037:
038: // Load the Java to C# dictionary
039: List<Pair> dict = new ArrayList<Pair>();
040: try {
041: BufferedReader in = new BufferedReader(new FileReader(
042: "java-to-c-sharp.docs"));
043: String line = null;
044: while ((line = in.readLine()) != null) {
045: if (line.equals(""))
046: continue;
047: if (line.startsWith("#"))
048: continue;
049: Pair pair = new Pair();
050: pair.key = line.substring(0, line.indexOf('='));
051: pair.value = line.substring(line.indexOf('=') + 1);
052: dict.add(pair);
053: }
054: } catch (IOException ioe) {
055: System.err.println(ioe);
056: }
057:
058: StringBuffer data = new StringBuffer();
059: try {
060: BufferedReader in = new BufferedReader(new FileReader(
061: readDir + "/" + fileName));
062: int ch;
063: while ((ch = in.read()) != -1) {
064: data.append((char) ch);
065: }
066:
067: BufferedWriter out = new BufferedWriter(new FileWriter(
068: writeDir + "/" + fileName));
069:
070: for (int i = 0; i < dict.size(); i++) {
071: Pair pair = dict.get(i);
072: searchAndReplace(data, pair.key, pair.value);
073: }
074:
075: out.write(data.toString(), 0, data.length());
076: out.close();
077:
078: in.close();
079: } catch (Exception e) {
080: e.printStackTrace();
081: }
082: }
083:
084: // Searches the buffer for 'key' and replaces it with 'value'
085: private void searchAndReplace(StringBuffer buf, String key,
086: String value) {
087:
088: String str = buf.toString();
089: buf.setLength(0);
090:
091: int i = 0;
092: int j = 0;
093:
094: while (true) {
095: j = str.indexOf(key, i);
096: if (j == -1) {
097: buf.append(str.substring(i));
098: break;
099: }
100:
101: buf.append(str.substring(i, j));
102: buf.append(value);
103:
104: j += key.length();
105: i = j;
106: }
107: }
108:
109: public static void main(String[] args) {
110: String readDir = "docs/java/com/pdfjet";
111: String writeDir = "docs/c-sharp/net/pdfjet";
112: File file = new File(readDir);
113: String[] fileNames = file.list(new HtmlFilenameFilter());
114: for (int i = 0; i < fileNames.length; i++) {
115: new Translate(readDir, fileNames[i], writeDir);
116: }
117:
118: readDir = "docs/java";
119: writeDir = "docs/c-sharp";
120: file = new File(readDir);
121: fileNames = file.list(new HtmlFilenameFilter());
122: for (int i = 0; i < fileNames.length; i++) {
123: new Translate(readDir, fileNames[i], writeDir);
124: }
125: }
126:
127: } // End of Translate.java
128:
129: class HtmlFilenameFilter implements FilenameFilter {
130:
131: public boolean accept(File dir, String name) {
132: if (name.endsWith(".html")) {
133: return true;
134: }
135: return false;
136: }
137:
138: } // End of Translate.java
|