Fix inconsistent whitespace/indents
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678188 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9fd1832753
commit
1c693babb8
@ -20,35 +20,35 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
|
||||
|
||||
public class XWPFLatentStyles {
|
||||
private CTLatentStyles latentStyles;
|
||||
protected XWPFStyles styles; //LatentStyle shall know styles
|
||||
private CTLatentStyles latentStyles;
|
||||
protected XWPFStyles styles; //LatentStyle shall know styles
|
||||
|
||||
protected XWPFLatentStyles(){
|
||||
}
|
||||
protected XWPFLatentStyles(){
|
||||
}
|
||||
|
||||
protected XWPFLatentStyles(CTLatentStyles latentStyles){
|
||||
this(latentStyles,null);
|
||||
}
|
||||
protected XWPFLatentStyles(CTLatentStyles latentStyles){
|
||||
this(latentStyles,null);
|
||||
}
|
||||
|
||||
protected XWPFLatentStyles(CTLatentStyles latentStyles, XWPFStyles styles) {
|
||||
this.latentStyles=latentStyles;
|
||||
this.styles=styles;
|
||||
}
|
||||
protected XWPFLatentStyles(CTLatentStyles latentStyles, XWPFStyles styles) {
|
||||
this.latentStyles=latentStyles;
|
||||
this.styles=styles;
|
||||
}
|
||||
|
||||
public int getNumberOfStyles() {
|
||||
return latentStyles.sizeOfLsdExceptionArray();
|
||||
}
|
||||
public int getNumberOfStyles() {
|
||||
return latentStyles.sizeOfLsdExceptionArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether specific LatentStyleID is a latentStyle
|
||||
*/
|
||||
/**
|
||||
* checks whether specific LatentStyleID is a latentStyle
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
protected boolean isLatentStyle(String latentStyleID){
|
||||
for ( CTLsdException lsd: latentStyles.getLsdExceptionArray()) {
|
||||
if(lsd.getName().equals(latentStyleID)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected boolean isLatentStyle(String latentStyleID){
|
||||
for ( CTLsdException lsd: latentStyles.getLsdExceptionArray()) {
|
||||
if(lsd.getName().equals(latentStyleID)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -59,54 +59,53 @@ public class XWPFStyles extends POIXMLDocumentPart{
|
||||
* @param part the package part holding the data of the styles,
|
||||
* @param rel the package relationship of type "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
|
||||
*/
|
||||
public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
|
||||
super(part, rel);
|
||||
}
|
||||
|
||||
public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
|
||||
super(part, rel);
|
||||
}
|
||||
/**
|
||||
* Construct XWPFStyles from scratch for a new document.
|
||||
*/
|
||||
public XWPFStyles() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct XWPFStyles from scratch for a new document.
|
||||
*/
|
||||
public XWPFStyles() {
|
||||
}
|
||||
/**
|
||||
* Read document
|
||||
*/
|
||||
@Override
|
||||
protected void onDocumentRead() throws IOException{
|
||||
StylesDocument stylesDoc;
|
||||
try {
|
||||
InputStream is = getPackagePart().getInputStream();
|
||||
stylesDoc = StylesDocument.Factory.parse(is);
|
||||
setStyles(stylesDoc.getStyles());
|
||||
latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
|
||||
} catch (XmlException e) {
|
||||
throw new POIXMLException("Unable to read styles", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read document
|
||||
*/
|
||||
@Override
|
||||
protected void onDocumentRead() throws IOException{
|
||||
StylesDocument stylesDoc;
|
||||
try {
|
||||
InputStream is = getPackagePart().getInputStream();
|
||||
stylesDoc = StylesDocument.Factory.parse(is);
|
||||
setStyles(stylesDoc.getStyles());
|
||||
latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
|
||||
} catch (XmlException e) {
|
||||
throw new POIXMLException("Unable to read styles", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void commit() throws IOException {
|
||||
if (ctStyles == null) {
|
||||
throw new IllegalStateException("Unable to write out styles that were never read in!");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void commit() throws IOException {
|
||||
if (ctStyles == null) {
|
||||
throw new IllegalStateException("Unable to write out styles that were never read in!");
|
||||
}
|
||||
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
|
||||
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
|
||||
map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
|
||||
xmlOptions.setSaveSuggestedPrefixes(map);
|
||||
PackagePart part = getPackagePart();
|
||||
OutputStream out = part.getOutputStream();
|
||||
ctStyles.save(out, xmlOptions);
|
||||
out.close();
|
||||
}
|
||||
|
||||
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
|
||||
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
|
||||
map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
|
||||
xmlOptions.setSaveSuggestedPrefixes(map);
|
||||
PackagePart part = getPackagePart();
|
||||
OutputStream out = part.getOutputStream();
|
||||
ctStyles.save(out, xmlOptions);
|
||||
out.close();
|
||||
}
|
||||
|
||||
protected void ensureDocDefaults() {
|
||||
// TODO Refactor from elsewhere
|
||||
}
|
||||
protected void ensureDocDefaults() {
|
||||
// TODO Refactor from elsewhere
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the ctStyles
|
||||
@ -114,204 +113,204 @@ public class XWPFStyles extends POIXMLDocumentPart{
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public void setStyles(CTStyles styles) {
|
||||
ctStyles = styles;
|
||||
ctStyles = styles;
|
||||
|
||||
// Build up all the style objects
|
||||
for(CTStyle style : ctStyles.getStyleArray()) {
|
||||
listStyle.add(new XWPFStyle(style, this));
|
||||
}
|
||||
// Build up all the style objects
|
||||
for(CTStyle style : ctStyles.getStyleArray()) {
|
||||
listStyle.add(new XWPFStyle(style, this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether style with styleID exist
|
||||
* @param styleID styleID of the Style in the style-Document
|
||||
* @return true if style exist, false if style not exist
|
||||
*/
|
||||
public boolean styleExist(String styleID){
|
||||
for (XWPFStyle style : listStyle) {
|
||||
if (style.getStyleId().equals(styleID))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* add a style to the document
|
||||
* @param style
|
||||
* @throws IOException
|
||||
*/
|
||||
public void addStyle(XWPFStyle style){
|
||||
listStyle.add(style);
|
||||
ctStyles.addNewStyle();
|
||||
int pos = ctStyles.sizeOfStyleArray() - 1;
|
||||
ctStyles.setStyleArray(pos, style.getCTStyle());
|
||||
}
|
||||
/**
|
||||
*get style by a styleID
|
||||
* @param styleID styleID of the searched style
|
||||
* @return style
|
||||
*/
|
||||
public XWPFStyle getStyle(String styleID){
|
||||
for (XWPFStyle style : listStyle) {
|
||||
if(style.getStyleId().equals(styleID))
|
||||
return style;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public int getNumberOfStyles() {
|
||||
return listStyle.size();
|
||||
}
|
||||
/**
|
||||
* checks whether style with styleID exist
|
||||
* @param styleID styleID of the Style in the style-Document
|
||||
* @return true if style exist, false if style not exist
|
||||
*/
|
||||
public boolean styleExist(String styleID){
|
||||
for (XWPFStyle style : listStyle) {
|
||||
if (style.getStyleId().equals(styleID))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* add a style to the document
|
||||
* @param style
|
||||
* @throws IOException
|
||||
*/
|
||||
public void addStyle(XWPFStyle style){
|
||||
listStyle.add(style);
|
||||
ctStyles.addNewStyle();
|
||||
int pos = ctStyles.sizeOfStyleArray() - 1;
|
||||
ctStyles.setStyleArray(pos, style.getCTStyle());
|
||||
}
|
||||
/**
|
||||
* Get style by a styleID
|
||||
* @param styleID styleID of the searched style
|
||||
* @return style
|
||||
*/
|
||||
public XWPFStyle getStyle(String styleID){
|
||||
for (XWPFStyle style : listStyle) {
|
||||
if(style.getStyleId().equals(styleID))
|
||||
return style;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public int getNumberOfStyles() {
|
||||
return listStyle.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the styles which are related to the parameter style and their relatives
|
||||
* this method can be used to copy all styles from one document to another document
|
||||
* @param style
|
||||
* @return a list of all styles which were used by this method
|
||||
*/
|
||||
public List<XWPFStyle> getUsedStyleList(XWPFStyle style){
|
||||
List<XWPFStyle> usedStyleList = new ArrayList<XWPFStyle>();
|
||||
usedStyleList.add(style);
|
||||
return getUsedStyleList(style, usedStyleList);
|
||||
}
|
||||
/**
|
||||
* get the styles which are related to the parameter style and their relatives
|
||||
* this method can be used to copy all styles from one document to another document
|
||||
* @param style
|
||||
* @return a list of all styles which were used by this method
|
||||
*/
|
||||
public List<XWPFStyle> getUsedStyleList(XWPFStyle style){
|
||||
List<XWPFStyle> usedStyleList = new ArrayList<XWPFStyle>();
|
||||
usedStyleList.add(style);
|
||||
return getUsedStyleList(style, usedStyleList);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the styles which are related to parameter style
|
||||
* @param style
|
||||
* @return all Styles of the parameterList
|
||||
*/
|
||||
private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){
|
||||
String basisStyleID = style.getBasisStyleID();
|
||||
XWPFStyle basisStyle = getStyle(basisStyleID);
|
||||
if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){
|
||||
usedStyleList.add(basisStyle);
|
||||
getUsedStyleList(basisStyle, usedStyleList);
|
||||
}
|
||||
String linkStyleID = style.getLinkStyleID();
|
||||
XWPFStyle linkStyle = getStyle(linkStyleID);
|
||||
if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){
|
||||
usedStyleList.add(linkStyle);
|
||||
getUsedStyleList(linkStyle, usedStyleList);
|
||||
}
|
||||
/**
|
||||
* get the styles which are related to parameter style
|
||||
* @param style
|
||||
* @return all Styles of the parameterList
|
||||
*/
|
||||
private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){
|
||||
String basisStyleID = style.getBasisStyleID();
|
||||
XWPFStyle basisStyle = getStyle(basisStyleID);
|
||||
if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){
|
||||
usedStyleList.add(basisStyle);
|
||||
getUsedStyleList(basisStyle, usedStyleList);
|
||||
}
|
||||
String linkStyleID = style.getLinkStyleID();
|
||||
XWPFStyle linkStyle = getStyle(linkStyleID);
|
||||
if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){
|
||||
usedStyleList.add(linkStyle);
|
||||
getUsedStyleList(linkStyle, usedStyleList);
|
||||
}
|
||||
|
||||
String nextStyleID = style.getNextStyleID();
|
||||
XWPFStyle nextStyle = getStyle(nextStyleID);
|
||||
if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){
|
||||
usedStyleList.add(linkStyle);
|
||||
getUsedStyleList(linkStyle, usedStyleList);
|
||||
}
|
||||
return usedStyleList;
|
||||
}
|
||||
String nextStyleID = style.getNextStyleID();
|
||||
XWPFStyle nextStyle = getStyle(nextStyleID);
|
||||
if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){
|
||||
usedStyleList.add(linkStyle);
|
||||
getUsedStyleList(linkStyle, usedStyleList);
|
||||
}
|
||||
return usedStyleList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default spelling language on ctStyles DocDefaults parameter
|
||||
* @param strSpellingLanguage
|
||||
*/
|
||||
public void setSpellingLanguage(String strSpellingLanguage) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
CTLanguage lang = null;
|
||||
/**
|
||||
* Sets the default spelling language on ctStyles DocDefaults parameter
|
||||
* @param strSpellingLanguage
|
||||
*/
|
||||
public void setSpellingLanguage(String strSpellingLanguage) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
CTLanguage lang = null;
|
||||
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
if(runProps.isSetLang())
|
||||
lang = runProps.getLang();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
if(runProps.isSetLang())
|
||||
lang = runProps.getLang();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
if(lang == null)
|
||||
lang = runProps.addNewLang();
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
if(lang == null)
|
||||
lang = runProps.addNewLang();
|
||||
|
||||
lang.setVal(strSpellingLanguage);
|
||||
lang.setBidi(strSpellingLanguage);
|
||||
}
|
||||
lang.setVal(strSpellingLanguage);
|
||||
lang.setBidi(strSpellingLanguage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default East Asia spelling language on ctStyles DocDefaults parameter
|
||||
* @param strEastAsia
|
||||
*/
|
||||
public void setEastAsia(String strEastAsia) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
CTLanguage lang = null;
|
||||
/**
|
||||
* Sets the default East Asia spelling language on ctStyles DocDefaults parameter
|
||||
* @param strEastAsia
|
||||
*/
|
||||
public void setEastAsia(String strEastAsia) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
CTLanguage lang = null;
|
||||
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
if(runProps.isSetLang())
|
||||
lang = runProps.getLang();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
if(runProps.isSetLang())
|
||||
lang = runProps.getLang();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
if(lang == null)
|
||||
lang = runProps.addNewLang();
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
if(lang == null)
|
||||
lang = runProps.addNewLang();
|
||||
|
||||
lang.setEastAsia(strEastAsia);
|
||||
}
|
||||
lang.setEastAsia(strEastAsia);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default font on ctStyles DocDefaults parameter
|
||||
* @param fonts
|
||||
*/
|
||||
public void setDefaultFonts(CTFonts fonts) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
/**
|
||||
* Sets the default font on ctStyles DocDefaults parameter
|
||||
* @param fonts
|
||||
*/
|
||||
public void setDefaultFonts(CTFonts fonts) {
|
||||
CTDocDefaults docDefaults = null;
|
||||
CTRPr runProps = null;
|
||||
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Just making sure we use the members that have already been defined
|
||||
if(ctStyles.isSetDocDefaults()) {
|
||||
docDefaults = ctStyles.getDocDefaults();
|
||||
if(docDefaults.isSetRPrDefault()) {
|
||||
CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
|
||||
if(RPrDefault.isSetRPr()) {
|
||||
runProps = RPrDefault.getRPr();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
if(docDefaults == null)
|
||||
docDefaults = ctStyles.addNewDocDefaults();
|
||||
if(runProps == null)
|
||||
runProps = docDefaults.addNewRPrDefault().addNewRPr();
|
||||
|
||||
runProps.setRFonts(fonts);
|
||||
}
|
||||
runProps.setRFonts(fonts);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the style with the same name
|
||||
* if this style is not existing, return null
|
||||
*/
|
||||
public XWPFStyle getStyleWithSameName(XWPFStyle style){
|
||||
for (XWPFStyle ownStyle : listStyle) {
|
||||
if(ownStyle.hasSameName(style)){
|
||||
return ownStyle;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* get the style with the same name
|
||||
* if this style is not existing, return null
|
||||
*/
|
||||
public XWPFStyle getStyleWithSameName(XWPFStyle style){
|
||||
for (XWPFStyle ownStyle : listStyle) {
|
||||
if(ownStyle.hasSameName(style)){
|
||||
return ownStyle;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default paragraph style which applies to the document
|
||||
*/
|
||||
public XWPFDefaultParagraphStyle getDefaultParagraphStyle() {
|
||||
return null; // TODO
|
||||
}
|
||||
/**
|
||||
* Get the default paragraph style which applies to the document
|
||||
*/
|
||||
public XWPFDefaultParagraphStyle getDefaultParagraphStyle() {
|
||||
return null; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the definition of all the Latent Styles
|
||||
|
Loading…
Reference in New Issue
Block a user