KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SplitLongLines


1 import au.id.jericho.lib.html.*;
2 import java.util.*;
3 import java.io.*;
4 import java.net.*;
5
6 public class SplitLongLines {
7     private static final int MAX_LENGTH=70;
8
9     private static int col;
10
11     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
12         String JavaDoc sourceUrlString="../doc/index.html";
13         if (args.length==0)
14           System.err.println("Using default argument of \""+sourceUrlString+'"');
15         else
16             sourceUrlString=args[0];
17         if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString;
18         URL sourceUrl=new URL(sourceUrlString);
19         BufferedReader reader=null;
20         try {
21             reader=new BufferedReader(new InputStreamReader(sourceUrl.openStream()));
22             String JavaDoc line;
23             while ((line=reader.readLine())!=null) {
24                 if (line.length()<=MAX_LENGTH) {
25                     println(line);
26                     continue;
27                 }
28                 line=line.trim();
29                 if (line.length()<=MAX_LENGTH) {
30                     println(line);
31                     continue;
32                 }
33                 Source source=new Source(line);
34                 source.fullSequentialParse();
35                 int pos=0;
36                 for (final Iterator i=source.findAllTags().iterator(); i.hasNext();) {
37                     Tag tag=(Tag)i.next();
38                     if (pos!=tag.getBegin()) print(line.subSequence(pos,tag.getBegin())); // print the text between this tag and the last
39
printTag(tag,line);
40                     pos=tag.getEnd();
41                 }
42                 if (pos!=line.length()) print(line.subSequence(pos,line.length())); // print the text between the last tag and the end of line
43
println();
44             }
45         } finally {
46             if (reader!=null) reader.close();
47         }
48   }
49
50     private static void println() {
51         System.out.println();
52         col=0;
53     }
54
55     private static void println(CharSequence JavaDoc text) {
56         System.out.println(text);
57         col=0;
58     }
59
60     private static void print(CharSequence JavaDoc text) {
61         print(text,true);
62     }
63
64     private static void print(CharSequence JavaDoc text, boolean splitLongText) {
65         if (splitLongText && text.length()>MAX_LENGTH) {
66             String JavaDoc[] words=text.toString().split("\\s");
67             for (int i=0; i<words.length; i++) {
68                 print(words[i],false);
69                 if (i<words.length-1) print(" ");
70             }
71             return;
72         }
73         if (col>0 && col+text.length()>MAX_LENGTH) println();
74         System.out.print(text);
75         col+=text.length();
76     }
77
78     private static void printTag(Tag tag, String JavaDoc line) {
79         if (tag.length()<=MAX_LENGTH || tag instanceof EndTag) {
80             print(tag);
81             return;
82         }
83         StartTag startTag=(StartTag)tag;
84         Attributes attributes=startTag.getAttributes();
85         if (attributes!=null) {
86             print(line.substring(startTag.getBegin(),attributes.getBegin()));
87             for (Iterator j=attributes.iterator(); j.hasNext();) {
88                 Attribute attribute=(Attribute)j.next();
89                 print(" ");
90                 print(attribute);
91             }
92             print(line.substring(attributes.getEnd(),startTag.getEnd()));
93         } else {
94             print(startTag);
95         }
96     }
97 }
98
Popular Tags