Optimized the loading of ODF files (the old xmerge code is completely rewritten)
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@144 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
e3a808f820
commit
ecacd13bce
26 changed files with 1215 additions and 2603 deletions
123
source/java/writer2latex/util/SimpleDOMBuilder.java
Normal file
123
source/java/writer2latex/util/SimpleDOMBuilder.java
Normal file
|
@ -0,0 +1,123 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* SimpleDOMBuilder.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2012 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2012-03-22)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.util;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.DocumentType;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/** This class provides a simple way to create and populate a DOM tree in logical order
|
||||
*/
|
||||
public class SimpleDOMBuilder {
|
||||
private Document dom=null;
|
||||
private Element currentElement=null;
|
||||
|
||||
/**
|
||||
* Append an element to the current element and set this new element to be the current element.
|
||||
* If there is no current element, a new DOM tree will be created (discarding the current DOM tree if any)
|
||||
* with the new element as the document element.
|
||||
*
|
||||
* @param sTagName
|
||||
* @return true on success
|
||||
*/
|
||||
public boolean startElement(String sTagName) {
|
||||
if (currentElement!=null) {
|
||||
currentElement = (Element) currentElement.appendChild(dom.createElement(sTagName));
|
||||
}
|
||||
else {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
try {
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
DOMImplementation domImpl = builder.getDOMImplementation();
|
||||
DocumentType doctype = domImpl.createDocumentType(sTagName, "", "");
|
||||
dom = domImpl.createDocument("",sTagName,doctype);
|
||||
currentElement = dom.getDocumentElement();
|
||||
} catch (ParserConfigurationException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current element to the parent of the current element
|
||||
* @return true on success, false if there is no current element to end
|
||||
*/
|
||||
public boolean endElement() {
|
||||
if (currentElement!=null) {
|
||||
if (currentElement!=dom.getDocumentElement()) {
|
||||
currentElement=(Element) currentElement.getParentNode();
|
||||
}
|
||||
else { // Back at document element: Finished populating the DOM tree
|
||||
currentElement=null;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an attribute of the current element
|
||||
* @param sName
|
||||
* @param sValue
|
||||
* @return true on success, false if there is no current element
|
||||
*/
|
||||
public boolean setAttribute(String sName,String sValue) {
|
||||
if (currentElement!=null) {
|
||||
currentElement.setAttribute(sName, sValue);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add characters to the currentElement
|
||||
* @param sText
|
||||
* @return true on success, false if there is no current element
|
||||
*/
|
||||
public boolean characters(String sText) {
|
||||
if (currentElement!=null) {
|
||||
currentElement.appendChild(dom.createTextNode(sText));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DOM tree
|
||||
*
|
||||
* @return the DOM tree, or null if none has been created
|
||||
*/
|
||||
public Document getDOM() {
|
||||
return dom;
|
||||
}
|
||||
}
|
62
source/java/writer2latex/util/SimpleSAXHandler.java
Normal file
62
source/java/writer2latex/util/SimpleSAXHandler.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* SimpleSAXHandler.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2012 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2012-03-23)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.util;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
|
||||
/** A simple SAX handler which transforms the SAX events into a DOM tree
|
||||
* (supporting element and text nodes only)
|
||||
*/
|
||||
public class SimpleSAXHandler extends DefaultHandler {
|
||||
|
||||
private SimpleDOMBuilder builder = new SimpleDOMBuilder();
|
||||
|
||||
public org.w3c.dom.Document getDOM() {
|
||||
return builder.getDOM();
|
||||
}
|
||||
|
||||
@Override public void startElement(String nameSpace, String localName, String qName, Attributes attributes){
|
||||
builder.startElement(qName);
|
||||
int nLen = attributes.getLength();
|
||||
for (int i=0;i<nLen;i++) {
|
||||
builder.setAttribute(attributes.getQName(i), attributes.getValue(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void endElement(String nameSpace, String localName, String qName){
|
||||
builder.endElement();
|
||||
}
|
||||
|
||||
@Override public void characters(char[] characters, int nStart, int nEnd) throws SAXException {
|
||||
builder.characters(new String(characters,nStart,nEnd));
|
||||
|
||||
}
|
||||
|
||||
}
|
84
source/java/writer2latex/util/SimpleZipReader.java
Normal file
84
source/java/writer2latex/util/SimpleZipReader.java
Normal file
|
@ -0,0 +1,84 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* SimpleZipReader.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2012 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2012-03-27)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class SimpleZipReader {
|
||||
|
||||
private final static int BUFFERSIZE = 1024;
|
||||
|
||||
private Map<String,byte[]> entries = new HashMap<String,byte[]>();
|
||||
|
||||
|
||||
/** Read a zipped stream
|
||||
*
|
||||
* @param is <code>InputStream</code> to read
|
||||
*
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
public void read(InputStream is) throws IOException {
|
||||
ZipInputStream zis = new ZipInputStream(is);
|
||||
ZipEntry entry = null;
|
||||
while ((entry=zis.getNextEntry())!=null) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
int nLen = 0;
|
||||
byte buffer[] = new byte[BUFFERSIZE];
|
||||
while ((nLen = zis.read(buffer)) > 0) {
|
||||
baos.write(buffer, 0, nLen);
|
||||
}
|
||||
byte bytes[] = baos.toByteArray();
|
||||
entries.put(entry.getName(), bytes);
|
||||
}
|
||||
zis.close();
|
||||
}
|
||||
|
||||
/** Get an entry from the ZIP file. Getting should be taken quite literally here:
|
||||
* You can only get an entry once: The <code>SimpleZipReader</code> removes the entry from the
|
||||
* collection when this method is called (memory optimization).
|
||||
*
|
||||
* @param name the name (path) of the ZIP entry
|
||||
*
|
||||
* @return a byte array with the contents of the entry, or null if the entry does not exist
|
||||
*/
|
||||
public byte[] getEntry(String sName) {
|
||||
if (entries.containsKey(sName)) {
|
||||
byte[] bytes = entries.get(sName);
|
||||
entries.remove(sName);
|
||||
return bytes;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue