*/
private SxwDocument buildDocument(String docName, Wse[] data, Document origDoc)
throws IOException {
// create minimum office xml document.
SxwDocument sxwDoc = new SxwDocument(docName);
sxwDoc.initContentDOM();
org.w3c.dom.Document doc = sxwDoc.getContentDOM();
// Grab hold of the office:body tag,
// Assume there should be one.
// This is where top level paragraphs will append to.
NodeList list = doc.getElementsByTagName(TAG_OFFICE_BODY);
Node bodyNode = list.item(0);
styleCat = new StyleCatalog(50);
oldStyleCat = new StyleCatalog(50);
if (origDoc != null)
readStyleCatalog(origDoc);
Element currPara = null;
ParaStyle currParaStyle = null;
int newTextStyleNr = 0;
int newParaStyleNr = 0;
// Now write out the document body by running through
// the list of WordSmith elements and processing each one
// in turn.
for (int i = 0; i < data.length; i++) {
if (data[i].getClass() == WsePara.class) {
currPara = doc.createElement(TAG_PARAGRAPH);
log("</PARA>");
log("<PARA>");
WsePara p = (WsePara)data[i];
// Save info about the first text run, if there is one.
WseTextRun firstTextRun = null;
if ((data.length >= i + 2)
&& (data[i+1].getClass() == WseTextRun.class))
firstTextRun = (WseTextRun)data[i+1];
Style matches[] = oldStyleCat.getMatching(p.makeStyle());
// See if we can find a unique match in the catalog
// of existing styles from the original document.
ParaStyle pStyle = null;
if (matches.length == 1) {
pStyle = (ParaStyle)matches[0];
log("using an existing style");
} else if ((matches.length > 1) && (firstTextRun != null)) {
pStyle = matchParaByText(matches, firstTextRun.makeStyle());
log("resolved a para by looking @ text");
}
// If nothing found so far, try looking in the catalog
// of newly-created styles.
// DJP FIXME: if we need to add two para styles with the
// same para formatting info but different default text
// styles, this won't work!
if (pStyle == null) {
log("had " + matches.length + " matches in old catalog");
matches = styleCat.getMatching(p.makeStyle());
if (matches.length == 0) {
pStyle = p.makeStyle();
String newName = new String("PPP" + ++newParaStyleNr);
pStyle.setName(newName);
styleCat.add(pStyle);
// DJP: write in the text format info here
log("created a new style");
} else if (matches.length == 1) {
pStyle = (ParaStyle)matches[0];
log("re-using a new style");
} else if (firstTextRun != null) {
pStyle = matchParaByText(matches, firstTextRun.makeStyle());
if (pStyle != null) {
log("resolved a (new) para by looking @ text");
} else
log("Hey this shouldn't happen! - nr of matches is "
+ matches.length);
}
}
if (pStyle == null)
log("Unable to figure out a para style");
// Figured out a style to use. Specify the style in this
// paragraph's attributes.
currPara.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME, pStyle.getName());
bodyNode.appendChild(currPara);
currParaStyle = pStyle;
} else if (data[i].getClass() == WseTextRun.class) {
WseTextRun tr = (WseTextRun)data[i];
TextStyle trStyle = null;
Node trNodes[] = parseText(tr.getText(), doc);
// First see if the formatting of this text run matches
// the default text formatting for this paragraph. If
// it does, then just make the text node(s) children of
// the current paragraph.
Style[] cps = new Style[1];
cps[0] = currParaStyle;
if (matchParaByText(cps, tr.makeStyle()) != null) {
for (int ii = 0; ii < trNodes.length; ii++) {
currPara.appendChild(trNodes[ii]);
}
continue;
}
// Check for existing, matching styles in the old style
// catalog. If exactly one is found, use it. Otherwise,
// check the new style catalog, and either use the style
// found or add this new one to it.
Style matches[] = oldStyleCat.getMatching(tr.makeStyle());
if (matches.length == 1)
trStyle = (TextStyle)matches[0];
else {
matches = styleCat.getMatching(tr.makeStyle());
if (matches.length == 0) {
trStyle = tr.makeStyle();
String newName = new String("TTT" + ++newTextStyleNr);
trStyle.setName(newName);
styleCat.add(trStyle);
} else if (matches.length == 1)
trStyle = (TextStyle)matches[0];
else
log("multiple text style matches from new catalog");
}
// Create a text span node, set the style attribute, make the
// text node(s) its children, and append it to current paragraph's
// list of children.
Element textSpanNode = doc.createElement(TAG_SPAN);
textSpanNode.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME, trStyle.getName());
for (int ii = 0; ii < trNodes.length; ii++) {
textSpanNode.appendChild(trNodes[ii]);
}
currPara.appendChild(textSpanNode);
log("</SPAN>");
}
else if (data[i].getClass() == WseFontTable.class) {
fontTable = (WseFontTable)data[i];
}
else if (data[i].getClass() == WseColorTable.class) {
colorTable = (WseColorTable)data[i];
}
}
//NodeList r = doc.getElementsByTagName(TAG_OFFICE_DOCUMENT);
NodeList r = doc.getElementsByTagName(TAG_OFFICE_DOCUMENT_CONTENT);
Node rootNode = r.item(0);
// read the original document
org.w3c.dom.NodeList nl;
if (origDoc != null) {
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
origDoc.write(bos);
SxwDocument origSxwDoc = new SxwDocument("old");
origSxwDoc.read(new ByteArrayInputStream(bos.toByteArray()));
org.w3c.dom.Document origDomDoc = origSxwDoc.getContentDOM();
XmlUtil xu = new XmlUtil();
org.w3c.dom.DocumentFragment df;
org.w3c.dom.Node newNode;