Source Code Cross Referenced for Builder.java in  » Source-Control » sourcejammer » JLibDiff » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Source Control » sourcejammer » JLibDiff 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright (C) 2001, 2002 Robert MacGrogan
003:         *
004:         *  This library is free software; you can redistribute it and/or
005:         *  modify it under the terms of the GNU Lesser General Public
006:         *  License as published by the Free Software Foundation; either
007:         *  version 2.1 of the License, or (at your option) any later version.
008:         *
009:         *  This library is distributed in the hope that it will be useful,
010:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012:         *  Lesser General Public License for more details.
013:         *
014:         *  You should have received a copy of the GNU Lesser General Public
015:         *  License along with this library; if not, write to the Free Software
016:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:         *
018:         *
019:         * $Archive: SourceJammer$
020:         * $FileName: Builder.java$
021:         * $FileID: 4290$
022:         *
023:         * Last change:
024:         * $AuthorName: Rob MacGrogan$
025:         * $Date: 4/23/03 5:23 PM$
026:         * $Comment: Replaced GPL header with LGPL header.$
027:         */
028:        package JLibDiff;
029:
030:        import java.io.BufferedReader;
031:        import java.io.File;
032:        import java.io.FileInputStream;
033:        import java.io.FileOutputStream;
034:        import java.io.IOException;
035:        import java.io.InputStreamReader;
036:        import java.io.PrintStream;
037:        import java.io.Reader;
038:        import java.io.StringReader;
039:        import java.util.Enumeration;
040:        import java.util.StringTokenizer;
041:
042:        import org.sourcejammer.util.BufferedLineReader;
043:        import org.sourcejammer.util.FileUtil;
044:        import org.sourcejammer.util.StringUtil;
045:
046:        /**
047:         * Title: $FileName: Builder.java$
048:         * @author $AuthorName: Rob MacGrogan$
049:         * @version $VerNum: 2$
050:         *
051:         * $KeyWordsOff: $<br/><br/>
052:         * 
053:         * Static methods for building diffs from text and vice-versa.
054:         */
055:        public class Builder {
056:
057:            public static String buildHunkString(HunkAdd h) {
058:                StringBuffer str = new StringBuffer();
059:                str.append(h.ld1).append("a").append(h.ld2);
060:
061:                if (h.ld2 != h.lf2) {
062:                    str.append(",").append(h.lf2);
063:                }
064:                str.append("\n");
065:                Enumeration enm = h.b.elements();
066:                while (enm.hasMoreElements()) {
067:                    str.append("> " + (String) enm.nextElement()).append("\n");
068:                }
069:                return str.toString();
070:            }
071:
072:            public static String buildHunkString(HunkDel h) {
073:                StringBuffer str = new StringBuffer();
074:                str.append(Integer.toString(h.ld1));
075:                if (h.ld1 != h.lf1) {
076:                    str.append(",").append(h.lf1);
077:                }
078:                str.append("d").append(h.ld2).append("\n");
079:
080:                Enumeration enm = h.a.elements();
081:                while (enm.hasMoreElements()) {
082:                    str.append("< ").append((String) enm.nextElement()).append(
083:                            "\n");
084:                }
085:                return str.toString();
086:            }
087:
088:            public static String buildHunkString(HunkChange h) {
089:                StringBuffer str = new StringBuffer();
090:                str.append(Integer.toString(h.ld1));
091:                if (h.ld1 != h.lf1) {
092:                    str.append(",").append(h.lf1);
093:                }
094:                str.append("c").append(h.ld2);
095:                if (h.ld2 != h.lf2) {
096:                    str.append(",").append(h.lf2);
097:                }
098:                str.append("\n");
099:                Enumeration enm = h.a.elements();
100:                while (enm.hasMoreElements()) {
101:                    str.append("< ").append((String) enm.nextElement()).append(
102:                            "\n");
103:                }
104:                str.append("---\n");
105:
106:                enm = h.b.elements();
107:                while (enm.hasMoreElements()) {
108:                    str.append("> ").append((String) enm.nextElement()).append(
109:                            "\n");
110:                }
111:                return str.toString();
112:            }
113:
114:            public static diff buildDiff(File diffFile)
115:                    throws DiffFormatException, IOException {
116:                diff d = null;
117:                FileInputStream stmFile = new FileInputStream(diffFile);
118:                try {
119:                    InputStreamReader oInStreamRead = new InputStreamReader(
120:                            stmFile);
121:                    try {
122:                        BufferedReader oBuffRead = new BufferedReader(
123:                                oInStreamRead);
124:                        try {
125:                            d = buildDiff(oBuffRead);
126:                        } finally {
127:                            oBuffRead.close();
128:                        }
129:                    } finally {
130:                        oInStreamRead.close();
131:                    }
132:                } finally {
133:                    stmFile.close();
134:                }
135:                return d;
136:            }
137:
138:            public static diff buildDiff(String s) throws DiffFormatException {
139:                StringReader reader = new StringReader(s);
140:                return buildDiff(reader);
141:            }
142:
143:            public static diff buildDiff(Reader reader)
144:                    throws DiffFormatException {
145:                diff d = new diff();
146:
147:                BufferedLineReader lnReader = new BufferedLineReader(reader);
148:                try {
149:                    String firstLine = null;
150:                    if ((firstLine = lnReader.readLine()) != null) {
151:                        //String firstLine = lnReader.readLine();
152:                        BuildHunkResult result = null;
153:                        do {
154:                            if (firstLine.indexOf("a") > -1) {
155:                                result = buildHunkAdd(firstLine, lnReader);
156:                            } else if (firstLine.indexOf("d") > -1) {
157:                                result = buildHunkDel(firstLine, lnReader);
158:                            } else if (firstLine.indexOf("c") > -1) {
159:                                result = buildHunkChange(firstLine, lnReader);
160:                            } else {
161:                                throw new DiffFormatException(
162:                                        "Diff string is improperly formatted.");
163:                            }
164:
165:                            firstLine = result.nextLine;
166:                            d.v.add(result.hunk);
167:                        } while (firstLine != null);
168:                    }
169:                } catch (IOException ex) {
170:                    throw new DiffFormatException("Unexpected end of file.", ex);
171:                }
172:                return d;
173:            }
174:
175:            private static BuildHunkResult buildHunkAdd(String firstLine,
176:                    BufferedLineReader reader) throws DiffFormatException {
177:                BuildHunkResult wrapper = new BuildHunkResult();
178:                HunkAdd result = new HunkAdd();
179:
180:                //Find location of a.
181:                int aIndex = firstLine.indexOf("a", 0);
182:                String sLd1 = firstLine.substring(0, aIndex);
183:                result.ld1 = intValue(sLd1);
184:
185:                //Do we have a comma?
186:                int commaIndex = firstLine.indexOf(",", aIndex);
187:                int endLd2Index = firstLine.length();
188:                if (commaIndex > -1) {
189:                    endLd2Index = commaIndex;
190:                    String sLf2 = firstLine.substring(commaIndex + 1, firstLine
191:                            .length());
192:                    result.lf2 = intValue(sLf2);
193:                }
194:                String sLd2 = firstLine.substring(aIndex + 1, endLd2Index);
195:                result.ld2 = intValue(sLd2);
196:
197:                if (commaIndex == -1) {
198:                    result.lf2 = result.ld2;
199:                }
200:
201:                try {
202:                    //Remaining lines go into a Vector.
203:                    String line = null;
204:                    while ((line = reader.readLine()) != null) {
205:                        //String line = reader.readLine();
206:                        if (line.startsWith(">")) {
207:                            //Must remove 1st 2 chars.
208:                            line = line.substring(2);
209:                            result.b.add(line);
210:                        } else {
211:                            wrapper.nextLine = line;
212:                            break;
213:                        }
214:                    }
215:                } catch (IOException ex) {
216:                    throw new DiffFormatException("Unexpected end of file.", ex);
217:                }
218:
219:                wrapper.hunk = result;
220:
221:                return wrapper;
222:            }
223:
224:            private static BuildHunkResult buildHunkDel(String firstLine,
225:                    BufferedLineReader reader) throws DiffFormatException {
226:                BuildHunkResult wrapper = new BuildHunkResult();
227:                HunkDel result = new HunkDel();
228:
229:                //Find location of d.
230:                int dIndex = firstLine.indexOf("d", 0);
231:                String sL1 = firstLine.substring(0, dIndex);
232:
233:                //Is there a comma in sL1?
234:                int commaIndex = sL1.indexOf(",");
235:                int endLd1Index = sL1.length();
236:                if (commaIndex > -1) {
237:                    endLd1Index = commaIndex;
238:                    String sLf1 = sL1.substring(commaIndex + 1);
239:                    result.lf1 = intValue(sLf1);
240:                }
241:                String sLd1 = sL1.substring(0, endLd1Index);
242:                result.ld1 = intValue(sLd1);
243:
244:                if (commaIndex == -1) {
245:                    result.lf1 = result.ld1;
246:                }
247:
248:                String sLd2 = firstLine.substring(dIndex + 1);
249:                result.ld2 = intValue(sLd2);
250:
251:                try {
252:                    //Remaining lines go into a Vector.
253:                    String line = null;
254:                    while ((line = reader.readLine()) != null) {
255:                        //String line = reader.readLine();
256:                        if (line.startsWith("<")) {
257:                            //Must remove 1st 2 chars.
258:                            line = line.substring(2);
259:                            result.a.add(line);
260:                        } else {
261:                            wrapper.nextLine = line;
262:                            break;
263:                        }
264:                    }
265:                } catch (IOException ex) {
266:                    throw new DiffFormatException("Unexpected end of file.", ex);
267:                }
268:
269:                wrapper.hunk = result;
270:                return wrapper;
271:            }
272:
273:            private static BuildHunkResult buildHunkChange(String firstLine,
274:                    BufferedLineReader reader) throws DiffFormatException {
275:                BuildHunkResult wrapper = new BuildHunkResult();
276:                HunkChange result = new HunkChange();
277:
278:                //Find location of c.
279:                int cIndex = firstLine.indexOf("c", 0);
280:                String sL1 = firstLine.substring(0, cIndex);
281:
282:                //Is there a comma in sL1?
283:                int commaIndex = sL1.indexOf(",");
284:                int endLd1Index = sL1.length();
285:                if (commaIndex > -1) {
286:                    endLd1Index = commaIndex;
287:                    String sLf1 = sL1.substring(commaIndex + 1);
288:                    result.lf1 = intValue(sLf1);
289:                }
290:                String sLd1 = sL1.substring(0, endLd1Index);
291:                result.ld1 = intValue(sLd1);
292:
293:                if (commaIndex == -1) {
294:                    result.lf1 = result.ld1;
295:                }
296:
297:                //Do we have another comma?
298:                commaIndex = firstLine.indexOf(",", cIndex);
299:                int endLd2Index = firstLine.length();
300:                if (commaIndex > -1) {
301:                    endLd2Index = commaIndex;
302:                    String sLf2 = firstLine.substring(commaIndex + 1, firstLine
303:                            .length());
304:                    result.lf2 = intValue(sLf2);
305:                }
306:                String sLd2 = firstLine.substring(cIndex + 1, endLd2Index);
307:                result.ld2 = intValue(sLd2);
308:
309:                if (commaIndex == -1) {
310:                    result.lf2 = result.ld2;
311:                }
312:
313:                try {
314:                    //Put del lines into vector
315:                    String line = null;
316:                    while ((line = reader.readLine()) != null) {
317:                        //String line = reader.readLine();
318:                        if (line.startsWith("<")) {
319:                            //Must remove 1st 2 chars.
320:                            line = line.substring(2);
321:                            result.a.add(line);
322:                        } else {
323:                            break;
324:                        }
325:                    }
326:
327:                    //Put add lines into vector
328:                    while ((line = reader.readLine()) != null) {
329:                        //String line = reader.readLine();
330:                        if (line.startsWith(">")) {
331:                            //Must remove 1st 2 chars.
332:                            line = line.substring(2);
333:                            result.b.add(line);
334:                        } else {
335:                            wrapper.nextLine = line;
336:                            break;
337:                        }
338:                    }
339:                } catch (IOException ex) {
340:                    throw new DiffFormatException("Unexpected end of file.", ex);
341:                }
342:
343:                wrapper.hunk = result;
344:                return wrapper;
345:            }
346:
347:            public static void main(String[] args) {
348:                try {
349:                    String file1 = args[0];
350:                    java.io.File flDiff1 = new java.io.File(file1);
351:
352:                    //Now read the diff into a string.
353:                    //String sDiff = new String(FileUtil.readBytesFromFileSys(flDiff1));
354:
355:                    diff d = buildDiff(flDiff1);
356:
357:                    System.out.println("\r\n\r\n\r\n");
358:                    DiffTextPrinter print = new DiffTextPrinter(System.out);
359:                    d.accept(print);
360:
361:                } catch (Throwable thr) {
362:                    thr.printStackTrace();
363:                }
364:            }
365:
366:            private static int intValue(String s) throws DiffFormatException {
367:                int i = -1;
368:                if (s == null || s.equals("")) {
369:                    throw new DiffFormatException(
370:                            "Invalid value where integer expected.");
371:                }
372:                try {
373:                    i = Integer.parseInt(s.trim());
374:                } catch (NumberFormatException ex) {
375:                    throw new DiffFormatException("The value " + s
376:                            + " is not an int.");
377:                }
378:                return i;
379:            }
380:
381:            private static class BuildHunkResult {
382:                String nextLine = null;
383:                Hunk hunk = null;
384:            }
385:
386:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.