Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Compiling the proxy

Using a Doclet to compile the proxy ContentHandler

Return to article.


Listing 7: DocbookHandler

package org.ananas.hc.test;

import java.io.*;
import org.xml.sax.*;
import org.ananas.hc.*;
import org.xml.sax.helpers.*;

/**
 * @xmlns http://ananas.org/2002/docbook
 */

public class DocbookHandler
   implements org.ananas.hc.HCHandler
{
   protected PrintWriter writer;
   protected String title;

   public DocbookHandler()
   {
      this(null);
      if(writer == null)
      {
         System.out.println("error");
         System.exit(0);
      }
   }

   public DocbookHandler(PrintWriter writer)
   {
      if(writer == null)
         this.writer = new PrintWriter(new OutputStreamWriter(System.out));
      else
         this.writer = writer;
   }

   /**
    * @xpath /
    */
   public void startHTML()
   {
      title = null;
      writer.println("<html>");
   }

   /**
    * @xpath /
    */
   public void endHTML()
   {
      writer.println("</body>");
      writer.print("</html>");
      writer.flush();
   }

   /**
    * @xpath articleinfo
    */
   public void endInfo()
   {
      writer.println("<body>");
   }

   /**
    * @xpath para
    */
   public void startPara()
   {
      writer.print("<p>");
   }

   /**
    * @xpath para
    */
   public void endPara()
   {
      writer.println("</p>");
   }

   /**
    * @xpath para
    * @xpath ulink
    */
   public void characters(char[] ch,int offset,int len)
   {
      writer.write(ch,offset,len);
   }

   /**
    * @xpath articleinfo/title
    */
   public void charactersTitle(String title)
   {
      this.title = title;
   }

   /**
    * @xpath ulink
    */
   public void startULink(Attributes atts)
   {
      writer.print("<a href='");
      writer.print(atts.getValue("uri"));
      writer.print("'>");
   }

   /**
    * @xpath ulink
    */
   public void endULink()
   {
      writer.print("</a>");
   }

   /**
    * @xpath articleinfo/title
    */
   public void endArticleTitle()
   {
      writer.print("<header><title>");
      writer.print(title);
      writer.println("</title></header>");
      writer.print("<h1>");
      writer.print(title);
      writer.println("</h1>");
   }

   public static void main(String[] params)
   {
      try
      {
         XMLReader xparser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
         if(params.length > 1)
         {
            PrintWriter writer = new PrintWriter(new FileWriter(params[1]));
            xparser.setContentHandler(new XPathHandler(new DocbookHandler(writer)));
         }
         else
            xparser.setContentHandler(new XPathHandler(new DocbookHandler()));
         xparser.parse(new InputSource(params[0]));
      }
      catch(SAXException e)
      {
         if(e.getException() != null)
            e.getException().printStackTrace();
         else
            e.printStackTrace();
      }
      catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Return to article.