/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: MIFHandler.java 679326 2008-07-24 09:35:34Z vhennebert $ */
package org.apache.fop.render.mif;
// Java
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fo.FOEventHandler;
import org.apache.fop.fo.flow.BasicLink;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.ExternalGraphic;
import org.apache.fop.fo.flow.Footnote;
import org.apache.fop.fo.flow.FootnoteBody;
import org.apache.fop.fo.flow.Inline;
import org.apache.fop.fo.flow.InstreamForeignObject;
import org.apache.fop.fo.flow.Leader;
import org.apache.fop.fo.flow.ListBlock;
import org.apache.fop.fo.flow.ListItem;
import org.apache.fop.fo.flow.PageNumber;
import org.apache.fop.fo.flow.table.Table;
import org.apache.fop.fo.flow.table.TableBody;
import org.apache.fop.fo.flow.table.TableCell;
import org.apache.fop.fo.flow.table.TableColumn;
import org.apache.fop.fo.flow.table.TableRow;
import org.apache.fop.fo.pagination.Flow;
import org.apache.fop.fo.pagination.PageSequence;
import org.apache.fop.fo.pagination.PageSequenceMaster;
import org.apache.fop.fo.pagination.SimplePageMaster;
import org.apache.fop.fonts.FontSetup;
import org.apache.fop.render.DefaultFontResolver;
import org.xml.sax.SAXException;
// TODO: do we really want every method throwing a SAXException
/**
* The MIF Handler.
* This generates MIF output using the structure events from
* the FO Tree sent to this structure handler.
* This builds an MIF file and writes it to the output.
*/
public class MIFHandler extends FOEventHandler {
/** Logger */
private static Log log = LogFactory.getLog(MIFHandler.class);
/** the MIFFile instance */
protected MIFFile mifFile;
/** the OutputStream to write to */
protected OutputStream outStream;
// current state elements
private MIFElement textFlow;
private MIFElement para;
/**
* Creates a new MIF handler on a given OutputStream.
* @param ua FOUserAgent instance for this process
* @param os OutputStream to write to
*/
public MIFHandler(FOUserAgent ua, OutputStream os) {
super(ua);
outStream = os;
FontSetup.setup(fontInfo, null, new DefaultFontResolver(ua));
}
/**
* @see org.apache.fop.fo.FOEventHandler#startDocument()
*/
public void startDocument() throws SAXException {
log.fatal("The MIF Handler is non-functional at this time. Please help resurrect it!");
mifFile = new MIFFile();
try {
mifFile.output(outStream);
} catch (IOException ioe) {
throw new SAXException(ioe);
}
}
/**
* @see org.apache.fop.fo.FOEventHandler#endDocument()
*/
public void endDocument() throws SAXException {
// finish all open elements
mifFile.finish(true);
try {
mifFile.output(outStream);
outStream.flush();
} catch (IOException ioe) {
throw new SAXException(ioe);
}
}
/**
* Start the page sequence.
* This creates the pages in the MIF document that will be used
* by the following flows and static areas.
* @see org.apache.fop.fo.FOEventHandler
*/
public void startPageSequence(PageSequence pageSeq) {
// get the layout master set
// setup the pages for this sequence
String name = pageSeq.getMasterReference();
SimplePageMaster spm = pageSeq.getRoot().getLayoutMasterSet().getSimplePageMaster(name);
if (spm == null) {
PageSequenceMaster psm = pageSeq.getRoot().getLayoutMasterSet().getPageSequenceMaster(name);
} else {
// create simple master with regions
MIFElement prop = new MIFElement("PageType");
prop.setValue("BodyPage");
MIFElement page = new MIFElement("Page");
page.addElement(prop);
prop = new MIFElement("PageBackground");
prop.setValue("'Default'");
page.addElement(prop);
// build regions
MIFElement textRect = new MIFElement("TextRect");
prop = new MIFElement("ID");
prop.setValue("1");
textRect.addElement(prop);
prop = new MIFElement("ShapeRect");
prop.setValue("0.0 841.889 453.543 0.0");
textRect.addElement(prop);
page.addElement(textRect);
textRect = new MIFElement("TextRect");
prop = new MIFElement("ID");
prop.setValue("2");
textRect.addElement(prop);
prop = new MIFElement("ShapeRect");
prop.setValue("0.0 841.889 453.543 187.65");
textRect.addElement(prop);
page.addElement(textRect);
mifFile.addPage(page);
}
}
/**
* @see org.apache.fop.fo.FOEventHandler#endPageSequence(PageSequence)
*/
public void endPageSequence(PageSequence pageSeq) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startFlow(Flow)
*/
public void startFlow(Flow fl) {
// start text flow in body region
textFlow = new MIFElement("TextFlow");
}
/**
* @see org.apache.fop.fo.FOEventHandler#endFlow(Flow)
*/
public void endFlow(Flow fl) {
textFlow.finish(true);
mifFile.addElement(textFlow);
textFlow = null;
}
/**
* @see org.apache.fop.fo.FOEventHandler#startBlock(Block)
*/
public void startBlock(Block bl) {
para = new MIFElement("Para");
// get font
textFlow.addElement(para);
}
/**
* @see org.apache.fop.fo.FOEventHandler#endBlock(Block)
*/
public void endBlock(Block bl) {
para.finish(true);
para = null;
}
/**
*
* @param inl Inline that is starting.
*/
public void startInline(Inline inl){
}
/**
*
* @param inl Inline that is ending.
*/
public void endInline(Inline inl){
}
/**
* @see org.apache.fop.fo.FOEventHandler#startTable(Table)
*/
public void startTable(Table tbl) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endTable(Table)
*/
public void endTable(Table tbl) {
}
/**
*
* @param tc TableColumn that is starting;
*/
public void startColumn(TableColumn tc) {
}
/**
*
* @param tc TableColumn that is ending;
*/
public void endColumn(TableColumn tc) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startHeader(TableBody)
*/
public void startHeader(TableBody th) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endHeader(TableBody)
*/
public void endHeader(TableBody th) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startFooter(TableBody)
*/
public void startFooter(TableBody tf) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endFooter(TableBody)
*/
public void endFooter(TableBody tf) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startBody(TableBody)
*/
public void startBody(TableBody tb) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endBody(TableBody)
*/
public void endBody(TableBody tb) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startRow(TableRow)
*/
public void startRow(TableRow tr) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endRow(TableRow)
*/
public void endRow(TableRow tr) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startCell(TableCell)
*/
public void startCell(TableCell tc) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endCell(TableCell)
*/
public void endCell(TableCell tc) {
}
// Lists
/**
* @see org.apache.fop.fo.FOEventHandler#startList(ListBlock)
*/
public void startList(ListBlock lb) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endList(ListBlock)
*/
public void endList(ListBlock lb) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startListItem(ListItem)
*/
public void startListItem(ListItem li) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endListItem(ListItem)
*/
public void endListItem(ListItem li) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startListLabel()
*/
public void startListLabel() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endListLabel()
*/
public void endListLabel() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startListBody()
*/
public void startListBody() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endListBody()
*/
public void endListBody() {
}
// Static Regions
/**
* @see org.apache.fop.fo.FOEventHandler#startStatic()
*/
public void startStatic() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endStatic()
*/
public void endStatic() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startMarkup()
*/
public void startMarkup() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endMarkup()
*/
public void endMarkup() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startLink(BasicLink basicLink)
*/
public void startLink(BasicLink basicLink) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endLink()
*/
public void endLink() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#image(ExternalGraphic)
*/
public void image(ExternalGraphic eg) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#pageRef()
*/
public void pageRef() {
}
/**
* @see org.apache.fop.fo.FOEventHandler#foreignObject(InstreamForeignObject)
*/
public void foreignObject(InstreamForeignObject ifo) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startFootnote(Footnote)
*/
public void startFootnote(Footnote footnote) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endFootnote(Footnote)
*/
public void endFootnote(Footnote footnote) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#startFootnoteBody(FootnoteBody)
*/
public void startFootnoteBody(FootnoteBody body) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#endFootnoteBody(FootnoteBody)
*/
public void endFootnoteBody(FootnoteBody body) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#leader(Leader)
*/
public void leader(Leader l) {
}
/**
* @see org.apache.fop.fo.FOEventHandler#characters(char[], int, int)
*/
public void characters(char data[], int start, int length) {
if (para != null) {
String str = new String(data, start, length);
str = str.trim();
// break into nice length chunks
if (str.length() == 0) {
return;
}
MIFElement line = new MIFElement("ParaLine");
MIFElement prop = new MIFElement("TextRectID");
prop.setValue("2");
line.addElement(prop);
prop = new MIFElement("String");
prop.setValue("\"" + str + "\"");
line.addElement(prop);
para.addElement(line);
}
}
/**
*
* @param pagenum PageNumber that is starting.
*/
public void startPageNumber(PageNumber pagenum) {
}
/**
*
* @param pagenum PageNumber that is ending.
*/
public void endPageNumber(PageNumber pagenum) {
}
}