
net.alantea.docwork.OdtFile Maven / Gradle / Ivy
The newest version!
package net.alantea.docwork;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.incubator.doc.text.OdfTextSpan;
import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.style.Font;
import org.odftoolkit.simple.style.StyleTypeDefinitions;
import org.odftoolkit.simple.style.StyleTypeDefinitions.HorizontalAlignmentType;
import org.odftoolkit.simple.text.Paragraph;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import net.alantea.docwork.DocWorkParagraph.Alignment;
import net.alantea.utils.exception.LntException;
@SuppressWarnings("deprecation")
public class OdtFile extends DocWorkFile
{
private File targetFile;
private TextDocument textDocument;
public OdtFile(String path)
{
this(new File(path));
}
public OdtFile(File inputfile)
{
try
{
if (inputfile.exists())
{
textDocument = TextDocument.loadDocument(inputfile);
}
else
{
textDocument = TextDocument.newTextDocument();
}
targetFile = inputfile;
}
catch (Exception e)
{
new LntException("Error creating ODT file information class : " + inputfile.getAbsolutePath(), e);
}
}
@Override
public void save()
{
try
{
textDocument.save(targetFile);
}
catch (Exception e)
{
new LntException("Error saving ODT file : " + targetFile.getAbsolutePath(), e);
}
}
@Override
public void close()
{
textDocument.close();
}
@Override
public void appendParagraph(String text)
{
textDocument.addParagraph(text);
}
@Override
public void appendParagraph(DocWorkParagraph para)
{
String cont = "";
boolean bold = false;
boolean italic = false;
DocWorkSpan span0 = null;
for (DocWorkSpan span : para.getContent())
{
cont += span.getContent();
if (span0 == null)
{
span0 = span;
bold = span.isBold();
italic = span.isItalic();
}
}
if (para.isPageBreak())
{
textDocument.addPageBreak();
}
Paragraph paragraph = textDocument.addParagraph(cont);
StyleTypeDefinitions.FontStyle style = StyleTypeDefinitions.FontStyle.REGULAR;
if ((bold) && (italic))
{
style = StyleTypeDefinitions.FontStyle.BOLDITALIC;
}
else if (bold)
{
style = StyleTypeDefinitions.FontStyle.BOLD;
}
else if (italic)
{
style = StyleTypeDefinitions.FontStyle.ITALIC;
}
Font origfont = paragraph.getFont();
Font font = new Font(origfont.getFamilyName(), style, origfont.getSize());
paragraph.setFont(font);
Alignment align = para.getAlign();
switch(align)
{
case RIGHT :
paragraph.setHorizontalAlignment(HorizontalAlignmentType.RIGHT);
break;
case CENTER :
paragraph.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
break;
case JUSTIFIED :
paragraph.setHorizontalAlignment(HorizontalAlignmentType.JUSTIFY);
break;
default :
paragraph.setHorizontalAlignment(HorizontalAlignmentType.LEFT);
break;
}
}
@Override
public int calculateSize()
{
try
{
TextDocument doc = TextDocument.loadDocument(targetFile);
Iterator iter = doc.getParagraphIterator();
while (iter.hasNext())
{
Paragraph para = iter.next();
String content = para.getTextContent();
return content.length();
}
}
catch (Exception e)
{
new LntException("Error getting ODT file informations : " + targetFile.getAbsolutePath(), e);
}
return -1;
}
@Override
public List getParagraphs()
{
Map stylesMap = new HashMap<>();
try
{
OdfContentDom root = textDocument.getContentDom();
NodeList children = root.getChildNodes();
boolean found = false;
for (int i = 0; i < children.getLength(); i++)
{
NodeList children1 = children.item(i).getChildNodes();
for (int j = 0; j < children1.getLength(); j++)
{
Node node = children1.item(j);
if (node.getLocalName().equals("automatic-styles"))
{
found = true;
NodeList children2 = node.getChildNodes();
for (int k = 0; k < children2.getLength(); k++)
{
Node snode = children2.item(k);
NamedNodeMap attributes = snode.getAttributes();
String family = attributes.getNamedItem("style:family").getNodeValue();
if (family.equals("text"))
{
Boolean[] values = new Boolean[2];
values[0] = false;
values[1] = false;
String name = attributes.getNamedItem("style:name").getNodeValue();
NodeList children3 = snode.getChildNodes();
Node defnode = children3.item(0);
NamedNodeMap attributes1 = defnode.getAttributes();
Node weightNode = attributes1.getNamedItem("fo:font-weight");
if (weightNode != null)
{
String weight = weightNode.getNodeValue();
values[0] = "bold".equals(weight);
}
Node italicNode = attributes1.getNamedItem("fo:font-style");
if (italicNode != null)
{
String italic = italicNode.getNodeValue();
values[1] = "italic".equals(italic);
}
stylesMap.put(name, values);
}
}
}
}
if (found)
break;
}
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
List ret = new LinkedList<>();
Iterator iter = textDocument.getParagraphIterator();
while (iter.hasNext())
{
Paragraph para = iter.next();
DocWorkParagraph paragraph = new DocWorkParagraph();
try
{
boolean bold = false;
boolean italic = false;
// Font font = para.getFont();
// if (font != null)
// {
// FontStyle style = font.getFontStyle();
// switch( style)
// {
// case BOLD :
// bold = true;
// break;
// case ITALIC :
// italic = true;
// break;
// case BOLDITALIC :
// bold = true;
// italic = true;
// break;
// default :
// break;
// }
// }
NodeList nodes = para.getOdfElement().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++)
{
Node n = nodes.item(i);
if (n instanceof OdfTextSpan)
{
OdfTextSpan spanNode = (OdfTextSpan) n;
String stylename = spanNode.getStyleName();
if (stylename != null)
{
Boolean[] style = stylesMap.get(stylename);
if (style != null)
{
bold = style[0];
italic = style[1];
}
DocWorkSpan span = new DocWorkSpan(n.getTextContent(), bold, italic);
paragraph.add(span);
}
}
else
{
DocWorkSpan span = new DocWorkSpan(n.getTextContent(), false, false);
paragraph.add(span);
}
}
}
catch (NullPointerException e)
{
// nothing to do
}
HorizontalAlignmentType align = para.getHorizontalAlignment();
switch(align)
{
case RIGHT :
paragraph.setAlign(Alignment.RIGHT);
break;
case CENTER :
paragraph.setAlign(Alignment.CENTER);
break;
case JUSTIFY :
case FILLED :
paragraph.setAlign(Alignment.JUSTIFIED);
break;
default :
break;
}
ret.add(paragraph);
}
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy