You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLUtil.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.Rectangle2D;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.SAXException;
  23. import org.xml.sax.helpers.AttributesImpl;
  24. /**
  25. * A collection of utility method for XML handling.
  26. */
  27. public final class XMLUtil implements XMLConstants {
  28. private XMLUtil() {
  29. }
  30. /**
  31. * Returns an attribute value as a boolean value.
  32. * @param attributes the Attributes object
  33. * @param name the name of the attribute
  34. * @param defaultValue the default value if the attribute is not specified
  35. * @return the attribute value as a boolean
  36. */
  37. public static boolean getAttributeAsBoolean(Attributes attributes, String name,
  38. boolean defaultValue) {
  39. String s = attributes.getValue(name);
  40. if (s == null) {
  41. return defaultValue;
  42. } else {
  43. return Boolean.valueOf(s).booleanValue();
  44. }
  45. }
  46. /**
  47. * Returns an attribute value as a int value.
  48. * @param attributes the Attributes object
  49. * @param name the name of the attribute
  50. * @param defaultValue the default value if the attribute is not specified
  51. * @return the attribute value as an int
  52. */
  53. public static int getAttributeAsInt(Attributes attributes, String name,
  54. int defaultValue) {
  55. String s = attributes.getValue(name);
  56. if (s == null) {
  57. return defaultValue;
  58. } else {
  59. return Integer.parseInt(s);
  60. }
  61. }
  62. /**
  63. * Returns an attribute value as a int value.
  64. * @param attributes the Attributes object
  65. * @param name the name of the attribute
  66. * @return the attribute value as an int
  67. * @throws SAXException if the attribute is missing
  68. */
  69. public static int getAttributeAsInt(Attributes attributes, String name) throws SAXException {
  70. String s = attributes.getValue(name);
  71. if (s == null) {
  72. throw new SAXException("Attribute '" + name + "' is missing");
  73. } else {
  74. return Integer.parseInt(s);
  75. }
  76. }
  77. /**
  78. * Returns an attribute value as a Integer value.
  79. * @param attributes the Attributes object
  80. * @param name the name of the attribute
  81. * @return the attribute value as an Integer or null if the attribute is missing
  82. */
  83. public static Integer getAttributeAsInteger(Attributes attributes, String name) {
  84. String s = attributes.getValue(name);
  85. if (s == null) {
  86. return null;
  87. } else {
  88. return new Integer(s);
  89. }
  90. }
  91. /**
  92. * Returns an attribute value as a Rectangle2D value. The string value is expected as 4
  93. * double-precision numbers separated by whitespace.
  94. * @param attributes the Attributes object
  95. * @param name the name of the attribute
  96. * @return the attribute value as an Rectangle2D
  97. */
  98. public static Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name) {
  99. String s = attributes.getValue(name).trim();
  100. double[] values = ConversionUtils.toDoubleArray(s, "\\s");
  101. if (values.length != 4) {
  102. throw new IllegalArgumentException("Rectangle must consist of 4 double values!");
  103. }
  104. return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
  105. }
  106. /**
  107. * Returns an attribute value as a Rectangle value. The string value is expected as 4
  108. * integer numbers separated by whitespace.
  109. * @param attributes the Attributes object
  110. * @param name the name of the attribute
  111. * @return the attribute value as an Rectangle
  112. */
  113. public static Rectangle getAttributeAsRectangle(Attributes attributes, String name) {
  114. String s = attributes.getValue(name);
  115. if (s == null) {
  116. return null;
  117. }
  118. int[] values = ConversionUtils.toIntArray(s.trim(), "\\s");
  119. if (values.length != 4) {
  120. throw new IllegalArgumentException("Rectangle must consist of 4 int values!");
  121. }
  122. return new Rectangle(values[0], values[1], values[2], values[3]);
  123. }
  124. /**
  125. * Returns an attribute value as a integer array. The string value is expected as 4
  126. * integer numbers separated by whitespace.
  127. * @param attributes the Attributes object
  128. * @param name the name of the attribute
  129. * @return the attribute value as an int array
  130. */
  131. public static int[] getAttributeAsIntArray(Attributes attributes, String name) {
  132. String s = attributes.getValue(name);
  133. if (s == null) {
  134. return null;
  135. } else {
  136. return ConversionUtils.toIntArray(s.trim(), "\\s");
  137. }
  138. }
  139. /**
  140. * Adds an attribute to a given {@link AttributesImpl} instance.
  141. * @param atts the attributes collection
  142. * @param attribute the attribute to add
  143. * @param value the attribute's CDATA value
  144. */
  145. public static void addAttribute(AttributesImpl atts,
  146. org.apache.xmlgraphics.util.QName attribute, String value) {
  147. atts.addAttribute(attribute.getNamespaceURI(),
  148. attribute.getLocalName(), attribute.getQName(), XMLUtil.CDATA, value);
  149. }
  150. /**
  151. * Adds an attribute to a given {@link AttributesImpl} instance. The attribute will be
  152. * added in the default namespace.
  153. * @param atts the attributes collection
  154. * @param localName the local name of the attribute
  155. * @param value the attribute's CDATA value
  156. */
  157. public static void addAttribute(AttributesImpl atts, String localName, String value) {
  158. atts.addAttribute("", localName, localName, XMLUtil.CDATA, value);
  159. }
  160. /**
  161. * Encode a glyph position adjustments array as a string, where the string value
  162. * adheres to the following syntax:
  163. *
  164. * count ( 'Z' repeat | number )
  165. *
  166. * where each token is separated by whitespace, except that 'Z' followed by repeat
  167. * are considered to be a single token with no intervening whitespace, and where
  168. * 'Z' repeat encodes repeated zeroes.
  169. * @param dp the adjustments array
  170. * @param paCount the number of entries to encode from adjustments array
  171. * @return the encoded value
  172. */
  173. public static String encodePositionAdjustments ( int[][] dp, int paCount ) {
  174. assert dp != null;
  175. StringBuffer sb = new StringBuffer();
  176. int na = paCount;
  177. int nz = 0;
  178. sb.append ( na );
  179. for ( int i = 0; i < na; i++ ) {
  180. int[] pa = dp [ i ];
  181. for ( int k = 0; k < 4; k++ ) {
  182. int a = pa [ k ];
  183. if ( a != 0 ) {
  184. encodeNextAdjustment ( sb, nz, a );
  185. nz = 0;
  186. } else {
  187. nz++;
  188. }
  189. }
  190. }
  191. encodeNextAdjustment ( sb, nz, 0 );
  192. return sb.toString();
  193. }
  194. /**
  195. * Encode a glyph position adjustments array as a string, where the string value
  196. * adheres to the following syntax:
  197. *
  198. * count ( 'Z' repeat | number )
  199. *
  200. * where each token is separated by whitespace, except that 'Z' followed by repeat
  201. * are considered to be a single token with no intervening whitespace.
  202. * @param dp the adjustments array
  203. * @return the encoded value
  204. */
  205. public static String encodePositionAdjustments ( int[][] dp ) {
  206. assert dp != null;
  207. return encodePositionAdjustments ( dp, dp.length );
  208. }
  209. private static void encodeNextAdjustment ( StringBuffer sb, int nz, int a ) {
  210. encodeZeroes ( sb, nz );
  211. encodeAdjustment ( sb, a );
  212. }
  213. private static void encodeZeroes ( StringBuffer sb, int nz ) {
  214. if ( nz > 0 ) {
  215. sb.append ( ' ' );
  216. if ( nz == 1 ) {
  217. sb.append ( '0' );
  218. } else {
  219. sb.append ( 'Z' );
  220. sb.append ( nz );
  221. }
  222. }
  223. }
  224. private static void encodeAdjustment ( StringBuffer sb, int a ) {
  225. if ( a != 0 ) {
  226. sb.append ( ' ' );
  227. sb.append ( a );
  228. }
  229. }
  230. /**
  231. * Decode a string as a glyph position adjustments array, where the string
  232. * shall adhere to the syntax specified by {@link #encodePositionAdjustments}.
  233. * @param value the encoded value
  234. * @return the position adjustments array
  235. */
  236. public static int[][] decodePositionAdjustments ( String value ) {
  237. int[][] dp = null;
  238. if ( value != null ) {
  239. String[] sa = value.split ( "\\s" );
  240. if ( sa != null ) {
  241. if ( sa.length > 0 ) {
  242. int na = Integer.parseInt ( sa[0] );
  243. dp = new int [ na ] [ 4 ];
  244. for ( int i = 1, n = sa.length, k = 0; i < n; i++ ) {
  245. String s = sa [ i ];
  246. if ( s.charAt(0) == 'Z' ) {
  247. int nz = Integer.parseInt ( s.substring ( 1 ) );
  248. k += nz;
  249. } else {
  250. dp [ k / 4 ] [ k % 4 ] = Integer.parseInt ( s );
  251. k += 1;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. return dp;
  258. }
  259. /**
  260. * Returns an attribute value as a glyph position adjustments array. The string value
  261. * is expected to be a non-empty sequence of either Z<repeat> or <number>, where the
  262. * former encodes a repeat count (of zeroes) and the latter encodes a integer number,
  263. * and where each item is separated by whitespace.
  264. * @param attributes the Attributes object
  265. * @param name the name of the attribute
  266. * @return the position adjustments array
  267. */
  268. public static int[][] getAttributeAsPositionAdjustments(Attributes attributes, String name) {
  269. String s = attributes.getValue(name);
  270. if (s == null) {
  271. return null;
  272. } else {
  273. return decodePositionAdjustments(s.trim());
  274. }
  275. }
  276. }