com.hfg.xml.msofficexml.docx.Docx Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.docx;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hfg.util.StringUtil;
import com.hfg.xml.msofficexml.OfficeOpenXMLContentType;
import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
import com.hfg.xml.msofficexml.RelationshipType;
import com.hfg.xml.msofficexml.docx.part.*;
import com.hfg.xml.msofficexml.docx.part.VbaRelationshipPart;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlBookmark;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlFooter;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlHeader;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
import com.hfg.xml.msofficexml.part.*;
//------------------------------------------------------------------------------
/**
Office Open XML format Word document.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// 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
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
/*
Testing notes:
- for zipping on the command-line: 'zip -r *'
- docProps/app.xml is not required as long as there is no rel entry
- docProps/thumbnail.jpeg is not required as long as there is no rel entry
*/
public class Docx extends OfficeOpenXmlDocument
{
private DocumentPart mDocumentPart;
private CommentsPart mCommentsPart;
private StylesPart mStylesPart;
private SettingsPart mSettingsPart;
private NumberingPart mNumberingPart;
private List mHeaderParts;
private List mFooterParts;
private VbaDataPart mVBA_DataPart;
private VbaRelationshipPart mVBA_RelationshipPart;
// For convenience, keep a list of bookmarks in the document.
private Map mBookmarkMap;
private static String sDefaultName = "Untitled.docx";
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public Docx()
{
super();
init();
}
//---------------------------------------------------------------------------
private void init()
{
// Setup essential relationships
getPackageRelationshipPart().addRelationship(RelationshipType.OFFICE_DOCUMENT, WmlXML.DOCUMENT_FILE);
mDocumentPart = new DocumentPart(this);
getContentTypesPart().addDefault(WmlContentType.PRINTER_SETTINGS);
getContentTypesPart().addOverride(mDocumentPart, WmlContentType.MAIN_DOCUMENT);
/*
getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/docProps/app.xml")), OfficeOpenXMLContentType.EXTENDED_PROPERTIES);
getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/footnotes.xml")), WmlContentType.FOOTNOTES);
getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/fontTable.xml")), WmlContentType.FONT_TABLE);
getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/webSettings.xml")), WmlContentType.WEB_SETTINGS);
*/
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
public static String getDefaultName()
{
return sDefaultName;
}
//---------------------------------------------------------------------------
@Override
public String name()
{
String name = super.name();
if (! StringUtil.isSet(name))
{
name = getDefaultName();
setName(name);
}
return name;
}
//---------------------------------------------------------------------------
/**
Convenience method for setting the default font for the document.
* @param inValue Font object to use as the default font-family, size, and style.
* @return the doument object to enable method chaining.
*/
public Docx setDefaultFont(Font inValue)
{
getStylesPart().getDocDefaults().getTextRunProperties().setFont(inValue);
return this;
}
//---------------------------------------------------------------------------
public SettingsPart getSettingsPart()
{
if (null == mSettingsPart)
{
mSettingsPart = new SettingsPart(this);
addPart(mSettingsPart);
getContentTypesPart().addOverride(mSettingsPart, WmlContentType.SETTINGS);
}
return mSettingsPart;
}
//---------------------------------------------------------------------------
public DocumentPart getDocumentPart()
{
return mDocumentPart;
}
//---------------------------------------------------------------------------
public StylesPart getStylesPart()
{
if (null == mStylesPart)
{
mStylesPart = new StylesPart(this);
addPart(mStylesPart);
getDocumentPart().getRelationshipPart().addRelationship(RelationshipType.STYLES, WmlXML.STYLES_FILE);
getContentTypesPart().addOverride(mStylesPart, WmlContentType.STYLES);
}
return mStylesPart;
}
//---------------------------------------------------------------------------
public NumberingPart getNumberingPart()
{
if (null == mNumberingPart)
{
mNumberingPart = new NumberingPart(this);
addPart(mNumberingPart);
getDocumentPart().getRelationshipPart().addRelationship(RelationshipType.NUMBERING, WmlXML.NUMBERING_FILE);
getContentTypesPart().addOverride(mNumberingPart, WmlContentType.NUMBERING);
}
return mNumberingPart;
}
//---------------------------------------------------------------------------
// The comments part will only be instantiated if requested.
public CommentsPart getCommentsPart()
{
if (null == mCommentsPart)
{
mCommentsPart = new CommentsPart(this);
mCommentsPart.setParentDoc(this);
addPart(mCommentsPart);
getPackageRelationshipPart().addRelationship(RelationshipType.COMMENTS, WmlXML.COMMENTS_FILE);
getContentTypesPart().addOverride(mCommentsPart, OfficeOpenXMLContentType.COMMENTS);
}
return mCommentsPart;
}
//---------------------------------------------------------------------------
public String addHeader(WmlHeader inValue)
{
HeaderPart headerPart = inValue.getPart();
if (null == mHeaderParts)
{
mHeaderParts = new ArrayList<>(5);
}
mHeaderParts.add(headerPart);
addPart(headerPart);
getContentTypesPart().addOverride(headerPart, OfficeOpenXMLContentType.HEADER);
return getDocumentPart().getRelationshipPart().addRelationship(RelationshipType.HEADER, headerPart.getFile());
}
//---------------------------------------------------------------------------
public String addFooter(WmlFooter inValue)
{
FooterPart footerPart = inValue.getPart();
if (null == mFooterParts)
{
mFooterParts = new ArrayList<>(5);
}
mFooterParts.add(footerPart);
addPart(footerPart);
getContentTypesPart().addOverride(footerPart, OfficeOpenXMLContentType.FOOTER);
return getDocumentPart().getRelationshipPart().addRelationship(RelationshipType.FOOTER, footerPart.getFile());
}
//---------------------------------------------------------------------------
/**
For convenience, bookmarks are automatically registered via this method so that
they can be retrieved via the getBookmark() method.
* @param inValue
*/
public void registerBookmark(WmlBookmark inValue)
{
if (null == mBookmarkMap)
{
mBookmarkMap = new HashMap<>(50);
}
mBookmarkMap.put(inValue.name(), inValue);
}
//---------------------------------------------------------------------------
public WmlBookmark getBookmark(String inName)
{
WmlBookmark bookmark = null;
if (mBookmarkMap != null)
{
bookmark = mBookmarkMap.get(inName);
}
return bookmark;
}
//---------------------------------------------------------------------------
public VbaDataPart getVBA_DataPart()
{
if (null == mVBA_DataPart)
{
mVBA_DataPart = new VbaDataPart(this);
getVBA_RelationshipPart().addRelationship(RelationshipType.WORD_VBA_DATA, WmlXML.VBA_DATA_FILE);
}
return mVBA_DataPart;
}
//---------------------------------------------------------------------------
public VbaRelationshipPart getVBA_RelationshipPart()
{
if (null == mVBA_RelationshipPart)
{
mVBA_RelationshipPart = new VbaRelationshipPart(this);
}
return mVBA_RelationshipPart;
}
}