001package com.hfg.xml.msofficexml.docx.wordprocessingml.style; 002 003import com.hfg.css.CSSDeclaration; 004import com.hfg.css.CSSException; 005import com.hfg.css.CSSProperty; 006import com.hfg.graphics.units.Pixels; 007import com.hfg.util.StringUtil; 008import com.hfg.xml.XMLTag; 009import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML; 010 011import java.util.List; 012import java.util.regex.Matcher; 013import java.util.regex.Pattern; 014 015public class WmlTableCellMargins extends XMLTag 016{ 017 private static final Pattern PX_PATTERN = Pattern.compile("(\\d+)(px)?"); 018 019 //--------------------------------------------------------------------------- 020 /** 021 The tag name is different depending on whether the parent tag is the table properties or the table cell properties. 022 */ 023 public WmlTableCellMargins(XMLTag inParentTag) 024 { 025 super(inParentTag.getTagName().equals(WmlXML.TABLE_PROPS.getLocalName()) ? WmlXML.TABLE_CELL_MARGIN_DEFAULTS : WmlXML.TABLE_CELL_MARGIN); 026 } 027 028 //--------------------------------------------------------------------------- 029 public WmlTableCellMargins addMargin(WmlTableCellMargin inMargin) 030 { 031 List<XMLTag> newTags = inMargin.toXMLTags(); 032 for (XMLTag newTag : newTags) 033 { 034 removeSubtagsByName(newTag.getTagName()); 035 addSubtag(newTag); 036 } 037 038 return this; 039 } 040 041 //--------------------------------------------------------------------------- 042 public WmlTableCellMargins addMargins(CSSDeclaration inCSSDeclaration) 043 { 044 if (inCSSDeclaration.getProperty().equals(CSSProperty.margin)) 045 { 046 String[] valuePieces = inCSSDeclaration.getValue().split("\\s+"); 047 if (valuePieces.length == 1) 048 { 049 WmlTableCellMargin margin = new WmlTableCellMargin(); 050 Matcher m = PX_PATTERN.matcher(valuePieces[0]); 051 if (m.matches()) 052 { 053 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 054 addMargin(margin); 055 } 056 } 057 else if (valuePieces.length == 2) 058 { 059 Matcher m = PX_PATTERN.matcher(valuePieces[0]); 060 if (m.matches()) 061 { 062 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.top); 063 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 064 addMargin(margin); 065 margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.bottom); 066 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 067 addMargin(margin); 068 } 069 070 m = PX_PATTERN.matcher(valuePieces[1]); 071 if (m.matches()) 072 { 073 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.left); 074 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 075 addMargin(margin); 076 margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.right); 077 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 078 addMargin(margin); 079 } 080 } 081 else if (valuePieces.length == 4) 082 { 083 Matcher m = PX_PATTERN.matcher(valuePieces[0]); 084 if (m.matches()) 085 { 086 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.top); 087 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 088 addMargin(margin); 089 } 090 091 m = PX_PATTERN.matcher(valuePieces[1]); 092 if (m.matches()) 093 { 094 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.right); 095 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 096 addMargin(margin); 097 } 098 099 m = PX_PATTERN.matcher(valuePieces[2]); 100 if (m.matches()) 101 { 102 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.bottom); 103 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 104 addMargin(margin); 105 } 106 107 m = PX_PATTERN.matcher(valuePieces[3]); 108 if (m.matches()) 109 { 110 WmlTableCellMargin margin = new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.left); 111 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 112 addMargin(margin); 113 } 114 } 115 } 116 else if (inCSSDeclaration.getProperty().equals(CSSProperty.margin_top)) 117 { 118 WmlTableCellMargin margin = new WmlTableCellMargin(); 119 margin.setPosition(WmlTableCellMargin.Position.top); 120 121 Matcher m = PX_PATTERN.matcher(inCSSDeclaration.getValue()); 122 if (m.matches()) 123 { 124 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 125 addMargin(margin); 126 } 127 } 128 else if (inCSSDeclaration.getProperty().equals(CSSProperty.margin_bottom)) 129 { 130 WmlTableCellMargin margin = new WmlTableCellMargin(); 131 margin.setPosition(WmlTableCellMargin.Position.bottom); 132 133 Matcher m = PX_PATTERN.matcher(inCSSDeclaration.getValue()); 134 if (m.matches()) 135 { 136 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 137 addMargin(margin); 138 } 139 } 140 else if (inCSSDeclaration.getProperty().equals(CSSProperty.margin_left)) 141 { 142 WmlTableCellMargin margin = new WmlTableCellMargin(); 143 margin.setPosition(WmlTableCellMargin.Position.left); 144 145 Matcher m = PX_PATTERN.matcher(inCSSDeclaration.getValue()); 146 if (m.matches()) 147 { 148 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 149 addMargin(margin); 150 } 151 } 152 else if (inCSSDeclaration.getProperty().equals(CSSProperty.margin_right)) 153 { 154 WmlTableCellMargin margin = new WmlTableCellMargin(); 155 margin.setPosition(WmlTableCellMargin.Position.right); 156 157 Matcher m = PX_PATTERN.matcher(inCSSDeclaration.getValue()); 158 if (m.matches()) 159 { 160 margin.setWidth(new Pixels(Integer.parseInt(m.group(1)))); 161 addMargin(margin); 162 } 163 } 164 else 165 { 166 throw new CSSException("The CSS declaration " + StringUtil.singleQuote(inCSSDeclaration.toString()) 167 + " cannot be used to construct a " + this.getClass().getSimpleName() + " object!"); 168 } 169 170 return this; 171 } 172} 173