1 18 package org.apache.batik.dom.svg; 19 20 import java.util.StringTokenizer ; 21 22 import org.apache.batik.util.SVGConstants; 23 import org.apache.batik.parser.ParseException; 24 import org.apache.batik.parser.DefaultPreserveAspectRatioHandler; 25 import org.apache.batik.parser.PreserveAspectRatioHandler; 26 import org.apache.batik.parser.PreserveAspectRatioParser; 27 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.svg.SVGPreserveAspectRatio; 30 31 38 public abstract class AbstractSVGPreserveAspectRatio 39 implements SVGPreserveAspectRatio { 40 41 45 protected short align = 46 SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID; 47 48 52 protected short meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET; 53 54 55 public AbstractSVGPreserveAspectRatio() { 56 } 57 58 public short getAlign() { 59 return this.align; 60 } 61 62 public short getMeetOrSlice() { 63 return this.meetOrSlice; 64 } 65 66 public void setAlign(short align) { 67 this.align = align; 68 setAttributeValue(getValueAsString()); 69 } 70 71 public void setMeetOrSlice(short meetOrSlice) { 72 this.meetOrSlice = meetOrSlice; 73 setAttributeValue(getValueAsString()); 74 } 75 76 public void reset() { 77 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID; 78 meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET; 79 } 81 82 protected abstract void setAttributeValue(String value) 83 throws DOMException ; 84 85 protected abstract DOMException createDOMException(short type, String key, 86 Object [] args); 87 88 protected void setValueAsString(String value) throws DOMException { 89 PreserveAspectRatioParserHandler ph; 90 ph = new PreserveAspectRatioParserHandler(); 91 try { 92 PreserveAspectRatioParser p = new PreserveAspectRatioParser(); 93 p.setPreserveAspectRatioHandler(ph); 94 p.parse(value); 95 align = ph.getAlign(); 96 meetOrSlice = ph.getMeetOrSlice(); 97 } catch (ParseException ex) { 98 throw createDOMException(SVG_PRESERVEASPECTRATIO_UNKNOWN, 99 "invalid value for preserveAspectRatio", 100 null); 101 } 102 } 103 104 108 protected String getValueAsString() { 109 String value = null; 110 111 switch (align) { 112 case SVG_PRESERVEASPECTRATIO_NONE: 113 value = SVGConstants.SVG_NONE_VALUE; 114 return value; case SVG_PRESERVEASPECTRATIO_XMINYMIN: 116 value = SVGConstants.SVG_XMINYMIN_VALUE; 117 break; 118 case SVG_PRESERVEASPECTRATIO_XMIDYMIN: 119 value = SVGConstants.SVG_XMIDYMIN_VALUE; 120 break; 121 case SVG_PRESERVEASPECTRATIO_XMAXYMIN: 122 value = SVGConstants.SVG_XMAXYMIN_VALUE; 123 break; 124 case SVG_PRESERVEASPECTRATIO_XMINYMID: 125 value = SVGConstants.SVG_XMINYMID_VALUE; 126 break; 127 case SVG_PRESERVEASPECTRATIO_XMIDYMID: 128 value = SVGConstants.SVG_XMIDYMID_VALUE; 129 break; 130 case SVG_PRESERVEASPECTRATIO_XMAXYMID: 131 value = SVGConstants.SVG_XMAXYMID_VALUE; 132 break; 133 case SVG_PRESERVEASPECTRATIO_XMINYMAX: 134 value = SVGConstants.SVG_XMINYMAX_VALUE; 135 break; 136 case SVG_PRESERVEASPECTRATIO_XMIDYMAX: 137 value = SVGConstants.SVG_XMIDYMAX_VALUE; 138 break; 139 case SVG_PRESERVEASPECTRATIO_XMAXYMAX: 140 value = SVGConstants.SVG_XMAXYMAX_VALUE; 141 break; 142 default: 143 throw createDOMException 144 (SVG_PRESERVEASPECTRATIO_UNKNOWN, 145 "invalid value for preserveAspectRatio",null); 146 } 148 149 switch (meetOrSlice) { 150 case SVG_MEETORSLICE_MEET: 151 value = value + " " + SVGConstants.SVG_MEET_VALUE; 152 break; 153 case SVG_MEETORSLICE_SLICE: 154 value = value + " " + SVGConstants.SVG_SLICE_VALUE; 155 break; 156 default: 157 throw createDOMException(SVG_MEETORSLICE_UNKNOWN, 158 "invalid value for preserveAspectRatio", 159 null); 160 } 162 163 return value; 164 } 165 166 protected class PreserveAspectRatioParserHandler 167 extends DefaultPreserveAspectRatioHandler { 168 public short align = 169 SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID; 170 public short meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET; 171 172 public short getAlign() { 173 return align; 174 } 175 176 public short getMeetOrSlice() { 177 return meetOrSlice; 178 } 179 180 185 public void none() throws ParseException { 186 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE; 187 } 188 189 194 public void xMaxYMax() throws ParseException { 195 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMAX; 196 } 197 198 203 public void xMaxYMid() throws ParseException { 204 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMID; 205 } 206 207 212 public void xMaxYMin() throws ParseException { 213 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMIN; 214 } 215 216 221 public void xMidYMax() throws ParseException { 222 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX; 223 } 224 225 230 public void xMidYMid() throws ParseException { 231 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID; 232 } 233 234 239 public void xMidYMin() throws ParseException { 240 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMIN; 241 } 242 243 248 public void xMinYMax() throws ParseException { 249 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMAX; 250 } 251 252 257 public void xMinYMid() throws ParseException { 258 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMID; 259 } 260 261 266 public void xMinYMin() throws ParseException { 267 align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN; 268 } 269 270 275 public void meet() throws ParseException { 276 meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET; 277 } 278 279 284 public void slice() throws ParseException { 285 meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_SLICE; 286 } 287 } 288 } 289 | Popular Tags |