Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Member Functions | Package Attributes
rene.zirkel.help.Translator Class Reference

Translator for help texts in Z.u.L. More...

Collaboration diagram for rene.zirkel.help.Translator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Translator (String source, String language)
 Translator class, which does the work.
String addHeader () throws IOException
 Assuming, we found a topic line ".topic ...", add it to the topic list.
void writeFile () throws IOException
 Write a file for the topic, collecting all the keywords in it along the way.
String extractKeywords (String s, String topic)
 Extract the keywords from a line of text.
void addKeyword (String keyword, String topic)
 Add a keyword in this topic to the Keywords list.
void writeKeywords () throws IOException
 Write out all keyworkds to the keyword HTMl file.
void writeTopics () throws IOException
 Write the topics file.
void writeHeader (PrintWriter out) throws IOException
 Write the header for each file, copied from header.dat.
void writeFooter (PrintWriter out) throws IOException
 Write the footer for each file, copied from footer.dat.
void copy (BufferedReader in, PrintWriter out) throws IOException
 Copy in to out till end.

Static Public Member Functions

static String replace (String source, String pattern, String replace)
static void main (String[] args)

Package Attributes

BufferedReader in
PrintWriter out
String line
Hashtable Headers
Hashtable Keywords
Hashtable Aliases
Hashtable NextTopic

Detailed Description

Translator for help texts in Z.u.L.

help format to a sequence of HTML pages, a keyword list, and a topic list. You need files header.dat and footer.dat in the current directory (which is the target directory).

Definition at line 64 of file Translator.java.


Constructor & Destructor Documentation

rene.zirkel.help.Translator.Translator ( String  source,
String  language 
) [inline]

Translator class, which does the work.

Parameters:
sourceName of source help file
codepageCodepage to read the source file

Definition at line 78 of file Translator.java.

       {      if (language!=null) Locale.setDefault(new Locale(language,""));
              Global.initBundle("rene/zirkel/docs/ZirkelProperties");
              String codepage=Global.name("codepage.help","default");
       
              try
              {      
                     // collect topics:
                     Headers=new Hashtable();
                     Aliases=new Hashtable();
                     NextTopic=new Hashtable();
                     // open source:
                     if (codepage.equals("default")) 
                            in=new BufferedReader(new InputStreamReader(new FileInputStream(source)));
                     else
                            in=new BufferedReader(new InputStreamReader(new FileInputStream(source),codepage));
                     // read all topics
                     line=in.readLine();
                     String topic="";
                     while (line!=null)
                     {      if (line.startsWith("."))
                            {      String thistopic=addHeader(); // add the topic
                                   if (!topic.equals("") && !thistopic.equals("")) // add as next topic
                                   {      NextTopic.put(topic,thistopic);
                                   }
                                   if (!thistopic.equals(""))
                                          topic=thistopic;
                                   continue;
                            }
                            line=in.readLine();
                     }
                     in.close();
                     
                     // print and collect keywords:
                     Keywords=new Hashtable();
                     // open source:
                     if (codepage.equals("default")) 
                            in=new BufferedReader(new InputStreamReader(new FileInputStream(source)));
                     else
                            in=new BufferedReader(new InputStreamReader(new FileInputStream(source),codepage));
                     line=in.readLine();
                     // write output file for each topic:
                     while (line!=null)
                     {      if (line.startsWith("."))
                            {      writeFile();
                                   continue;
                            }
                            line=in.readLine();
                     }
                     in.close();
                     
                     // write keyword file:
                     writeKeywords();
                     
                     // write topics file:
                     writeTopics();
              }
              catch (Exception e) { e.printStackTrace(); }
       }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

String rene.zirkel.help.Translator.addHeader ( ) throws IOException [inline]

Assuming, we found a topic line ".topic ...", add it to the topic list.

Exceptions:
IOException
Returns:
topic name

Definition at line 167 of file Translator.java.

       {      // extract topic name:
              String filename=line.substring(1);
              int n=filename.indexOf(" ");
              
              // filename (topic) has some aliases
              if (n>0)
              {      StringTokenizer t=new StringTokenizer(filename.substring(n+1));
                     filename=filename.substring(0,n);
                     while (t.hasMoreTokens())
                     {      String alias=t.nextToken();
                            Aliases.put(alias,filename);
                     }
              }
              
              // skip .related lines:
              line=in.readLine();
              while (line!=null)
              {      if (!line.startsWith(".related")) break;
                     line=in.readLine();
              }
              if (line==null) return "";
              
              // denote long description of topic (header)
              // using 1.1 style replace
              String header=replace(line,"__","");
              Headers.put(filename,header);
              while (line!=null)
              {      if (line.startsWith(".")) break;
                     line=in.readLine();
              }
              
              return filename;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.addKeyword ( String  keyword,
String  topic 
) [inline]

Add a keyword in this topic to the Keywords list.

Parameters:
keyword
topic

Definition at line 354 of file Translator.java.

       {      if (Keywords.get(keyword.toLowerCase())==null)
              {      // create a new keyword
                     Keyword k=new Keyword(keyword);
                     k.addTopic(topic);
                     // put it into the keyword list
                     Keywords.put(keyword.toLowerCase(),k);
              }
              else // have it already
              {      Keyword k=(Keyword)Keywords.get(keyword.toLowerCase());
                     k.addTopic(topic);
              }
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.copy ( BufferedReader  in,
PrintWriter  out 
) throws IOException [inline]

Copy in to out till end.

Parameters:
in
out
Exceptions:
IOException

Definition at line 490 of file Translator.java.

       {      while (true)
              {      String line=in.readLine();
                     if (line==null) return;
                     out.println(line);
              }
       }

Here is the caller graph for this function:

String rene.zirkel.help.Translator.extractKeywords ( String  s,
String  topic 
) [inline]

Extract the keywords from a line of text.

Keywords are marked like this§keyword, with § replacing the blanks (to keep keywords in one line).

Parameters:
sLine
topicStore keywords in this topic
Returns:
Modified line

Definition at line 325 of file Translator.java.

       {      // Do some necessary replacements
              s=s.replace('§',' '); // § is used for blanks in keywords
              s=s.replaceAll(">",">"); // convert > to HTML
              s=s.replaceAll("<","&lt;"); // convert < to HTML
              
              // Go through the keywords
              String t="";
              int n=s.indexOf("__"); // position of keyword
              if (n<0) return s; // none found!
              while (n>=0)
              {      t=t+s.substring(0,n)+"<b>"; // start modified line
                     s=s.substring(n+2); // rest of string
                     n=s.indexOf("__"); // end of keyword
                     if (n<0) return t+s+"</b>"; // keyword does not end!
                     String keyword=s.substring(0,n); // get keyword
                     addKeyword(keyword,topic); // add it
                     t=t+keyword+"</b>"; // add it to output line
                     s=s.substring(n+2); // find next keyword!
                     n=s.indexOf("__");
              }
              return t+s;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static void rene.zirkel.help.Translator.main ( String[]  args) [inline, static]
Parameters:
args

Definition at line 502 of file Translator.java.

       {      if (args.length!=2)
                     System.out.println("usage: java -cp zirkel.jar rene.helptohtmt.Translator source language");
              else
                     new Translator(args[0],args[1]);
       }

Here is the call graph for this function:

static String rene.zirkel.help.Translator.replace ( String  source,
String  pattern,
String  replace 
) [inline, static]

Definition at line 140 of file Translator.java.

    {                                                                                                                                      
            if (source!=null)                                                                                                              
            {                                                                                                                              
                final int len = pattern.length();                                                                                          
                StringBuffer sb = new StringBuffer();                                                                                      
                int found = -1;                                                                                                            
                int start = 0;                                                                                                             
                while( (found = source.indexOf(pattern, start) ) != -1) {                                                                  
                    sb.append(source.substring(start, found));                                                                             
                    sb.append(replace);                                                                                                    
                    start = found + len;                                                                                                   
                }                                                                                                                          
            sb.append(source.substring(start));                                                                                            
            return sb.toString();                                                                                                          
            }                                                                                                                              
        else return "";                                                                                                                    
    }  

Here is the caller graph for this function:

void rene.zirkel.help.Translator.writeFile ( ) throws IOException [inline]

Write a file for the topic, collecting all the keywords in it along the way.

Exceptions:
IOException

Definition at line 208 of file Translator.java.

       {      // find the topic name
              String filename=line.substring(1);
              int n=filename.indexOf(" ");
              if (n>0) filename=filename.substring(0,n);
              
              // open the topic HTML file
              out=new PrintWriter(new OutputStreamWriter(
                     new FileOutputStream(filename+".html"),"utf-8"));
              
              // write the header for this file
              writeHeader(out);
              
              // skip through the related lines and remember them
              line=in.readLine();
              String related="";
              while (line!=null)
              {      if (!line.startsWith(".related")) break;
                     related=related+line.substring(".related".length());
                     line=in.readLine();
              }
              if (line==null) return;
              
              // write out the header for the topic in h2 style
              out.println("<h2>"+extractKeywords(line,filename)+"</h2>");
              line=in.readLine();
              
              // go through the paragraphs of the topic
              boolean paragraph=false; // started a paragraph?
              boolean start=true; // first line of paragraph?
              while (line!=null)
              {      if (line.startsWith(".")) break; // end of topic!
                     if (line.trim().equals("")) // new paragraph!
                     {      if (paragraph) out.println("</p>"); // end paragraph
                            start=true; // first line of paragraph!
                            while (line!=null && // skip empty lines
                                   (line.trim().equals("") || line.startsWith("->") 
                                                 || line.startsWith("//image ")))
                            {      if (line.startsWith("//image ")) // paragraph image
                                   {      String image=line.substring("//image ".length());
                                          out.println("<p align=\"center\"><img src=\""+image+"\"/></p>");
                                   }
                                   line=in.readLine();
                            }
                            out.println("<p>"); // start new paragraph
                            paragraph=true;
                            continue; // continue with the paragraph
                     }
                     else
                     {      // if indented text is found, break for a new line
                            if (line.startsWith(" ") && !start) out.println("<br />");
                            else start=false; // no longer the first line
                            // extract the keywords of the line and print the result
                            if (line.startsWith("//image+ ")) // paragraph image
                            {      String image=line.substring("//image+ ".length());
                                   out.println("<img align=\"right\" src=\""+image+"\"/>");
                            }
                            else if (line.startsWith("//image- ")) // paragraph image
                            {      String image=line.substring("//image+ ".length());
                                   out.println("<img align=\"left\" src=\""+image+"\"/>");
                            }
                            else 
                            {      out.println(extractKeywords(line.trim(),filename));
                            }
                     }
                     line=in.readLine();
              }
              if (paragraph) out.println("</p>");
              
              // write out the related links
              out.println("<p>");
              out.print(Global.name("help.related","Related Topics")+": ");
              StringTokenizer st=new StringTokenizer(related," ");
              boolean first=true;
              while (st.hasMoreElements())
              {      String s=st.nextToken();
                     String t="";
                     try
                     {      if (Headers.get(s)!=null)
                                   t=(String)Headers.get(s);
                            else if (Headers.get((String)Aliases.get(s))!=null)
                                   t=(String)Headers.get((String)Aliases.get(s));
                     }
                     catch (Exception e)
                     {      System.out.println(s+" missing in "+filename);
                     }
                     if (!first) out.println(", ");
                     out.print("<a href=\""+s+".html\">"+t+"</a>");
                     first=false;
              }
              out.println();
              out.println("</p>");
              
              // write out next topic
              if (NextTopic.get(filename)!=null)
              {      out.println("<p>");
                     out.print(Global.name("help.nexttopic","NextTopic")+": ");
                     String nexttopic=(String)(NextTopic.get(filename));
                     out.println("<a href=\""+nexttopic+".html\">"+
                                   (String)(Headers.get(nexttopic))+"</a>");
                     out.println("</p>");
              }
              
              // write footer and close
              writeFooter(out);
              out.close();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.writeFooter ( PrintWriter  out) throws IOException [inline]

Write the footer for each file, copied from footer.dat.

Parameters:
out
Exceptions:
IOException

Definition at line 476 of file Translator.java.

       {      BufferedReader in=new BufferedReader(
                     new InputStreamReader(new FileInputStream("footer.dat"),"UTF-8"));
              copy(in,out);
              in.close();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.writeHeader ( PrintWriter  out) throws IOException [inline]

Write the header for each file, copied from header.dat.

Parameters:
out
Exceptions:
IOException

Definition at line 463 of file Translator.java.

       {      BufferedReader in=new BufferedReader(
                     new InputStreamReader(new FileInputStream("header.dat"),"UTF-8"));
              copy(in,out);
              in.close();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.writeKeywords ( ) throws IOException [inline]

Write out all keyworkds to the keyword HTMl file.

Exceptions:
IOException

Definition at line 372 of file Translator.java.

       {      // open the output file and write header
              PrintWriter out=new PrintWriter(
                     new OutputStreamWriter(new FileOutputStream("keywords.html"),"utf-8"));
              writeHeader(out);
              
              // sort the keywords
              Enumeration e=Keywords.elements(); // enumeration of all keywords
              MyVector All=new MyVector(); // vector to store the keywords
              while (e.hasMoreElements()) // store them
              {      Keyword key=(Keyword)e.nextElement();
                     All.addElement(key);
              }
              // copy to an array
              Keyword k[]= new Keyword[All.size()];
              All.copyInto(k);
              // sort
              Sorter.sort(k,All.size());
              
              // write out with starting letter as header
              out.println("<p>");
              char preface=0; // no letter yet
              for (int i=0; i<k.length; i++)
              {      char c=Character.toUpperCase(k[i].K.charAt(0)); // get first letter
                     if (c!=preface) // new first letter?
                     {      preface=c;
                            out.println("</p>");
                            out.println("<p><b>"+preface+"</b></p>");
                            out.println("<p>");
                     }
                     String keyword=k[i].K;
                     keyword=Character.toUpperCase(keyword.charAt(0))+keyword.substring(1);
                     out.print(keyword); // print keyword
                     for (int j=0; j<k[i].V.size(); j++) // print topics containing the keyword
                     {      String topic=(String)k[i].V.elementAt(j);
                            if (j==0) out.print(": ");
                            else out.print(", ");
                            out.print("<a href=\""+topic+".html\">"+((String)Headers.get(topic))+"</a>");
                     }
                     out.print("<br />"); // new line for next keyword
              }
              
              // finish and close
              out.println("</p>");
              writeFooter(out);
              out.close();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.zirkel.help.Translator.writeTopics ( ) throws IOException [inline]

Write the topics file.

Exceptions:
IOException

Definition at line 425 of file Translator.java.

       {      // open and write header
              PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream(
                     "topics.html"),"utf-8"));
              writeHeader(out);
              out.println("<p>");
              
              // walk through all topics and store in vector
              Enumeration e=Headers.keys();
              MyVector All=new MyVector();
              while (e.hasMoreElements())
              {      String key=(String)e.nextElement();
                     if (Headers.get(key)!=null)
                            All.addElement(new Topic(key,(String)Headers.get(key)));
              }
              
              // stoore in vector and sort
              Topic k[]= new Topic[All.size()];
              All.copyInto(k);
              Sorter.sort(k,All.size());
              
              // write out all topics
              for (int i=0; i<k.length; i++)
              {      out.println("<a href=\""+k[i].T+".html\">"+k[i].D+"</a><br />");
              }
              
              // finish and close
              out.println("</p>");
              writeFooter(out);
              out.close();
       }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 70 of file Translator.java.

Definition at line 68 of file Translator.java.

BufferedReader rene.zirkel.help.Translator.in [package]

Definition at line 65 of file Translator.java.

Definition at line 69 of file Translator.java.

Definition at line 67 of file Translator.java.

Definition at line 71 of file Translator.java.

PrintWriter rene.zirkel.help.Translator.out [package]

Definition at line 66 of file Translator.java.


The documentation for this class was generated from the following file: