From e43a4c07efb8ef4159379fb213b761ee61fad246 Mon Sep 17 00:00:00 2001 From: arved Date: Sat, 19 May 2001 00:59:33 +0000 Subject: [PATCH] Seshadri G.K.: memory buffering git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194257 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/apps/CommandLineOptions.java | 26 +++ .../apache/fop/apps/CommandLineStarter.java | 4 +- src/org/apache/fop/apps/Driver.java | 16 +- src/org/apache/fop/fo/FONode.java | 72 +++---- src/org/apache/fop/fo/FOText.java | 73 ++----- src/org/apache/fop/fo/FOTreeBuilder.java | 13 +- src/org/apache/fop/system/BufferArray.java | 29 +++ src/org/apache/fop/system/BufferFile.java | 26 +++ src/org/apache/fop/system/BufferManager.java | 190 ++++++++++++++++++ 9 files changed, 343 insertions(+), 106 deletions(-) create mode 100644 src/org/apache/fop/system/BufferArray.java create mode 100644 src/org/apache/fop/system/BufferFile.java create mode 100644 src/org/apache/fop/system/BufferManager.java diff --git a/src/org/apache/fop/apps/CommandLineOptions.java b/src/org/apache/fop/apps/CommandLineOptions.java index 59e7048f4..a05c0ffdc 100644 --- a/src/org/apache/fop/apps/CommandLineOptions.java +++ b/src/org/apache/fop/apps/CommandLineOptions.java @@ -42,6 +42,9 @@ public class CommandLineOptions { /* output: text file */ private static final int TXT_OUTPUT = 6; + /* System buffers */ + private static final int BUFFER_FILE = 7; + /* use debug mode*/ Boolean errorDump = new Boolean(false); /* show configuration information */ @@ -58,10 +61,14 @@ public class CommandLineOptions { File xmlfile = null; /* name of output file */ File outfile = null; + /* name of buffer file */ + File bufferFile = null; /* input mode */ int inputmode = NOT_SET; /*output mode */ int outputmode = NOT_SET; + /* buffer mode */ + int buffermode = NOT_SET; /* language for user information */ String language = null; @@ -206,6 +213,21 @@ public class CommandLineOptions { } else { throw new FOPException("Don't know what to do with " + args[i]); } + } else if (args[i].equals("-buf")) { + if (buffermode == NOT_SET) { + buffermode = BUFFER_FILE; + } else { + MessageHandler.errorln("ERROR: you can only set one buffer method"); + printUsage(); + } + if ((i + 1 == args.length) || + (args[i + 1].charAt(0) == '-')) { + MessageHandler.errorln("ERROR: you must specify the buffer output file"); + printUsage(); + } else { + bufferFile = new File (args[i + 1]); + i++; + } } else { printUsage(); return false; @@ -392,6 +414,10 @@ public class CommandLineOptions { return language; } + public File getBufferFile() { + return bufferFile; + } + public Boolean isQuiet() { return quiet; } diff --git a/src/org/apache/fop/apps/CommandLineStarter.java b/src/org/apache/fop/apps/CommandLineStarter.java index 7ef33c3ea..bf25dd07d 100644 --- a/src/org/apache/fop/apps/CommandLineStarter.java +++ b/src/org/apache/fop/apps/CommandLineStarter.java @@ -45,8 +45,10 @@ public class CommandLineStarter extends Starter { XMLReader parser = inputHandler.getParser(); setParserFeatures(parser); + + Driver driver = new Driver(); + driver.setBufferFile(commandLineOptions.getBufferFile()); - Driver driver = new Driver(); if (errorDump) { driver.setErrorDump(true); } diff --git a/src/org/apache/fop/apps/Driver.java b/src/org/apache/fop/apps/Driver.java index 3443d4087..0880dc1d4 100644 --- a/src/org/apache/fop/apps/Driver.java +++ b/src/org/apache/fop/apps/Driver.java @@ -1,4 +1,4 @@ -/* +/* $Id$ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources." @@ -20,6 +20,7 @@ import org.apache.fop.configuration.Configuration; import org.apache.fop.tools.DocumentInputSource; import org.apache.fop.tools.DocumentReader; +import org.apache.fop.system.BufferManager; // DOM import org.w3c.dom.Document; @@ -130,10 +131,15 @@ public class Driver { /** If true, full error stacks are reported */ private boolean _errorDump = false; + /** the system resources that FOP will use */ + private BufferManager _bufferManager; + /** create a new Driver */ public Driver() { _stream = null; - _treeBuilder = new FOTreeBuilder(); + _bufferManager = new BufferManager(); + _treeBuilder = new FOTreeBuilder(); + _treeBuilder.setBufferManager(_bufferManager); setupDefaultMappings(); } @@ -451,6 +457,12 @@ public class Driver { } } + /* Set up the system buffers */ + + public void setBufferFile(File bufferFile) { + this._bufferManager.addBufferFile(bufferFile); + } + /** * format the formatting object tree into an area tree */ diff --git a/src/org/apache/fop/fo/FONode.java b/src/org/apache/fop/fo/FONode.java index a4d67c608..5b3d48107 100644 --- a/src/org/apache/fop/fo/FONode.java +++ b/src/org/apache/fop/fo/FONode.java @@ -1,60 +1,17 @@ /*-- $Id$ -- - - ============================================================================ - The Apache Software License, Version 1.1 - ============================================================================ - - Copyright (C) 1999 The Apache Software Foundation. All rights reserved. - - Redistribution and use in source and binary forms, with or without modifica- - tion, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - 3. The end-user documentation included with the redistribution, if any, must - include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if - and wherever such third-party acknowledgments normally appear. - - 4. The names "FOP" and "Apache Software Foundation" must not be used to - endorse or promote products derived from this software without prior - written permission. For written permission, please contact - apache@apache.org. - - 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the - Apache Software Foundation. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation and was originally created by - James Tauber . For more information on the Apache - Software Foundation, please see . - + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." */ + package org.apache.fop.fo; // FOP import org.apache.fop.apps.FOPException; import org.apache.fop.layout.Area; import org.apache.fop.layout.LinkSet; +import org.apache.fop.system.BufferManager; // Java import java.util.Vector; @@ -66,6 +23,8 @@ abstract public class FONode { protected FObj parent; + public BufferManager bufferManager; + public Vector children = new Vector(); // made public for searching for id's /** value of marker before layout begins */ @@ -93,6 +52,9 @@ abstract public class FONode { protected FONode(FObj parent) { this.parent = parent; + if (parent != null) { + this.bufferManager = parent.bufferManager; + } } public void setIsInTableCell() { @@ -152,6 +114,14 @@ abstract public class FONode { return this.parent; } + public void setBufferManager(BufferManager bufferManager) { + this.bufferManager = bufferManager; + } + + public BufferManager getBufferManager() { + return this.bufferManager; + } + public void setLinkSet(LinkSet linkSet) { this.linkSet = linkSet; for (int i = 0; i < this.children.size(); i++) { @@ -220,6 +190,12 @@ abstract public class FONode { return; int numChildren = this.children.size(); + + if (this.marker <= START) + { + return; + } + for (int i = this.marker + 1; i < numChildren; i++) { FONode fo = (FONode) children.elementAt(i); fo.resetMarker(); diff --git a/src/org/apache/fop/fo/FOText.java b/src/org/apache/fop/fo/FOText.java index 32c2fbe56..ae4971c71 100644 --- a/src/org/apache/fop/fo/FOText.java +++ b/src/org/apache/fop/fo/FOText.java @@ -1,52 +1,7 @@ -/*-- $Id$ -- - - ============================================================================ - The Apache Software License, Version 1.1 - ============================================================================ - - Copyright (C) 1999 The Apache Software Foundation. All rights reserved. - - Redistribution and use in source and binary forms, with or without modifica- - tion, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - 3. The end-user documentation included with the redistribution, if any, must - include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if - and wherever such third-party acknowledgments normally appear. - - 4. The names "FOP" and "Apache Software Foundation" must not be used to - endorse or promote products derived from this software without prior - written permission. For written permission, please contact - apache@apache.org. - - 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the - Apache Software Foundation. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation and was originally created by - James Tauber . For more information on the Apache - Software Foundation, please see . - +/* $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." */ package org.apache.fop.fo; @@ -60,13 +15,14 @@ import org.apache.fop.layout.*; import org.apache.fop.datatypes.*; import org.apache.fop.fo.properties.*; import org.apache.fop.apps.FOPException; +import org.apache.fop.system.BufferManager; /** * a text node in the formatting object tree */ public class FOText extends FONode { - protected char[] ca; + // protected char[] ca; protected int start; protected int length; @@ -89,10 +45,18 @@ public class FOText extends FONode { public FOText(char[] chars, int s, int e, FObj parent) { super(parent); this.start = 0; - this.ca = new char[e - s]; + char ca[] = new char[e - s]; for (int i = s; i < e; i++) - this.ca[i - s] = chars[i]; + ca[i - s] = chars[i]; this.length = e - s; + this.bufferManager = parent.bufferManager; + if (this.bufferManager != null) { + bufferManager.writeBuffer((Object) this, ca); + } + else { + System.out.println("abnormal exit"); + System.exit(0); + } } public void setUnderlined(boolean ul) { @@ -110,11 +74,13 @@ public class FOText extends FONode { public boolean willCreateArea() { + char ca[] = this.bufferManager.readBuffer((Object) this); this.whiteSpaceCollapse = this.parent.properties.get( "white-space-collapse").getEnum(); if(this.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE && length > 0) { return true; } + for (int i = start; i < start + length; i++) { char ch = ca[i]; if (!((ch == ' ') || (ch == '\n') || (ch == '\r') || @@ -126,6 +92,7 @@ public class FOText extends FONode { } public Status layout(Area area) throws FOPException { + char ca[] = this.bufferManager.readBuffer((Object) this); if (!(area instanceof BlockArea)) { MessageHandler.errorln("WARNING: text outside block area" + new String(ca, start, length)); @@ -152,7 +119,7 @@ public class FOText extends FONode { this.red = c.red(); this.green = c.green(); this.blue = c.blue(); - + this.verticalAlign = this.parent.properties.get("vertical-align").getEnum(); this.wrapOption = diff --git a/src/org/apache/fop/fo/FOTreeBuilder.java b/src/org/apache/fop/fo/FOTreeBuilder.java index 01f8f948d..e9891ee78 100644 --- a/src/org/apache/fop/fo/FOTreeBuilder.java +++ b/src/org/apache/fop/fo/FOTreeBuilder.java @@ -55,6 +55,8 @@ import org.apache.fop.layout.AreaTree; import org.apache.fop.messaging.MessageHandler; import org.apache.fop.apps.FOPException; import org.apache.fop.fo.pagination.Root; +import org.apache.fop.system.BufferManager; + // SAX import org.xml.sax.helpers.DefaultHandler; @@ -93,7 +95,9 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder { */ protected FObj rootFObj = null; - /** + public BufferManager bufferManager; + + /** * set of names of formatting objects encountered but unknown */ protected Hashtable unknownFOs = new Hashtable(); @@ -277,6 +281,7 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder { if (rootFObj == null) { rootFObj = fobj; + rootFObj.setBufferManager(this.bufferManager); if (!fobj.getName().equals("fo:root")) { throw new SAXException( new FOPException("Root element must" + @@ -296,6 +301,7 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder { */ public void format(AreaTree areaTree) throws FOPException { MessageHandler.logln("formatting FOs into areas"); + this.bufferManager.readComplete(); ((Root) this.rootFObj).format(areaTree); } @@ -309,5 +315,8 @@ public class FOTreeBuilder extends DefaultHandler implements TreeBuilder { { return (rootFObj != null); } - + + public void setBufferManager(BufferManager bufferManager) { + this.bufferManager = bufferManager; + } } diff --git a/src/org/apache/fop/system/BufferArray.java b/src/org/apache/fop/system/BufferArray.java new file mode 100644 index 000000000..628bfe7f3 --- /dev/null +++ b/src/org/apache/fop/system/BufferArray.java @@ -0,0 +1,29 @@ +/* $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." + */ + +// Seshadri +/* This package is to be used for all Oeprating System related activities. */ +/* This buffers data into an array in memory */ + + package org.apache.fop.system; + + +// FOP + + + +// Java + + +public class BufferArray { + + + public BufferArray() { + + } + +} + diff --git a/src/org/apache/fop/system/BufferFile.java b/src/org/apache/fop/system/BufferFile.java new file mode 100644 index 000000000..f92650557 --- /dev/null +++ b/src/org/apache/fop/system/BufferFile.java @@ -0,0 +1,26 @@ +/* $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." + */ + +// Seshadri +/* This package is to be used for all Oeprating System related activities. */ +/* This buffers data into a file */ + + + +package org.apache.fop.system; + + +import java.io.*; + + + +public class BufferFile { + + public BufferFile () { + + } + +} diff --git a/src/org/apache/fop/system/BufferManager.java b/src/org/apache/fop/system/BufferManager.java new file mode 100644 index 000000000..8269d7edf --- /dev/null +++ b/src/org/apache/fop/system/BufferManager.java @@ -0,0 +1,190 @@ +/* $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." + */ + +// Seshadri +/* This package is to be used for all Oeprating System related activities. + This file manages system buffers */ + +package org.apache.fop.system; + + +// FOP + +import org.apache.fop.fo.FONode; + + +// Java + + import java.io.*; + import java.util.Hashtable; + + +public class BufferManager { + + + protected FileWriter fw; + protected FileReader fr; + protected char cache[]; // Cache + protected long csize; //Cache size + + + protected File buff=null; + + protected long fp = 0; + + protected long markStart=0; // used to set the current point in the stream while reading + protected long markEnd=0; + protected long curMark=0; + + // Hash of objects and their offsets within + + Hashtable offSetTable = new Hashtable(); + + private class Offset { + + long fp=0; // File Pointer + int length; + char[] data; // when no buffer is specified + + Offset(long fp,int length,char data[]) { + this.fp = fp; + this.length = length; + this.data = data; + } + } + + + + public void addBufferFile(File buff) { + + if (buff != null) + try { + fw = new FileWriter(buff); + fr = new FileReader(buff); + csize = 100000; + this.buff = buff; + } + catch (Exception e) { + System.out.println(e); + } + + } + + public void writeBuffer(Object obj,char arr[]) { + + int length = arr.length; + + if (buff != null) { + offSetTable.put(obj, new Offset(this.fp,length,null)); + try { + fw.write(arr); + + this.fp += length; + } + + catch (Exception e) { + System.out.println(e); + } + } + else { + // Store the data in memory + offSetTable.put(obj, new Offset(this.fp,length,arr)); + } + + + } + + +public void readComplete() { + + // An indication that manager can close the writable buffers and prepare + // for reading.. + if (buff != null) + try { + + fw.close(); + + cache = new char[(int) csize]; + setupCache(curMark); + + } + catch (Exception e) { + System.out.println(e); + } +} + + + + + public char[] readBuffer(Object obj) { + + Offset values = (Offset) offSetTable.get(obj); + + // Was buffering used? + + if (buff != null) { + + + char ca[] = new char[values.length]; + + // Check if csize is too small + + if (csize < values.length) { + System.out.println("Cache size too small"); + System.exit(0); + } + + + // Is the data outside the cache? + + if (! (values.fp >= markStart && values.fp + values.length <= markEnd)) { + + setupCache(values.fp); + } + + + for (long i = values.fp - markStart,j=0; j < values.length; ++i,++j) { + + ca[(int)j] = cache[(int)i]; + } + + + return ca; + } + else { + return values.data; + } + } + + protected void setupCache(long curMark) { + + try { + + FileReader fr = new FileReader(buff); + fr.skip(curMark); + + long rem = buff.length() - curMark; + if (rem > csize) { + + rem = csize; + } + + fr.read(cache,0,(int)rem); + + + markStart = curMark; + markEnd = rem - 1; + + } + catch (Exception e) { + System.out.println(e); + } + + + } + + +} -- 2.39.5