import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
+import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
return ExcelToHtmlUtils.isEmpty( value ) && cellStyleIndex == 0;
}
+ protected void processDocumentInformation(
+ SummaryInformation summaryInformation )
+ {
+ if ( ExcelToHtmlUtils.isNotEmpty( summaryInformation.getTitle() ) )
+ htmlDocumentFacade.setTitle( summaryInformation.getTitle() );
+
+ if ( ExcelToHtmlUtils.isNotEmpty( summaryInformation.getAuthor() ) )
+ htmlDocumentFacade.addAuthor( summaryInformation.getAuthor() );
+
+ if ( ExcelToHtmlUtils.isNotEmpty( summaryInformation.getKeywords() ) )
+ htmlDocumentFacade.addKeywords( summaryInformation.getKeywords() );
+
+ if ( ExcelToHtmlUtils.isNotEmpty( summaryInformation.getComments() ) )
+ htmlDocumentFacade
+ .addDescription( summaryInformation.getComments() );
+ }
+
protected boolean processRow( HSSFRow row, Element tableRowElement )
{
boolean emptyRow = true;
public void processWorkbook( HSSFWorkbook workbook )
{
+ final SummaryInformation summaryInformation = workbook
+ .getSummaryInformation();
+ if ( summaryInformation != null )
+ {
+ processDocumentInformation( summaryInformation );
+ }
+
for ( short i = 0; i < workbook.getNumCellStyles(); i++ )
{
HSSFCellStyle cellStyle = workbook.getCellStyleAt( i );
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.model.ListFormatOverride;
public void processDocument( HWPFDocumentCore wordDocument )
{
+ final SummaryInformation summaryInformation = wordDocument
+ .getSummaryInformation();
+ if ( summaryInformation != null )
+ {
+ processDocumentInformation( summaryInformation );
+ }
+
final Range range = wordDocument.getRange();
for ( int s = 0; s < range.numSections(); s++ )
{
}
}
+ protected abstract void processDocumentInformation(
+ SummaryInformation summaryInformation );
+
protected void processField( HWPFDocumentCore wordDocument,
Element currentBlock, Paragraph paragraph, int currentTableLevel,
List<CharacterRun> characterRuns, int beginMark, int separatorMark,
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class FoDocumentFacade
{
+ private static final String NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+
private static final String NS_XSLFO = "http://www.w3.org/1999/XSL/Format";
+ protected final Element declarations;
protected final Document document;
protected final Element layoutMasterSet;
+ protected Element propertiesRoot;
protected final Element root;
public FoDocumentFacade( Document document )
layoutMasterSet = document.createElementNS( NS_XSLFO,
"fo:layout-master-set" );
root.appendChild( layoutMasterSet );
+
+ declarations = document.createElementNS( NS_XSLFO, "fo:declarations" );
+ root.appendChild( declarations );
}
public Element addFlowToPageSequence( final Element pageSequence,
return document;
}
+ protected Element getOrCreatePropertiesRoot()
+ {
+ if ( propertiesRoot != null )
+ return propertiesRoot;
+
+ // See http://xmlgraphics.apache.org/fop/0.95/metadata.html
+
+ Element xmpmeta = document.createElementNS( "adobe:ns:meta",
+ "x:xmpmeta" );
+ declarations.appendChild( xmpmeta );
+
+ Element rdf = document.createElementNS( NS_RDF, "rdf:RDF" );
+ xmpmeta.appendChild( rdf );
+
+ propertiesRoot = document.createElementNS( NS_RDF, "rdf:Description" );
+ rdf.appendChild( propertiesRoot );
+
+ return propertiesRoot;
+ }
+
+ public void setCreator( String value )
+ {
+ setDublinCoreProperty( "creator", value );
+ }
+
+ public void setCreatorTool( String value )
+ {
+ setXmpProperty( "CreatorTool", value );
+ }
+
+ public void setDescription( String value )
+ {
+ Element element = setDublinCoreProperty( "description", value );
+
+ if ( element != null )
+ {
+ element.setAttributeNS( "http://www.w3.org/XML/1998/namespace",
+ "xml:lang", "x-default" );
+ }
+ }
+
+ public Element setDublinCoreProperty( String name, String value )
+ {
+ return setProperty( "http://purl.org/dc/elements/1.1/", "dc", name,
+ value );
+ }
+
+ public void setKeywords( String value )
+ {
+ setPdfProperty( "Keywords", value );
+ }
+
+ public Element setPdfProperty( String name, String value )
+ {
+ return setProperty( "http://ns.adobe.com/pdf/1.3/", "pdf", name, value );
+ }
+
+ public void setProducer( String value )
+ {
+ setPdfProperty( "Producer", value );
+ }
+
+ protected Element setProperty( String namespace, String prefix,
+ String name, String value )
+ {
+ Element propertiesRoot = getOrCreatePropertiesRoot();
+ NodeList existingChildren = propertiesRoot.getChildNodes();
+ for ( int i = 0; i < existingChildren.getLength(); i++ )
+ {
+ Node child = existingChildren.item( i );
+ if ( child.getNodeType() == Node.ELEMENT_NODE )
+ {
+ Element childElement = (Element) child;
+ if ( WordToFoUtils.isNotEmpty( childElement.getNamespaceURI() )
+ && WordToFoUtils.isNotEmpty( childElement
+ .getLocalName() )
+ && namespace.equals( childElement.getNamespaceURI() )
+ && name.equals( childElement.getLocalName() ) )
+ {
+ propertiesRoot.removeChild( childElement );
+ break;
+ }
+ }
+ }
+
+ if ( WordToFoUtils.isNotEmpty( value ) )
+ {
+ Element property = document.createElementNS( namespace, prefix
+ + ":" + name );
+ property.appendChild( document.createTextNode( value ) );
+ propertiesRoot.appendChild( property );
+ return property;
+ }
+
+ return null;
+ }
+
+ public void setSubject( String value )
+ {
+ setDublinCoreProperty( "title", value );
+ }
+
+ public void setTitle( String value )
+ {
+ setDublinCoreProperty( "title", value );
+ }
+
+ public Element setXmpProperty( String name, String value )
+ {
+ return setProperty( "http://ns.adobe.com/xap/1.0/", "xmp", name, value );
+ }
+
}
protected final Element head;
protected final Element html;
+ protected Element title;
+ protected Text titleText;
+
public HtmlDocumentFacade( Document document )
{
this.document = document;
html.appendChild( body );
}
+ public void addAuthor( String value )
+ {
+ addMeta( "author", value );
+ }
+
+ public void addDescription( String value )
+ {
+ addMeta( "description", value );
+ }
+
+ public void addKeywords( String value )
+ {
+ addMeta( "keywords", value );
+ }
+
+ public void addMeta( final String name, String value )
+ {
+ Element meta = document.createElement( "meta" );
+ meta.setAttribute( "name", name );
+ meta.setAttribute( "content", value );
+ head.appendChild( meta );
+ }
+
public Element createHeader1()
{
return document.createElement( "h1" );
return head;
}
+ public String getTitle()
+ {
+ if ( title == null )
+ return null;
+
+ return titleText.getTextContent();
+ }
+
+ public void setTitle( String titleText )
+ {
+ if ( WordToHtmlUtils.isEmpty( titleText ) && this.title != null )
+ {
+ this.head.removeChild( this.title );
+ this.title = null;
+ this.titleText = null;
+ }
+
+ if ( this.title == null )
+ {
+ this.title = document.createElement( "title" );
+ this.titleText = document.createTextNode( titleText );
+ this.title.appendChild( this.titleText );
+ this.head.appendChild( title );
+ }
+
+ this.titleText.setData( titleText );
+ }
}
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
+import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.usermodel.CharacterRun;
inline.appendChild( textNode );
}
+ @Override
+ protected void processDocumentInformation(
+ SummaryInformation summaryInformation )
+ {
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getTitle() ) )
+ foDocumentFacade.setTitle( summaryInformation.getTitle() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getAuthor() ) )
+ foDocumentFacade.setCreator( summaryInformation.getAuthor() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getKeywords() ) )
+ foDocumentFacade.setKeywords( summaryInformation.getKeywords() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getComments() ) )
+ foDocumentFacade.setDescription( summaryInformation.getComments() );
+ }
+
protected void processHyperlink( HWPFDocumentCore hwpfDocument,
Element currentBlock, Paragraph paragraph,
List<CharacterRun> characterRuns, int currentTableLevel,
Element flow = foDocumentFacade.addFlowToPageSequence( pageSequence,
"xsl-region-body" );
- processSectionParagraphes( wordDocument, flow, section, Integer.MIN_VALUE );
+ processSectionParagraphes( wordDocument, flow, section,
+ Integer.MIN_VALUE );
}
protected void processTable( HWPFDocumentCore wordDocument, Element flow,
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
+import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.usermodel.CharacterRun;
span.appendChild( textNode );
}
+ @Override
+ protected void processDocumentInformation(
+ SummaryInformation summaryInformation )
+ {
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getTitle() ) )
+ htmlDocumentFacade.setTitle( summaryInformation.getTitle() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getAuthor() ) )
+ htmlDocumentFacade.addAuthor( summaryInformation.getAuthor() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getKeywords() ) )
+ htmlDocumentFacade.addKeywords( summaryInformation.getKeywords() );
+
+ if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getComments() ) )
+ htmlDocumentFacade
+ .addDescription( summaryInformation.getComments() );
+ }
+
protected void processHyperlink( HWPFDocumentCore wordDocument,
Element currentBlock, Paragraph paragraph,
List<CharacterRun> characterRuns, int currentTableLevel,
div.setAttribute( "style", getSectionStyle( section ) );
htmlDocumentFacade.body.appendChild( div );
- processSectionParagraphes( wordDocument, div, section, Integer.MIN_VALUE );
+ processSectionParagraphes( wordDocument, div, section,
+ Integer.MIN_VALUE );
}
@Override
return result;
}
+ public void testDocumentProperties() throws Exception
+ {
+ String result = getFoText( "documentProperties.doc" );
+
+ assertTrue( result
+ .contains( "<dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">This is document title</dc:title>" ) );
+ assertTrue( result
+ .contains( "<pdf:Keywords xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">This is document keywords</pdf:Keywords>" ) );
+ }
+
public void testEquation() throws Exception
{
final String sampleFileName = "equation.doc";
import org.apache.poi.hwpf.HWPFDocument;
/**
- * Test cases for {@link WordToFoConverter}
+ * Test cases for {@link WordToHtmlConverter}
*
* @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
*/
assertTrue( result.contains( "<table>" ) );
}
+ public void testDocumentProperties() throws Exception
+ {
+ String result = getHtmlText( "documentProperties.doc" );
+
+ assertTrue( result.contains( "<title>This is document title</title>" ) );
+ assertTrue( result
+ .contains( "<meta content=\"This is document keywords\" name=\"keywords\">" ) );
+ }
+
public void testEquation() throws Exception
{
String result = getHtmlText( "equation.doc" );