All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.model.styles.StyleUtil Maven / Gradle / Ivy

Go to download

docx4j is a library which helps you to work with the Office Open XML file format as used in docx documents, pptx presentations, and xlsx spreadsheets.

There is a newer version: 6.1.2
Show newest version
/*
   Licensed to Plutext Pty Ltd under one or more contributor license agreements.  
   
 *  This file is part of docx4j.

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

        http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.

 */
package org.docx4j.model.styles;

import java.math.BigInteger;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.sharedtypes.STOnOff;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTBorder;
import org.docx4j.wml.CTCnf;
import org.docx4j.wml.CTEm;
import org.docx4j.wml.CTFramePr;
import org.docx4j.wml.CTHeight;
import org.docx4j.wml.CTLanguage;
import org.docx4j.wml.CTShd;
import org.docx4j.wml.CTShortHexNumber;
import org.docx4j.wml.CTSignedHpsMeasure;
import org.docx4j.wml.CTSignedTwipsMeasure;
import org.docx4j.wml.CTTabStop;
import org.docx4j.wml.CTTblCellMar;
import org.docx4j.wml.CTTblLayoutType;
import org.docx4j.wml.CTTblLook;
import org.docx4j.wml.CTTblOverlap;
import org.docx4j.wml.CTTblPPr;
import org.docx4j.wml.CTTblPrBase;
import org.docx4j.wml.CTTblPrBase.TblStyle;
import org.docx4j.wml.CTTblPrBase.TblStyleColBandSize;
import org.docx4j.wml.CTTblPrBase.TblStyleRowBandSize;
import org.docx4j.wml.CTTblStylePr;
import org.docx4j.wml.CTTextEffect;
import org.docx4j.wml.CTTextScale;
import org.docx4j.wml.CTTextboxTightWrap;
import org.docx4j.wml.CTTrPrBase.GridAfter;
import org.docx4j.wml.CTTrPrBase.GridBefore;
import org.docx4j.wml.CTVerticalAlignRun;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.Color;
import org.docx4j.wml.Highlight;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.Jc;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase;
import org.docx4j.wml.PPrBase.Ind;
import org.docx4j.wml.PPrBase.NumPr;
import org.docx4j.wml.PPrBase.OutlineLvl;
import org.docx4j.wml.PPrBase.PBdr;
import org.docx4j.wml.PPrBase.PStyle;
import org.docx4j.wml.PPrBase.Spacing;
import org.docx4j.wml.PPrBase.TextAlignment;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.RStyle;
import org.docx4j.wml.STBorder;
import org.docx4j.wml.STDropCap;
import org.docx4j.wml.STEm;
import org.docx4j.wml.STHAnchor;
import org.docx4j.wml.STHeightRule;
import org.docx4j.wml.STHint;
import org.docx4j.wml.STShd;
import org.docx4j.wml.STTblLayoutType;
import org.docx4j.wml.STTblOverlap;
import org.docx4j.wml.STTblStyleOverrideType;
import org.docx4j.wml.STTextEffect;
import org.docx4j.wml.STTheme;
import org.docx4j.wml.STThemeColor;
import org.docx4j.wml.STVAnchor;
import org.docx4j.wml.STVerticalAlignRun;
import org.docx4j.wml.STVerticalJc;
import org.docx4j.wml.STWrap;
import org.docx4j.wml.STXAlign;
import org.docx4j.wml.STYAlign;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.Style;
import org.docx4j.wml.Style.BasedOn;
import org.docx4j.wml.Tabs;
import org.docx4j.wml.TblBorders;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.TcMar;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TcPrInner.GridSpan;
import org.docx4j.wml.TcPrInner.HMerge;
import org.docx4j.wml.TcPrInner.TcBorders;
import org.docx4j.wml.TcPrInner.VMerge;
import org.docx4j.wml.TextDirection;
import org.docx4j.wml.TrPr;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 *  @author Alberto Zerolo
 *  @since 3.0.0
 *  
*/
public class StyleUtil {
	
	protected static Logger log = LoggerFactory.getLogger(StyleUtil.class);	
	
	
	public static final String CHARACTER_STYLE = "character";
	public static final String PARAGRAPH_STYLE = "paragraph";
	public static final String TABLE_STYLE = "table";
	public static final String NUMBERING_STYLE = "numbering";

/////////////////////////////////////////////
// areEqual-Methods
/////////////////////////////////////////////
	public static boolean areEqual(Style style1, Style style2, boolean compareIDs) {
		if (style1 == style2) 
			return true;
		if (((style1 != null) && (style2 == null)) || ((style1 == null) && (style2 != null)))
			return false;
		
		if ((compareIDs && (!areEqual(style1.getStyleId(), style2.getStyleId()))) ||
			(!areEqual(style1.getType(), style2.getType()))) {
			return false;
		}
		if (CHARACTER_STYLE.equals(style1.getType())) {
			return areEqual(style1.getBasedOn(), style2.getBasedOn()) && 
			       areEqual(style1.getRPr(), style2.getRPr());
		}
		else if (PARAGRAPH_STYLE.equals(style1.getType()) || 
				 NUMBERING_STYLE.equals(style1.getType())) {
			return areEqual(style1.getBasedOn(), style2.getBasedOn()) &&
				   areEqual(style1.getRPr(), style2.getRPr()) &&
			       areEqual(style1.getPPr(), style2.getPPr());
		}
		else if (TABLE_STYLE.equals(style1.getType())) {
			return areEqual(style1.getBasedOn(), style2.getBasedOn()) &&
			       areEqual(style1.getTblPr(), style2.getTblPr()) &&
				   areEqual(style1.getTcPr(), style2.getTcPr()) &&
				   areEqual(style1.getTblStylePr(), style2.getTblStylePr());
			
		}
		throw new IllegalArgumentException("Invalid style type: " + style1.getType());
	}

	public static boolean areEqual(PPr pPr1, PPr pPr2) {
		return (pPr1 == pPr2) ||
			((pPr1 != null) && (pPr2 != null) &&
			  areEqual((PPrBase)pPr1, (PPrBase)pPr2) &&
			  areEqual(pPr1.getRPr(), pPr2.getRPr()) &&
			  areEqual(pPr1.getSectPr(), pPr2.getSectPr())			  
			);
	}
	
	/**
	 * @param sectPr1
	 * @param sectPr2
	 * @return
	 * @since 3.2
	 */
	public static boolean areEqual(SectPr sectPr1, SectPr sectPr2) {

		log.warn("TODO: implementation is incomplete; contributions welcome");						
		
		return (sectPr1 == sectPr2) ||
				((sectPr1 != null) && (sectPr2 != null) &&	
						areEqual(sectPr1.getFormProt() , sectPr2.getFormProt()) &&
						areEqual(sectPr1.getVAlign() , sectPr2.getVAlign()) &&
						areEqual(sectPr1.getNoEndnote(), sectPr2.getNoEndnote()) &&
						areEqual(sectPr1.getTitlePg() , sectPr2.getTitlePg()) &&
						areEqual(sectPr1.getTextDirection(), sectPr2.getTextDirection()) &&
						areEqual(sectPr1.getBidi(), sectPr2.getBidi()) &&
						areEqual(sectPr1.getRtlGutter(), sectPr2.getRtlGutter()) 
						
// TODO						
//						areEqual(sectPr1.getDocGrid() , sectPr2.getDocGrid()) &&
//						areEqual(sectPr1.getEGHdrFtrReferences(), sectPr2.getEGHdrFtrReferences()) &&
//						areEqual(sectPr1.getFootnotePr(), sectPr2.getFootnotePr()) &&
//						areEqual(sectPr1.getEndnotePr(), sectPr2.getEndnotePr()) &&
//						areEqual(sectPr1.getType() , sectPr2.getType()) &&
//						areEqual(sectPr1.getPgSz() , sectPr2.getPgSz()) &&
//						areEqual(sectPr1.getPgMar(), sectPr2.getPgMar()) &&
//						areEqual(sectPr1.getPaperSrc(), sectPr2.getPaperSrc()) &&
//						areEqual(sectPr1.getPgBorders() , sectPr2.getPgBorders()) &&
//						areEqual(sectPr1.getLnNumType(), sectPr2.getLnNumType()) &&
//						areEqual(sectPr1.getPgNumType(), sectPr2.getPgNumType()) &&
//						areEqual(sectPr1.getCols() , sectPr2.getCols()) &&
//						areEqual(sectPr1.getPrinterSettings(), sectPr2.getPrinterSettings()) &&
//						areEqual(sectPr1.getFootnoteColumns(), sectPr2.getFootnoteColumns()) 
					  
					);
												
	}

	public static boolean areEqual(PPrBase pPrBase1, PPrBase pPrBase2) {
		return (pPrBase1 == pPrBase2) ||
			(   (pPrBase1 != null) && (pPrBase2 != null) &&
				areEqual(pPrBase1.getPStyle(), pPrBase2.getPStyle()) &&	
				areEqual(pPrBase1.getKeepNext(), pPrBase2.getKeepNext()) &&	
				areEqual(pPrBase1.getKeepLines(), pPrBase2.getKeepLines()) &&	
				areEqual(pPrBase1.getPageBreakBefore(), pPrBase2.getPageBreakBefore()) &&	
				areEqual(pPrBase1.getFramePr(), pPrBase2.getFramePr()) &&	
				areEqual(pPrBase1.getWidowControl(), pPrBase2.getWidowControl()) &&	
				areEqual(pPrBase1.getNumPr(), pPrBase2.getNumPr()) &&	
				areEqual(pPrBase1.getSuppressLineNumbers(), pPrBase2.getSuppressLineNumbers()) &&	
				areEqual(pPrBase1.getPBdr(), pPrBase2.getPBdr()) &&	
				areEqual(pPrBase1.getShd(), pPrBase2.getShd()) &&	
				areEqual(pPrBase1.getTabs(), pPrBase2.getTabs()) &&	
				areEqual(pPrBase1.getSuppressAutoHyphens(), pPrBase2.getSuppressAutoHyphens()) &&	
				areEqual(pPrBase1.getKinsoku(), pPrBase2.getKinsoku()) &&	
				areEqual(pPrBase1.getWordWrap(), pPrBase2.getWordWrap()) &&	
				areEqual(pPrBase1.getOverflowPunct(), pPrBase2.getOverflowPunct()) &&	
				areEqual(pPrBase1.getTopLinePunct(), pPrBase2.getTopLinePunct()) &&	
				areEqual(pPrBase1.getAutoSpaceDE(), pPrBase2.getAutoSpaceDE()) &&	
				areEqual(pPrBase1.getAutoSpaceDN(), pPrBase2.getAutoSpaceDN()) &&	
				areEqual(pPrBase1.getBidi(), pPrBase2.getBidi()) &&	
				areEqual(pPrBase1.getAdjustRightInd(), pPrBase2.getAdjustRightInd()) &&	
				areEqual(pPrBase1.getSnapToGrid(), pPrBase2.getSnapToGrid()) &&	
				areEqual(pPrBase1.getSpacing(), pPrBase2.getSpacing()) &&	
				areEqual(pPrBase1.getInd(), pPrBase2.getInd()) &&	
				areEqual(pPrBase1.getContextualSpacing(), pPrBase2.getContextualSpacing()) &&	
				areEqual(pPrBase1.getMirrorIndents(), pPrBase2.getMirrorIndents()) &&	
				areEqual(pPrBase1.getSuppressOverlap(), pPrBase2.getSuppressOverlap()) &&	
				areEqual(pPrBase1.getJc(), pPrBase2.getJc()) &&	
				areEqual(pPrBase1.getTextDirection(), pPrBase2.getTextDirection()) &&	
				areEqual(pPrBase1.getTextAlignment(), pPrBase2.getTextAlignment()) &&	
				areEqual(pPrBase1.getTextboxTightWrap(), pPrBase2.getTextboxTightWrap()) &&	
				areEqual(pPrBase1.getOutlineLvl(), pPrBase2.getOutlineLvl()) &&
				areEqual(pPrBase1.getCnfStyle(), pPrBase2.getCnfStyle())	
			);
	}

	public static boolean areEqual(RPr rPr1, RPr rPr2) {
		return ((rPr1 == rPr2) ||
				((rPr1 != null) && (rPr2 != null) &&
				 areEqual(rPr1.getRStyle(), rPr2.getRStyle()) &&
				 areEqual(rPr1.getRFonts(), rPr2.getRFonts()) &&
				 areEqual(rPr1.getB(), rPr2.getB()) &&
				 areEqual(rPr1.getBCs(), rPr2.getBCs()) &&
				 areEqual(rPr1.getI(), rPr2.getI()) &&
				 areEqual(rPr1.getICs(), rPr2.getICs()) &&
				 areEqual(rPr1.getCaps(), rPr2.getCaps()) &&
				 areEqual(rPr1.getSmallCaps(), rPr2.getSmallCaps()) &&
				 areEqual(rPr1.getStrike(), rPr2.getStrike()) &&
				 areEqual(rPr1.getDstrike(), rPr2.getDstrike()) &&
				 areEqual(rPr1.getOutline(), rPr2.getOutline()) &&
				 areEqual(rPr1.getShadow(), rPr2.getShadow()) &&
				 areEqual(rPr1.getEmboss(), rPr2.getEmboss()) &&
				 areEqual(rPr1.getImprint(), rPr2.getImprint()) &&
				 areEqual(rPr1.getSnapToGrid(), rPr2.getSnapToGrid()) &&
				 areEqual(rPr1.getVanish(), rPr2.getVanish()) &&
				 areEqual(rPr1.getColor(), rPr2.getColor()) &&
			 	 areEqual(rPr1.getSpacing(), rPr2.getSpacing()) &&
				 areEqual(rPr1.getW(), rPr2.getW()) &&
				 areEqual(rPr1.getKern(), rPr2.getKern()) &&
				 areEqual(rPr1.getPosition(), rPr2.getPosition()) &&
				 areEqual(rPr1.getSz(), rPr2.getSz()) &&
				 areEqual(rPr1.getSzCs(), rPr2.getSzCs()) &&
				 areEqual(rPr1.getHighlight(), rPr2.getHighlight()) &&
				 areEqual(rPr1.getU(), rPr2.getU()) &&
				 areEqual(rPr1.getEffect(), rPr2.getEffect()) &&
				 areEqual(rPr1.getBdr(), rPr2.getBdr()) &&
				 areEqual(rPr1.getShd(), rPr2.getShd()) &&
				 areEqual(rPr1.getVertAlign(), rPr2.getVertAlign()) &&
				 areEqual(rPr1.getRtl(), rPr2.getRtl()) &&
				 areEqual(rPr1.getCs(), rPr2.getCs()) &&
				 areEqual(rPr1.getEm(), rPr2.getEm()) &&
				 areEqual(rPr1.getSpecVanish(), rPr2.getSpecVanish()) &&
				 areEqual(rPr1.getOMath(), rPr2.getOMath())
				 
				 // rPr1.getLang()  ??
				 )
			    );
	}

	public static boolean areEqual(ParaRPr rPr1, ParaRPr rPr2) {
		return ((rPr1 == rPr2) ||
				((rPr1 != null) && (rPr2 != null) &&
				 areEqual(rPr1.getRStyle(), rPr2.getRStyle()) &&
				 areEqual(rPr1.getRFonts(), rPr2.getRFonts()) &&
				 areEqual(rPr1.getB(), rPr2.getB()) &&
				 areEqual(rPr1.getBCs(), rPr2.getBCs()) &&
				 areEqual(rPr1.getI(), rPr2.getI()) &&
				 areEqual(rPr1.getICs(), rPr2.getICs()) &&
				 areEqual(rPr1.getCaps(), rPr2.getCaps()) &&
				 areEqual(rPr1.getSmallCaps(), rPr2.getSmallCaps()) &&
				 areEqual(rPr1.getStrike(), rPr2.getStrike()) &&
				 areEqual(rPr1.getDstrike(), rPr2.getDstrike()) &&
				 areEqual(rPr1.getOutline(), rPr2.getOutline()) &&
				 areEqual(rPr1.getShadow(), rPr2.getShadow()) &&
				 areEqual(rPr1.getEmboss(), rPr2.getEmboss()) &&
				 areEqual(rPr1.getImprint(), rPr2.getImprint()) &&
				 areEqual(rPr1.getSnapToGrid(), rPr2.getSnapToGrid()) &&
				 areEqual(rPr1.getVanish(), rPr2.getVanish()) &&
				 areEqual(rPr1.getColor(), rPr2.getColor()) &&
			 	 areEqual(rPr1.getSpacing(), rPr2.getSpacing()) &&
				 areEqual(rPr1.getW(), rPr2.getW()) &&
				 areEqual(rPr1.getKern(), rPr2.getKern()) &&
				 areEqual(rPr1.getPosition(), rPr2.getPosition()) &&
				 areEqual(rPr1.getSz(), rPr2.getSz()) &&
				 areEqual(rPr1.getSzCs(), rPr2.getSzCs()) &&
				 areEqual(rPr1.getHighlight(), rPr2.getHighlight()) &&
				 areEqual(rPr1.getU(), rPr2.getU()) &&
				 areEqual(rPr1.getEffect(), rPr2.getEffect()) &&
				 areEqual(rPr1.getBdr(), rPr2.getBdr()) &&
				 areEqual(rPr1.getShd(), rPr2.getShd()) &&
				 areEqual(rPr1.getVertAlign(), rPr2.getVertAlign()) &&
				 areEqual(rPr1.getRtl(), rPr2.getRtl()) &&
				 areEqual(rPr1.getCs(), rPr2.getCs()) &&
				 areEqual(rPr1.getEm(), rPr2.getEm()) &&
				 areEqual(rPr1.getSpecVanish(), rPr2.getSpecVanish()) &&
				 areEqual(rPr1.getOMath(), rPr2.getOMath())
				 )
			    );
	}

	public static boolean areEqual(CTTblPrBase tblPr1, CTTblPrBase tblPr2) {
		return ((tblPr1 == tblPr2) ||
				((tblPr1 != null) && (tblPr2 != null) &&
				 areEqual(tblPr1.getTblStyle(), tblPr2.getTblStyle()) &&
				 areEqual(tblPr1.getTblpPr(), tblPr2.getTblpPr()) &&
				 areEqual(tblPr1.getTblOverlap(), tblPr2.getTblOverlap()) &&
				 areEqual(tblPr1.getTblStyleRowBandSize(), tblPr2.getTblStyleRowBandSize()) &&
				 areEqual(tblPr1.getTblStyleColBandSize(), tblPr2.getTblStyleColBandSize()) &&
				 areEqual(tblPr1.getTblW(), tblPr2.getTblW()) &&
				 areEqual(tblPr1.getJc(), tblPr2.getJc()) &&
				 areEqual(tblPr1.getTblCellSpacing(), tblPr2.getTblCellSpacing()) &&
				 areEqual(tblPr1.getTblInd(), tblPr2.getTblInd()) &&
				 areEqual(tblPr1.getTblBorders(), tblPr2.getTblBorders()) &&
				 areEqual(tblPr1.getShd(), tblPr2.getShd()) &&
				 areEqual(tblPr1.getTblLayout(), tblPr2.getTblLayout()) &&
				 areEqual(tblPr1.getTblCellMar(), tblPr2.getTblCellMar()) &&
				 areEqual(tblPr1.getTblLook(), tblPr2.getTblLook())
				)
			   );
	}

	public static boolean areEqual(CTTblLook tblLook1, CTTblLook tblLook2) {
		return ((tblLook1 == tblLook2) || 
				((tblLook1 !=null) && (tblLook2 !=null) &&
						areEqual(tblLook1.getVal(), tblLook2.getVal()) &&
						areEqual(tblLook1.getFirstColumn(), tblLook2.getFirstColumn()) &&
						areEqual(tblLook1.getFirstRow(), tblLook2.getFirstRow()) &&
						areEqual(tblLook1.getLastColumn(), tblLook2.getLastColumn()) &&
						areEqual(tblLook1.getLastRow(), tblLook2.getLastRow()) &&
						areEqual(tblLook1.getNoHBand(), tblLook2.getNoHBand()) &&
						areEqual(tblLook1.getNoVBand(), tblLook2.getNoVBand())
						)
						);
	}
						
	
	private static boolean areEqual(STOnOff oo1, STOnOff oo2) {
		return (oo1 == oo2);
	}

	public static boolean areEqual(TcPr tcPr1, TcPr tcPr2) {
		return ((tcPr1 == tcPr2) ||
				((tcPr1 != null) && (tcPr2 != null) &&
				 areEqual(tcPr1.getCnfStyle(), tcPr2.getCnfStyle()) &&
				 areEqual(tcPr1.getTcW(), tcPr2.getTcW()) &&
				 areEqual(tcPr1.getGridSpan(), tcPr2.getGridSpan()) &&
				 areEqual(tcPr1.getHMerge(), tcPr2.getHMerge()) &&
				 areEqual(tcPr1.getVMerge(), tcPr2.getVMerge()) &&
				 areEqual(tcPr1.getTcBorders(), tcPr2.getTcBorders()) &&
				 areEqual(tcPr1.getShd(), tcPr2.getShd()) &&
				 areEqual(tcPr1.getNoWrap(), tcPr2.getNoWrap()) &&
				 areEqual(tcPr1.getTcMar(), tcPr2.getTcMar()) &&
				 areEqual(tcPr1.getTextDirection(), tcPr2.getTextDirection()) &&
				 areEqual(tcPr1.getTcFitText(), tcPr2.getTcFitText()) &&
				 areEqual(tcPr1.getVAlign(), tcPr2.getVAlign()) &&
				 areEqual(tcPr1.getHideMark(), tcPr2.getHideMark())
				)
			   );
	}

	public static boolean areEqual(List tblStylePrList1, List tblStylePrList2) {
		if (tblStylePrList1 == tblStylePrList2)
			return true;
		if (((tblStylePrList1 == null) && (tblStylePrList2 != null)) || ((tblStylePrList1 != null) && (tblStylePrList2 == null)))
			return false;
		if (tblStylePrList1.size() != tblStylePrList2.size())
			return false;
		
		for (int i=0; i> defs1 = null;
	List> defs2 = null;
	JAXBElement defs1element = null;
	Object defs2value = null;
	QName qName = null;
	String qNameLocal = null;
		if (trPr1 == trPr2)
			return true;
		if (((trPr1 != null) && (trPr2 == null)) || ((trPr1 == null) && (trPr2 != null)))
			return false;
		defs1 = trPr1.getCnfStyleOrDivIdOrGridBefore();
		defs2 = trPr2.getCnfStyleOrDivIdOrGridBefore();
		if (defs1 == defs2) 
			return true;
		//getCnfStyleOrDivIdOrGridBefore() is allways != null
		if (defs1.size() != defs2.size())
			return false;
		if (defs1.isEmpty())
			return true;

		for (int i=0; i> defs, QName qName) {
		for (int i=0; i tblStylePrList) {
		if ((tblStylePrList == null) || (tblStylePrList.isEmpty()))
			return true;
		
		for (int i=0; i
		//|| (!booleanDefaultTrue.isVal());
	}

	public static boolean isEmpty(Boolean bool) {
		return (bool == null) || (!bool.booleanValue());
	}

	protected static boolean isEmpty(BigInteger val) {
		return (val == null); // want to apply 0 value!
		//|| (val.equals(BigInteger.ZERO));
	}

	protected static boolean isEmpty(Integer val) {
		return (val == null); // want to apply 0 value!
		//|| (val.intValue() == 0);
	}

	protected static boolean isEmpty(String val) {
		return (val == null) || (val.length() == 0);
	}

	protected static boolean isEmpty(STThemeColor val) {
		return (val == null) || (val.equals(STThemeColor.NONE));
	}
	
	protected static boolean isEmpty(SectPr val) {
		
		if (val==null) return true;
		
		log.debug("TODO: isEmpty(SectPr) implementation is quite basic");
		
		return false;
	}
	
/////////////////////////////////////////////
//apply-Methods
//
// see similar ImmutablePropertyResolver
//	
/////////////////////////////////////////////
	
	/**
	 * Note that this method does not climb the hierarchy
	 * to take any account of what these styles are basedOn
	 * (other than to set the basedOn value)
	 * 
	 * @param source
	 * @param destination
	 */
	public static Style apply(Style source, Style destination) {

		if (!isEmpty(source)) {
			
			if (destination == null) {
				destination = Context.getWmlObjectFactory().createStyle();
				if (source.getType()!=null) {
					destination.setType(source.getType());
				}
			} else {
				if (areEqual(source.getType(), destination.getType())) {
					// good, as expected
				} else if (destination.getType()==null) {
					log.warn("Setting destination style type from source type " + source.getType());
					destination.setType(source.getType());
				} else {
					throw new RuntimeException("Source style type " + source.getType()
							+ " does not match destination type " + destination.getType());
					// Maybe there are some scenarios where you want to apply
					// say the rpr component of a p style to a run level style
					// but wait until need is proven.
					// You could still do that, by using apply(RPr, RPr).
					// Better to do type like checking here, and force explicit intention
				}
			}
		
			if (CHARACTER_STYLE.equals(source.getType())) {
				
				destination.setRPr(apply(source.getRPr(), destination.getRPr()));
			}
			else if (PARAGRAPH_STYLE.equals(source.getType()) || 
					 NUMBERING_STYLE.equals(source.getType())) {
				
				destination.setPPr(apply(source.getPPr(), destination.getPPr()));
				destination.setRPr(apply(source.getRPr(), destination.getRPr()));
			}
			else if (TABLE_STYLE.equals(source.getType())) {
				
				destination.setTblPr(apply(source.getTblPr(), destination.getTblPr()));
				destination.setTcPr(apply(source.getTcPr(), destination.getTcPr()));
				
				apply(source.getTblStylePr(), destination.getTblStylePr());
				
				destination.setPPr(apply(source.getPPr(), destination.getPPr()));
				destination.setRPr(apply(source.getRPr(), destination.getRPr()));
			}
			else {

				log.warn("source style type is currently unknown or null");
				
				destination.setTblPr(apply(source.getTblPr(), destination.getTblPr()));
				destination.setTcPr(apply(source.getTcPr(), destination.getTcPr()));
				
				apply(source.getTblStylePr(), destination.getTblStylePr());
				
				destination.setPPr(apply(source.getPPr(), destination.getPPr()));
				destination.setRPr(apply(source.getRPr(), destination.getRPr()));
			}
		}
		return destination;
	}

	public static PPr apply(PPr source, PPr destination) {
		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createPPr();
			apply((PPrBase)source, (PPrBase)destination);
			destination.setRPr(apply(source.getRPr(), destination.getRPr()));
			destination.setSectPr(apply(source.getSectPr(), destination.getSectPr()));
		}
		return destination;
	}

	public static void apply(PPrBase source, PPrBase destination) {
		
	//PPrBase as a Base class isn't instantiated
		if (!isEmpty((PPrBase)source)) {
			destination.setPStyle(apply(source.getPStyle(), destination.getPStyle()));	
			destination.setKeepNext(apply(source.getKeepNext(), destination.getKeepNext()));	
			destination.setKeepLines(apply(source.getKeepLines(), destination.getKeepLines()));	
			destination.setPageBreakBefore(apply(source.getPageBreakBefore(), destination.getPageBreakBefore()));	
			destination.setFramePr(apply(source.getFramePr(), destination.getFramePr()));	
			destination.setWidowControl(apply(source.getWidowControl(), destination.getWidowControl()));	
			destination.setNumPr(apply(source.getNumPr(), destination.getNumPr()));	
			destination.setSuppressLineNumbers(apply(source.getSuppressLineNumbers(), destination.getSuppressLineNumbers()));	
			destination.setPBdr(apply(source.getPBdr(), destination.getPBdr()));	
			destination.setShd(apply(source.getShd(), destination.getShd()));	
			destination.setTabs(apply(source.getTabs(), destination.getTabs()));	
			destination.setSuppressAutoHyphens(apply(source.getSuppressAutoHyphens(), destination.getSuppressAutoHyphens()));	
			destination.setKinsoku(apply(source.getKinsoku(), destination.getKinsoku()));	
			destination.setWordWrap(apply(source.getWordWrap(), destination.getWordWrap()));	
			destination.setOverflowPunct(apply(source.getOverflowPunct(), destination.getOverflowPunct()));	
			destination.setTopLinePunct(apply(source.getTopLinePunct(), destination.getTopLinePunct()));	
			destination.setAutoSpaceDE(apply(source.getAutoSpaceDE(), destination.getAutoSpaceDE()));	
			destination.setAutoSpaceDN(apply(source.getAutoSpaceDN(), destination.getAutoSpaceDN()));	
			destination.setBidi(apply(source.getBidi(), destination.getBidi()));	
			destination.setAdjustRightInd(apply(source.getAdjustRightInd(), destination.getAdjustRightInd()));	
			destination.setSnapToGrid(apply(source.getSnapToGrid(), destination.getSnapToGrid()));
			
			destination.setSpacing(apply(source.getSpacing(), destination.getSpacing()));	
			
			destination.setInd(apply(source.getInd(), destination.getInd()));
			
			destination.setContextualSpacing(apply(source.getContextualSpacing(), destination.getContextualSpacing()));	
			destination.setMirrorIndents(apply(source.getMirrorIndents(), destination.getMirrorIndents()));	
			destination.setSuppressOverlap(apply(source.getSuppressOverlap(), destination.getSuppressOverlap()));	
			destination.setJc(apply(source.getJc(), destination.getJc()));	
			destination.setTextDirection(apply(source.getTextDirection(), destination.getTextDirection()));	
			destination.setTextAlignment(apply(source.getTextAlignment(), destination.getTextAlignment()));	
			destination.setTextboxTightWrap(apply(source.getTextboxTightWrap(), destination.getTextboxTightWrap()));	
			destination.setOutlineLvl(apply(source.getOutlineLvl(), destination.getOutlineLvl()));
			destination.setCnfStyle(apply(source.getCnfStyle(), destination.getCnfStyle()));	
		}
	}
	
	/**
	 * @param source
	 * @param destination
	 * @return
	 * @since 3.2
	 */
	public static SectPr apply(SectPr source, SectPr destination) {
		
		if (!isEmpty(source)) {
			
			if (destination == null) {
				destination = Context.getWmlObjectFactory().createSectPr();
			}
			
			destination.setFormProt(apply(source.getFormProt() , destination.getFormProt()));
			destination.setVAlign(apply(source.getVAlign() , destination.getVAlign()) );
			destination.setNoEndnote(apply(source.getNoEndnote(), destination.getNoEndnote()) );
			destination.setTitlePg(apply(source.getTitlePg() , destination.getTitlePg()) );
			destination.setTextDirection(apply(source.getTextDirection(), destination.getTextDirection()) );
			destination.setBidi(apply(source.getBidi(), destination.getBidi()) );
			destination.setRtlGutter(apply(source.getRtlGutter(), destination.getRtlGutter() ));
					
			log.warn("TODO: implementation is incomplete");
					
// TODO						
//						destination.setDocGrid(apply(source.getDocGrid() , destination.getDocGrid()) );
//						destination.setEGHdrFtrReferences(apply(source.getEGHdrFtrReferences(), destination.getEGHdrFtrReferences()) );
//						destination.setFootnotePr(apply(source.getFootnotePr(), destination.getFootnotePr()) );
//						destination.setEndnotePr(apply(source.getEndnotePr(), destination.getEndnotePr()) );
//						destination.setType(apply(source.getType() , destination.getType()) );
//						destination.setPgSz(apply(source.getPgSz() , destination.getPgSz()) );
//						destination.setPgMar(apply(source.getPgMar(), destination.getPgMar()) );
//						destination.setPaperSrc(apply(source.getPaperSrc(), destination.getPaperSrc()) );
//						destination.setPgBorders(apply(source.getPgBorders() , destination.getPgBorders()) );
//						destination.setLnNumType(apply(source.getLnNumType(), destination.getLnNumType()) );
//						destination.setPgNumType(apply(source.getPgNumType(), destination.getPgNumType()) );
//						destination.setCols(apply(source.getCols() , destination.getCols()) );
//						destination.setPrinterSettings(apply(source.getPrinterSettings(), destination.getPrinterSettings()) );
//						destination.setFootnoteColumns(apply(source.getFootnoteColumns(), destination.getFootnoteColumns()) 
					  
		}
		return destination;												
	}	

	public static RPr apply(RPr source, RPr destination) {
		
		boolean hint = false;
		if (source!=null
				&& source.getRFonts()!=null
				&& !isEmpty(source.getRFonts().getHint())) {
			hint = true; 
			log.debug("source rPr contains rFonts with hint");
		}

		if (isEmpty(source) && !hint ) {
			log.debug("no source rPr to apply");			
		} else {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createRPr();
			
			destination.setLang(apply(source.getLang(), destination.getLang()));
			destination.setRStyle(apply(source.getRStyle(), destination.getRStyle()));
			destination.setRFonts(apply(source.getRFonts(), destination.getRFonts()));
			destination.setB(apply(source.getB(), destination.getB()));
			destination.setBCs(apply(source.getBCs(), destination.getBCs()));
			destination.setI(apply(source.getI(), destination.getI()));
			destination.setICs(apply(source.getICs(), destination.getICs()));
			destination.setCaps(apply(source.getCaps(), destination.getCaps()));
			destination.setSmallCaps(apply(source.getSmallCaps(), destination.getSmallCaps()));
			destination.setStrike(apply(source.getStrike(), destination.getStrike()));
			destination.setDstrike(apply(source.getDstrike(), destination.getDstrike()));
			destination.setOutline(apply(source.getOutline(), destination.getOutline()));
			destination.setShadow(apply(source.getShadow(), destination.getShadow()));
			destination.setEmboss(apply(source.getEmboss(), destination.getEmboss()));
			destination.setImprint(apply(source.getImprint(), destination.getImprint()));
			destination.setSnapToGrid(apply(source.getSnapToGrid(), destination.getSnapToGrid()));
			destination.setVanish(apply(source.getVanish(), destination.getVanish()));
			destination.setColor(apply(source.getColor(), destination.getColor()));
			destination.setSpacing(apply(source.getSpacing(), destination.getSpacing()));
			destination.setW(apply(source.getW(), destination.getW()));
			destination.setKern(apply(source.getKern(), destination.getKern()));
			destination.setPosition(apply(source.getPosition(), destination.getPosition()));
			destination.setSz(apply(source.getSz(), destination.getSz()));
			destination.setSzCs(apply(source.getSzCs(), destination.getSzCs()));
			destination.setHighlight(apply(source.getHighlight(), destination.getHighlight()));
			destination.setU(apply(source.getU(), destination.getU()));
			destination.setEffect(apply(source.getEffect(), destination.getEffect()));
			destination.setBdr(apply(source.getBdr(), destination.getBdr()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setVertAlign(apply(source.getVertAlign(), destination.getVertAlign()));
			destination.setRtl(apply(source.getRtl(), destination.getRtl()));
			destination.setCs(apply(source.getCs(), destination.getCs()));
			destination.setEm(apply(source.getEm(), destination.getEm()));
			destination.setSpecVanish(apply(source.getSpecVanish(), destination.getSpecVanish()));
			destination.setOMath(apply(source.getOMath(), destination.getOMath()));
		}
		return destination;
	}
	
	public static CTLanguage apply(CTLanguage source, CTLanguage destination) {
		// TODO refine this?
		return ((source == null) ? destination : source);
	}
	

	public static RPr apply(ParaRPr source, RPr destination) {
		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createRPr();
			
			destination.setRStyle(apply(source.getRStyle(), destination.getRStyle()));
			destination.setRFonts(apply(source.getRFonts(), destination.getRFonts()));
			destination.setB(apply(source.getB(), destination.getB()));
			destination.setBCs(apply(source.getBCs(), destination.getBCs()));
			destination.setI(apply(source.getI(), destination.getI()));
			destination.setICs(apply(source.getICs(), destination.getICs()));
			destination.setCaps(apply(source.getCaps(), destination.getCaps()));
			destination.setSmallCaps(apply(source.getSmallCaps(), destination.getSmallCaps()));
			destination.setStrike(apply(source.getStrike(), destination.getStrike()));
			destination.setDstrike(apply(source.getDstrike(), destination.getDstrike()));
			destination.setOutline(apply(source.getOutline(), destination.getOutline()));
			destination.setShadow(apply(source.getShadow(), destination.getShadow()));
			destination.setEmboss(apply(source.getEmboss(), destination.getEmboss()));
			destination.setImprint(apply(source.getImprint(), destination.getImprint()));
			destination.setSnapToGrid(apply(source.getSnapToGrid(), destination.getSnapToGrid()));
			destination.setVanish(apply(source.getVanish(), destination.getVanish()));
			destination.setColor(apply(source.getColor(), destination.getColor()));
			destination.setSpacing(apply(source.getSpacing(), destination.getSpacing()));
			destination.setW(apply(source.getW(), destination.getW()));
			destination.setKern(apply(source.getKern(), destination.getKern()));
			destination.setPosition(apply(source.getPosition(), destination.getPosition()));
			destination.setSz(apply(source.getSz(), destination.getSz()));
			destination.setSzCs(apply(source.getSzCs(), destination.getSzCs()));
			destination.setHighlight(apply(source.getHighlight(), destination.getHighlight()));
			destination.setU(apply(source.getU(), destination.getU()));
			destination.setEffect(apply(source.getEffect(), destination.getEffect()));
			destination.setBdr(apply(source.getBdr(), destination.getBdr()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setVertAlign(apply(source.getVertAlign(), destination.getVertAlign()));
			destination.setRtl(apply(source.getRtl(), destination.getRtl()));
			destination.setCs(apply(source.getCs(), destination.getCs()));
			destination.setEm(apply(source.getEm(), destination.getEm()));
			destination.setSpecVanish(apply(source.getSpecVanish(), destination.getSpecVanish()));
			destination.setOMath(apply(source.getOMath(), destination.getOMath()));
		}
		return destination;
	}

	public static ParaRPr apply(RPr source, ParaRPr destination) {
		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createParaRPr();
			
			destination.setRStyle(apply(source.getRStyle(), destination.getRStyle()));
			destination.setRFonts(apply(source.getRFonts(), destination.getRFonts()));
			destination.setB(apply(source.getB(), destination.getB()));
			destination.setBCs(apply(source.getBCs(), destination.getBCs()));
			destination.setI(apply(source.getI(), destination.getI()));
			destination.setICs(apply(source.getICs(), destination.getICs()));
			destination.setCaps(apply(source.getCaps(), destination.getCaps()));
			destination.setSmallCaps(apply(source.getSmallCaps(), destination.getSmallCaps()));
			destination.setStrike(apply(source.getStrike(), destination.getStrike()));
			destination.setDstrike(apply(source.getDstrike(), destination.getDstrike()));
			destination.setOutline(apply(source.getOutline(), destination.getOutline()));
			destination.setShadow(apply(source.getShadow(), destination.getShadow()));
			destination.setEmboss(apply(source.getEmboss(), destination.getEmboss()));
			destination.setImprint(apply(source.getImprint(), destination.getImprint()));
			destination.setSnapToGrid(apply(source.getSnapToGrid(), destination.getSnapToGrid()));
			destination.setVanish(apply(source.getVanish(), destination.getVanish()));
			destination.setColor(apply(source.getColor(), destination.getColor()));
			destination.setSpacing(apply(source.getSpacing(), destination.getSpacing()));
			destination.setW(apply(source.getW(), destination.getW()));
			destination.setKern(apply(source.getKern(), destination.getKern()));
			destination.setPosition(apply(source.getPosition(), destination.getPosition()));
			destination.setSz(apply(source.getSz(), destination.getSz()));
			destination.setSzCs(apply(source.getSzCs(), destination.getSzCs()));
			destination.setHighlight(apply(source.getHighlight(), destination.getHighlight()));
			destination.setU(apply(source.getU(), destination.getU()));
			destination.setEffect(apply(source.getEffect(), destination.getEffect()));
			destination.setBdr(apply(source.getBdr(), destination.getBdr()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setVertAlign(apply(source.getVertAlign(), destination.getVertAlign()));
			destination.setRtl(apply(source.getRtl(), destination.getRtl()));
			destination.setCs(apply(source.getCs(), destination.getCs()));
			destination.setEm(apply(source.getEm(), destination.getEm()));
			destination.setSpecVanish(apply(source.getSpecVanish(), destination.getSpecVanish()));
			destination.setOMath(apply(source.getOMath(), destination.getOMath()));
		}
		return destination;
	}

	public static ParaRPr apply(ParaRPr source, ParaRPr destination) {

		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createParaRPr();
			
			destination.setRStyle(apply(source.getRStyle(), destination.getRStyle()));
			destination.setRFonts(apply(source.getRFonts(), destination.getRFonts()));
			destination.setB(apply(source.getB(), destination.getB()));
			destination.setBCs(apply(source.getBCs(), destination.getBCs()));
			destination.setI(apply(source.getI(), destination.getI()));
			destination.setICs(apply(source.getICs(), destination.getICs()));
			destination.setCaps(apply(source.getCaps(), destination.getCaps()));
			destination.setSmallCaps(apply(source.getSmallCaps(), destination.getSmallCaps()));
			destination.setStrike(apply(source.getStrike(), destination.getStrike()));
			destination.setDstrike(apply(source.getDstrike(), destination.getDstrike()));
			destination.setOutline(apply(source.getOutline(), destination.getOutline()));
			destination.setShadow(apply(source.getShadow(), destination.getShadow()));
			destination.setEmboss(apply(source.getEmboss(), destination.getEmboss()));
			destination.setImprint(apply(source.getImprint(), destination.getImprint()));
			destination.setSnapToGrid(apply(source.getSnapToGrid(), destination.getSnapToGrid()));
			destination.setVanish(apply(source.getVanish(), destination.getVanish()));
			destination.setColor(apply(source.getColor(), destination.getColor()));
			destination.setSpacing(apply(source.getSpacing(), destination.getSpacing()));
			destination.setW(apply(source.getW(), destination.getW()));
			destination.setKern(apply(source.getKern(), destination.getKern()));
			destination.setPosition(apply(source.getPosition(), destination.getPosition()));
			destination.setSz(apply(source.getSz(), destination.getSz()));
			destination.setSzCs(apply(source.getSzCs(), destination.getSzCs()));
			destination.setHighlight(apply(source.getHighlight(), destination.getHighlight()));
			destination.setU(apply(source.getU(), destination.getU()));
			destination.setEffect(apply(source.getEffect(), destination.getEffect()));
			destination.setBdr(apply(source.getBdr(), destination.getBdr()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setVertAlign(apply(source.getVertAlign(), destination.getVertAlign()));
			destination.setRtl(apply(source.getRtl(), destination.getRtl()));
			destination.setCs(apply(source.getCs(), destination.getCs()));
			destination.setEm(apply(source.getEm(), destination.getEm()));
			destination.setSpecVanish(apply(source.getSpecVanish(), destination.getSpecVanish()));
			destination.setOMath(apply(source.getOMath(), destination.getOMath()));
		}
		return destination;
	}
	
	public static CTTblPrBase apply(CTTblPrBase source, CTTblPrBase destination) {
		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createTblPr();
			
			destination.setBidiVisual(apply(source.getBidiVisual(), destination.getBidiVisual()));
			
			destination.setTblStyle(apply(source.getTblStyle(), destination.getTblStyle()));
			destination.setTblpPr(apply(source.getTblpPr(), destination.getTblpPr()));
			destination.setTblOverlap(apply(source.getTblOverlap(), destination.getTblOverlap()));
			destination.setTblStyleRowBandSize(apply(source.getTblStyleRowBandSize(), destination.getTblStyleRowBandSize()));
			destination.setTblStyleColBandSize(apply(source.getTblStyleColBandSize(), destination.getTblStyleColBandSize()));
			destination.setTblW(apply(source.getTblW(), destination.getTblW()));
			destination.setJc(apply(source.getJc(), destination.getJc()));
			destination.setTblCellSpacing(apply(source.getTblCellSpacing(), destination.getTblCellSpacing()));
			destination.setTblInd(apply(source.getTblInd(), destination.getTblInd()));
			destination.setTblBorders(apply(source.getTblBorders(), destination.getTblBorders()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setTblLayout(apply(source.getTblLayout(), destination.getTblLayout()));
			destination.setTblCellMar(apply(source.getTblCellMar(), destination.getTblCellMar()));
			destination.setTblLook(apply(source.getTblLook(), destination.getTblLook()));
		}
		return destination;
	}

	public static TcPr apply(TcPr source, TcPr destination) {
		if (!isEmpty(source)) {
			if (destination == null) 
				destination = Context.getWmlObjectFactory().createTcPr();
			
			destination.setCnfStyle(apply(source.getCnfStyle(), destination.getCnfStyle()));
			destination.setTcW(apply(source.getTcW(), destination.getTcW()));
			destination.setGridSpan(apply(source.getGridSpan(), destination.getGridSpan()));
			destination.setHMerge(apply(source.getHMerge(), destination.getHMerge()));
			destination.setVMerge(apply(source.getVMerge(), destination.getVMerge()));
			destination.setTcBorders(apply(source.getTcBorders(), destination.getTcBorders()));
			destination.setShd(apply(source.getShd(), destination.getShd()));
			destination.setNoWrap(apply(source.getNoWrap(), destination.getNoWrap()));
			destination.setTcMar(apply(source.getTcMar(), destination.getTcMar()));
			destination.setTextDirection(apply(source.getTextDirection(), destination.getTextDirection()));
			destination.setTcFitText(apply(source.getTcFitText(), destination.getTcFitText()));
			destination.setVAlign(apply(source.getVAlign(), destination.getVAlign()));
			destination.setHideMark(apply(source.getHideMark(), destination.getHideMark()));
		}
		return destination;
	}

	public static void apply(List source, List destination) {
		CTTblStylePr destinationTblStylePr = null;
		if (!isEmpty(source)) {
			// not sure about this, but if the source defines a content model it should
			// replace the destination as a whole, and not parts of it.
			destination.clear();
			for (int i=0; i> defsSource = null;
	List> defsDestination = null;
	JAXBElement defsSourceElement = null;
		if (isEmpty(source))
			return destination;
		if (!isEmpty(destination)) {
			defsSource = source.getCnfStyleOrDivIdOrGridBefore();
			defsDestination = destination.getCnfStyleOrDivIdOrGridBefore();

			for (int i=0; i
	protected static BigInteger apply(BigInteger source, BigInteger destination) {
		return (source == null  ? destination : source);
	}

	protected static Integer apply(Integer source, Integer destination) {
		return (source == null || source.intValue() == 0 ? destination : source);
	}

	protected static String apply(String source, String destination) {
		return (source == null || source.length() == 0 ? destination : source);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy