Package net.java.textilej.parser.util

Source Code of net.java.textilej.parser.util.TextileToEclipseToc

package net.java.textilej.parser.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;

import net.java.textilej.parser.outline.OutlineItem;
import net.java.textilej.parser.outline.OutlineParser;
import net.java.textilej.util.DefaultXmlStreamWriter;
import net.java.textilej.util.FormattingXMLStreamWriter;
import net.java.textilej.util.XmlStreamWriter;

/**
* A conversion utility targeting the
* <a href="http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_help_toc.html">Eclipse help table of contents format</a>.
*
* @author dgreen
*/
public class TextileToEclipseToc {

  private String bookTitle;
  private String htmlFile;
 
  public static void main(String[] args) {
    try {
      if (args.length < 1) {
        usage();
        System.exit(-1);
      }
      File inputFile = new File(args[0]);
      File outputFile = args.length < 2?null:new File(args[1]);
      if (outputFile != null && outputFile.exists()) {
        System.err.println("File "+outputFile+" already exists");
        usage();
        System.exit(-1);
      }
     
      if (!inputFile.exists()) {
        System.err.println("File "+outputFile+" does not exist");
        usage();
        System.exit(-1);
      }
     
      String textileSource = readFully(inputFile);
     
      TextileToEclipseToc textileToEclipseToc = new TextileToEclipseToc();
      String name = inputFile.getName();
      if (name.lastIndexOf('.') != -1) {
        name = name.substring(0,name.lastIndexOf('.'));
      }
      textileToEclipseToc.setBookTitle(name);
      textileToEclipseToc.setHtmlFile(name+".html");
     
      String docbookSource = textileToEclipseToc.parse(textileSource);
      if (outputFile == null) {
        System.out.println(docbookSource);
      } else {
        Writer writer = new OutputStreamWriter(new FileOutputStream(outputFile),"utf-8");
        try {
          writer.write(docbookSource);
        } finally {
          writer.close();
        }
      }
     
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }
 
  public String parse(String textileSource) {
    OutlineParser parser = new OutlineParser();
   
    OutlineItem root = parser.parse(textileSource);
   
    StringWriter out = new StringWriter(8096);
   
    XmlStreamWriter writer = createXmlStreamWriter(out);
   
    writer.writeStartDocument("utf-8","1.0");
   
    writer.writeStartElement("toc");
    writer.writeAttribute("topic", getHtmlFile());
    writer.writeAttribute("label", getBookTitle());
   
    emitToc(writer,root.getChildren());
   
    writer.writeEndElement(); // toc
   
    writer.writeEndDocument();
    writer.close();
   
    return out.toString();
   
  }

  private void emitToc(XmlStreamWriter writer, List<OutlineItem> children) {
    for (OutlineItem item: children) {
      writer.writeStartElement("topic");
      writer.writeAttribute("href", getHtmlFile()+"#"+item.getId());
      writer.writeAttribute("label", item.getLabel());
     
      if (!item.getChildren().isEmpty()) {
        emitToc(writer,item.getChildren());
      }
     
      writer.writeEndElement(); // topic
    }
  }

  private static String readFully(File inputFile) throws IOException {
    int length = (int) inputFile.length();
    if (length <= 0) {
      length = 2048;
    }
    StringBuilder buf = new StringBuilder(length);
    Reader reader = new BufferedReader(new FileReader(inputFile));
    try {
      int c;
      while ((c = reader.read()) != -1) {
        buf.append((char)c);
      }
    } finally {
      reader.close();
    }
    return buf.toString();
  }

  private static void usage() {
    System.err.println("Usage: java "+TextileToEclipseToc.class.getName()+" <input file> [output file]");
  }


 

  public String getBookTitle() {
    return bookTitle;
  }

  public void setBookTitle(String bookTitle) {
    this.bookTitle = bookTitle;
  }


  public String getHtmlFile() {
    return htmlFile;
  }

  public void setHtmlFile(String htmlFile) {
    this.htmlFile = htmlFile;
  }

  protected XmlStreamWriter createXmlStreamWriter(Writer out) {
    XmlStreamWriter writer = new DefaultXmlStreamWriter(out);
    return new FormattingXMLStreamWriter(writer);
  }
 
}
TOP

Related Classes of net.java.textilej.parser.util.TextileToEclipseToc

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.