/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * 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 . */ package org.openoffice.xmerge.converter.xml.sxw.aportisdoc; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.NamedNodeMap; import java.io.IOException; import java.net.URLDecoder; import org.openoffice.xmerge.Document; import org.openoffice.xmerge.ConvertData; import org.openoffice.xmerge.ConvertException; import org.openoffice.xmerge.DocumentSerializer; import org.openoffice.xmerge.converter.xml.OfficeConstants; import org.openoffice.xmerge.converter.xml.sxw.SxwDocument; import org.openoffice.xmerge.converter.xml.sxw.aportisdoc.DocConstants; import org.openoffice.xmerge.converter.palm.PalmDB; import org.openoffice.xmerge.converter.palm.Record; import org.openoffice.xmerge.converter.palm.PalmDocument; import org.openoffice.xmerge.util.Debug; import org.openoffice.xmerge.util.XmlUtil; /** *
AportisDoc implementation of * org.openoffice.xmerge.DocumentSerializer * for the {@link * org.openoffice.xmerge.converter.xml.sxw.aportisdoc.PluginFactoryImpl * PluginFactoryImpl}.
* *The serialize
method traverses the DOM
* document from the given Document
object. It uses a
* DocEncoder
object for the actual conversion of
* contents to the AportisDoc format.
DocEncoder
object for encoding to AportisDoc. */
private DocEncoder encoder = null;
/** SXW Document
object that this converter processes. */
private SxwDocument sxwDoc = null;
/**
* Constructor.
*
* @param doc A SXW Document
to be converted.
*/
public DocumentSerializerImpl(Document doc) {
sxwDoc = (SxwDocument) doc;
}
/**
* Method to convert a Document
into a PDB.
* It passes back the converted data as a ConvertData
* object.
This method is not thread safe for performance reasons. * This method should not be called from within two threads. * It would be best to call this method only once per object * instance.
* * @return TheConvertData
object containing the output.
*
* @throws ConvertException If any conversion error occurs.
* @throws IOException If any I/O error occurs.
*/
public ConvertData serialize() throws ConvertException, IOException {
// get the server document name
String docName = URLDecoder.decode(sxwDoc.getName(), DocConstants.ENCODING);
// get DOM document
org.w3c.dom.Document domDoc = sxwDoc.getContentDOM();
encoder = new DocEncoder();
// Traverse to the office:body element.
// There should only be one.
NodeList list = domDoc.getElementsByTagName(TAG_OFFICE_BODY);
int len = list.getLength();
if (len > 0) {
Node node = list.item(0);
traverseBody(node);
}
// create a ConvertData object.
//
Record records[] = encoder.getRecords();
ConvertData cd = new ConvertData();
PalmDocument palmDoc = new PalmDocument(docName,
DocConstants.CREATOR_ID, DocConstants.TYPE_ID,
0, PalmDB.PDB_HEADER_ATTR_BACKUP, records);
cd.addDocument(palmDoc);
return cd;
}
/**
* This method traverses office:body element.
*
* @param node office:body Node
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseBody(Node node) throws IOException {
log("");
log("Node
objects.
*
* @param node A text:p or text:h
* Node
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseParagraph(Node node) throws IOException {
log("traverseParaElem
method to
* traverse into Element Node
objects.
*
* @param node A paragraph or content Node
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseParaContents(Node node) throws IOException {
if (node.hasChildNodes()) {
NodeList nodeList = node.getChildNodes();
int len = nodeList.getLength();
for (int i = 0; i < len; i++) {
Node child = nodeList.item(i);
short nodeType = child.getNodeType();
switch (nodeType) {
case Node.TEXT_NODE:
// this is for grabbing text nodes.
String s = child.getNodeValue();
if (s.length() > 0) {
encoder.addText(s);
}
log("Element
Node
* within a paragraph.
*
* @param node Element
Node
within a
* paragraph.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseParaElem(Node node) throws IOException {
String nodeName = node.getNodeName();
if (nodeName.equals(TAG_SPACE)) {
// this is for text:s tags.
NamedNodeMap map = node.getAttributes();
Node attr = map.getNamedItem(ATTRIBUTE_SPACE_COUNT);
StringBuffer space = new StringBuffer(SPACE_CHAR);
int count = 1;
if (attr != null) {
try {
String countStr = attr.getNodeValue();
count = Integer.parseInt(countStr.trim());
} catch (NumberFormatException e) {
// TODO: for now, throw IOException.
// later, perhaps will have to throw
// some other conversion exception instead.
throw new IOException(e.getMessage());
}
}
for (int j = 0; j < count; j++) {
space.append(SPACE_CHAR);
}
encoder.addText(space.toString());
log("Node
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseList(Node node) throws IOException {
log("Node
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseListHeader(Node node) throws IOException {
log("This method will traverse a text:list-item. * A list item may contain one or more of text:p, * text:h, text:section, text:ordered-list * and text:unordered-list.
* *This method currently only implements grabbing text:p, * text:h, text:unordered-list and * text:ordered-list.
* * @param node TheNode
.
*
* @throws IOException If any I/O error occurs.
*/
private void traverseListItem(Node node) throws IOException {
log("