Back to index

wims  3.65+svn20090927
Translator.java
Go to the documentation of this file.
00001 /*
00002  * Created on 04.04.2007
00003  * Translates the help files of Z.u.L. into HTML code.
00004  */
00005 
00006 package rene.zirkel.help;
00007 
00008 import java.io.*;
00009 import java.util.*;
00010 
00011 import rene.gui.Global;
00012 import rene.util.*;
00013 import rene.util.sort.*;
00014 
00019 class Keyword implements SortObject
00020 {      public String K;
00021        public MyVector V;
00022        public Keyword (String key)
00023        {      K=key;
00024               V=new MyVector();
00025        }
00029        public int compare (SortObject o) 
00030        {      Keyword k=(Keyword)o;
00031               return K.toLowerCase().compareTo(k.K.toLowerCase());
00032        }
00037        public void addTopic (String s)
00038        {      if (V.indexOf(s)>=0) return;
00039               V.addElement(s);
00040        }
00041 }
00042 
00046 class Topic implements SortObject
00047 {      public String T;
00048        public String D;
00049        public Topic (String topic, String descr)
00050        {      T=topic; D=descr;
00051        }
00052        public int compare(SortObject o) 
00053        {      Topic k=(Topic)o;
00054               return D.toLowerCase().compareTo(k.D.toLowerCase());
00055        }
00056 }
00057 
00064 public class Translator 
00065 {      BufferedReader in;
00066        PrintWriter out;
00067        String line;
00068        Hashtable Headers; // topics and long descriptions
00069        Hashtable Keywords; // keywords and occurances in topics
00070        Hashtable Aliases; // topics equal to other topics
00071        Hashtable NextTopic; // Next topic of each topic
00072        
00078        public Translator (String source, String language)
00079        {      if (language!=null) Locale.setDefault(new Locale(language,""));
00080               Global.initBundle("rene/zirkel/docs/ZirkelProperties");
00081               String codepage=Global.name("codepage.help","default");
00082        
00083               try
00084               {      
00085                      // collect topics:
00086                      Headers=new Hashtable();
00087                      Aliases=new Hashtable();
00088                      NextTopic=new Hashtable();
00089                      // open source:
00090                      if (codepage.equals("default")) 
00091                             in=new BufferedReader(new InputStreamReader(new FileInputStream(source)));
00092                      else
00093                             in=new BufferedReader(new InputStreamReader(new FileInputStream(source),codepage));
00094                      // read all topics
00095                      line=in.readLine();
00096                      String topic="";
00097                      while (line!=null)
00098                      {      if (line.startsWith("."))
00099                             {      String thistopic=addHeader(); // add the topic
00100                                    if (!topic.equals("") && !thistopic.equals("")) // add as next topic
00101                                    {      NextTopic.put(topic,thistopic);
00102                                    }
00103                                    if (!thistopic.equals(""))
00104                                           topic=thistopic;
00105                                    continue;
00106                             }
00107                             line=in.readLine();
00108                      }
00109                      in.close();
00110                      
00111                      // print and collect keywords:
00112                      Keywords=new Hashtable();
00113                      // open source:
00114                      if (codepage.equals("default")) 
00115                             in=new BufferedReader(new InputStreamReader(new FileInputStream(source)));
00116                      else
00117                             in=new BufferedReader(new InputStreamReader(new FileInputStream(source),codepage));
00118                      line=in.readLine();
00119                      // write output file for each topic:
00120                      while (line!=null)
00121                      {      if (line.startsWith("."))
00122                             {      writeFile();
00123                                    continue;
00124                             }
00125                             line=in.readLine();
00126                      }
00127                      in.close();
00128                      
00129                      // write keyword file:
00130                      writeKeywords();
00131                      
00132                      // write topics file:
00133                      writeTopics();
00134               }
00135               catch (Exception e) { e.printStackTrace(); }
00136        }
00137        
00138     // function that implements a java 1.5 style: String0.replace(String1,String2) --> replace(String0,String1,String2)                    
00139     // thanks to Bernadette Perrin and her java 1.4 ;)                                                                                     
00140     public static String replace(String source, String pattern, String replace)                                                            
00141     {                                                                                                                                      
00142             if (source!=null)                                                                                                              
00143             {                                                                                                                              
00144                 final int len = pattern.length();                                                                                          
00145                 StringBuffer sb = new StringBuffer();                                                                                      
00146                 int found = -1;                                                                                                            
00147                 int start = 0;                                                                                                             
00148                 while( (found = source.indexOf(pattern, start) ) != -1) {                                                                  
00149                     sb.append(source.substring(start, found));                                                                             
00150                     sb.append(replace);                                                                                                    
00151                     start = found + len;                                                                                                   
00152                 }                                                                                                                          
00153             sb.append(source.substring(start));                                                                                            
00154             return sb.toString();                                                                                                          
00155             }                                                                                                                              
00156         else return "";                                                                                                                    
00157     }  
00158        
00159        
00160 
00167        public String addHeader ()
00168               throws IOException
00169        {      // extract topic name:
00170               String filename=line.substring(1);
00171               int n=filename.indexOf(" ");
00172               
00173               // filename (topic) has some aliases
00174               if (n>0)
00175               {      StringTokenizer t=new StringTokenizer(filename.substring(n+1));
00176                      filename=filename.substring(0,n);
00177                      while (t.hasMoreTokens())
00178                      {      String alias=t.nextToken();
00179                             Aliases.put(alias,filename);
00180                      }
00181               }
00182               
00183               // skip .related lines:
00184               line=in.readLine();
00185               while (line!=null)
00186               {      if (!line.startsWith(".related")) break;
00187                      line=in.readLine();
00188               }
00189               if (line==null) return "";
00190               
00191               // denote long description of topic (header)
00192               // using 1.1 style replace
00193               String header=replace(line,"__","");
00194               Headers.put(filename,header);
00195               while (line!=null)
00196               {      if (line.startsWith(".")) break;
00197                      line=in.readLine();
00198               }
00199               
00200               return filename;
00201        }
00202        
00208        public void writeFile ()
00209               throws IOException
00210        {      // find the topic name
00211               String filename=line.substring(1);
00212               int n=filename.indexOf(" ");
00213               if (n>0) filename=filename.substring(0,n);
00214               
00215               // open the topic HTML file
00216               out=new PrintWriter(new OutputStreamWriter(
00217                      new FileOutputStream(filename+".html"),"utf-8"));
00218               
00219               // write the header for this file
00220               writeHeader(out);
00221               
00222               // skip through the related lines and remember them
00223               line=in.readLine();
00224               String related="";
00225               while (line!=null)
00226               {      if (!line.startsWith(".related")) break;
00227                      related=related+line.substring(".related".length());
00228                      line=in.readLine();
00229               }
00230               if (line==null) return;
00231               
00232               // write out the header for the topic in h2 style
00233               out.println("<h2>"+extractKeywords(line,filename)+"</h2>");
00234               line=in.readLine();
00235               
00236               // go through the paragraphs of the topic
00237               boolean paragraph=false; // started a paragraph?
00238               boolean start=true; // first line of paragraph?
00239               while (line!=null)
00240               {      if (line.startsWith(".")) break; // end of topic!
00241                      if (line.trim().equals("")) // new paragraph!
00242                      {      if (paragraph) out.println("</p>"); // end paragraph
00243                             start=true; // first line of paragraph!
00244                             while (line!=null && // skip empty lines
00245                                    (line.trim().equals("") || line.startsWith("->") 
00246                                                  || line.startsWith("//image ")))
00247                             {      if (line.startsWith("//image ")) // paragraph image
00248                                    {      String image=line.substring("//image ".length());
00249                                           out.println("<p align=\"center\"><img src=\""+image+"\"/></p>");
00250                                    }
00251                                    line=in.readLine();
00252                             }
00253                             out.println("<p>"); // start new paragraph
00254                             paragraph=true;
00255                             continue; // continue with the paragraph
00256                      }
00257                      else
00258                      {      // if indented text is found, break for a new line
00259                             if (line.startsWith(" ") && !start) out.println("<br />");
00260                             else start=false; // no longer the first line
00261                             // extract the keywords of the line and print the result
00262                             if (line.startsWith("//image+ ")) // paragraph image
00263                             {      String image=line.substring("//image+ ".length());
00264                                    out.println("<img align=\"right\" src=\""+image+"\"/>");
00265                             }
00266                             else if (line.startsWith("//image- ")) // paragraph image
00267                             {      String image=line.substring("//image+ ".length());
00268                                    out.println("<img align=\"left\" src=\""+image+"\"/>");
00269                             }
00270                             else 
00271                             {      out.println(extractKeywords(line.trim(),filename));
00272                             }
00273                      }
00274                      line=in.readLine();
00275               }
00276               if (paragraph) out.println("</p>");
00277               
00278               // write out the related links
00279               out.println("<p>");
00280               out.print(Global.name("help.related","Related Topics")+": ");
00281               StringTokenizer st=new StringTokenizer(related," ");
00282               boolean first=true;
00283               while (st.hasMoreElements())
00284               {      String s=st.nextToken();
00285                      String t="";
00286                      try
00287                      {      if (Headers.get(s)!=null)
00288                                    t=(String)Headers.get(s);
00289                             else if (Headers.get((String)Aliases.get(s))!=null)
00290                                    t=(String)Headers.get((String)Aliases.get(s));
00291                      }
00292                      catch (Exception e)
00293                      {      System.out.println(s+" missing in "+filename);
00294                      }
00295                      if (!first) out.println(", ");
00296                      out.print("<a href=\""+s+".html\">"+t+"</a>");
00297                      first=false;
00298               }
00299               out.println();
00300               out.println("</p>");
00301               
00302               // write out next topic
00303               if (NextTopic.get(filename)!=null)
00304               {      out.println("<p>");
00305                      out.print(Global.name("help.nexttopic","NextTopic")+": ");
00306                      String nexttopic=(String)(NextTopic.get(filename));
00307                      out.println("<a href=\""+nexttopic+".html\">"+
00308                                    (String)(Headers.get(nexttopic))+"</a>");
00309                      out.println("</p>");
00310               }
00311               
00312               // write footer and close
00313               writeFooter(out);
00314               out.close();
00315        }
00316        
00325        public String extractKeywords (String s, String topic)
00326        {      // Do some necessary replacements
00327               s=s.replace('',' '); //  is used for blanks in keywords
00328               s=s.replaceAll(">","&gt;"); // convert > to HTML
00329               s=s.replaceAll("<","&lt;"); // convert < to HTML
00330               
00331               // Go through the keywords
00332               String t="";
00333               int n=s.indexOf("__"); // position of keyword
00334               if (n<0) return s; // none found!
00335               while (n>=0)
00336               {      t=t+s.substring(0,n)+"<b>"; // start modified line
00337                      s=s.substring(n+2); // rest of string
00338                      n=s.indexOf("__"); // end of keyword
00339                      if (n<0) return t+s+"</b>"; // keyword does not end!
00340                      String keyword=s.substring(0,n); // get keyword
00341                      addKeyword(keyword,topic); // add it
00342                      t=t+keyword+"</b>"; // add it to output line
00343                      s=s.substring(n+2); // find next keyword!
00344                      n=s.indexOf("__");
00345               }
00346               return t+s;
00347        }
00348        
00354        public void addKeyword (String keyword, String topic)
00355        {      if (Keywords.get(keyword.toLowerCase())==null)
00356               {      // create a new keyword
00357                      Keyword k=new Keyword(keyword);
00358                      k.addTopic(topic);
00359                      // put it into the keyword list
00360                      Keywords.put(keyword.toLowerCase(),k);
00361               }
00362               else // have it already
00363               {      Keyword k=(Keyword)Keywords.get(keyword.toLowerCase());
00364                      k.addTopic(topic);
00365               }
00366        }
00367        
00372        public void writeKeywords ()
00373               throws IOException
00374        {      // open the output file and write header
00375               PrintWriter out=new PrintWriter(
00376                      new OutputStreamWriter(new FileOutputStream("keywords.html"),"utf-8"));
00377               writeHeader(out);
00378               
00379               // sort the keywords
00380               Enumeration e=Keywords.elements(); // enumeration of all keywords
00381               MyVector All=new MyVector(); // vector to store the keywords
00382               while (e.hasMoreElements()) // store them
00383               {      Keyword key=(Keyword)e.nextElement();
00384                      All.addElement(key);
00385               }
00386               // copy to an array
00387               Keyword k[]= new Keyword[All.size()];
00388               All.copyInto(k);
00389               // sort
00390               Sorter.sort(k,All.size());
00391               
00392               // write out with starting letter as header
00393               out.println("<p>");
00394               char preface=0; // no letter yet
00395               for (int i=0; i<k.length; i++)
00396               {      char c=Character.toUpperCase(k[i].K.charAt(0)); // get first letter
00397                      if (c!=preface) // new first letter?
00398                      {      preface=c;
00399                             out.println("</p>");
00400                             out.println("<p><b>"+preface+"</b></p>");
00401                             out.println("<p>");
00402                      }
00403                      String keyword=k[i].K;
00404                      keyword=Character.toUpperCase(keyword.charAt(0))+keyword.substring(1);
00405                      out.print(keyword); // print keyword
00406                      for (int j=0; j<k[i].V.size(); j++) // print topics containing the keyword
00407                      {      String topic=(String)k[i].V.elementAt(j);
00408                             if (j==0) out.print(": ");
00409                             else out.print(", ");
00410                             out.print("<a href=\""+topic+".html\">"+((String)Headers.get(topic))+"</a>");
00411                      }
00412                      out.print("<br />"); // new line for next keyword
00413               }
00414               
00415               // finish and close
00416               out.println("</p>");
00417               writeFooter(out);
00418               out.close();
00419        }
00420        
00425        public void writeTopics ()
00426               throws IOException
00427        {      // open and write header
00428               PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream(
00429                      "topics.html"),"utf-8"));
00430               writeHeader(out);
00431               out.println("<p>");
00432               
00433               // walk through all topics and store in vector
00434               Enumeration e=Headers.keys();
00435               MyVector All=new MyVector();
00436               while (e.hasMoreElements())
00437               {      String key=(String)e.nextElement();
00438                      if (Headers.get(key)!=null)
00439                             All.addElement(new Topic(key,(String)Headers.get(key)));
00440               }
00441               
00442               // stoore in vector and sort
00443               Topic k[]= new Topic[All.size()];
00444               All.copyInto(k);
00445               Sorter.sort(k,All.size());
00446               
00447               // write out all topics
00448               for (int i=0; i<k.length; i++)
00449               {      out.println("<a href=\""+k[i].T+".html\">"+k[i].D+"</a><br />");
00450               }
00451               
00452               // finish and close
00453               out.println("</p>");
00454               writeFooter(out);
00455               out.close();
00456        }
00457        
00463        public void writeHeader (PrintWriter out)
00464               throws IOException
00465        {      BufferedReader in=new BufferedReader(
00466                      new InputStreamReader(new FileInputStream("header.dat"),"UTF-8"));
00467               copy(in,out);
00468               in.close();
00469        }
00470        
00476        public void writeFooter (PrintWriter out)
00477               throws IOException
00478        {      BufferedReader in=new BufferedReader(
00479                      new InputStreamReader(new FileInputStream("footer.dat"),"UTF-8"));
00480               copy(in,out);
00481               in.close();
00482        }
00483 
00490        public void copy (BufferedReader in, PrintWriter out)
00491               throws IOException
00492        {      while (true)
00493               {      String line=in.readLine();
00494                      if (line==null) return;
00495                      out.println(line);
00496               }
00497        }
00498        
00502        public static void main (String[] args) 
00503        {      if (args.length!=2)
00504                      System.out.println("usage: java -cp zirkel.jar rene.helptohtmt.Translator source language");
00505               else
00506                      new Translator(args[0],args[1]);
00507        }
00508 
00509 }