Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XMLUtil.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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);
  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 Integer.valueOf(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. if (pa != null) {
  182. for (int k = 0; k < 4; k++) {
  183. int a = pa [ k ];
  184. if (a != 0) {
  185. encodeNextAdjustment(sb, nz, a);
  186. nz = 0;
  187. } else {
  188. nz++;
  189. }
  190. }
  191. } else {
  192. nz += 4;
  193. }
  194. }
  195. encodeNextAdjustment(sb, nz, 0);
  196. return sb.toString();
  197. }
  198. /**
  199. * Encode a glyph position adjustments array as a string, where the string value
  200. * adheres to the following syntax:
  201. *
  202. * count ( 'Z' repeat | number )
  203. *
  204. * where each token is separated by whitespace, except that 'Z' followed by repeat
  205. * are considered to be a single token with no intervening whitespace.
  206. * @param dp the adjustments array
  207. * @return the encoded value
  208. */
  209. public static String encodePositionAdjustments(int[][] dp) {
  210. assert dp != null;
  211. return encodePositionAdjustments(dp, dp.length);
  212. }
  213. private static void encodeNextAdjustment(StringBuffer sb, int nz, int a) {
  214. encodeZeroes(sb, nz);
  215. encodeAdjustment(sb, a);
  216. }
  217. private static void encodeZeroes(StringBuffer sb, int nz) {
  218. if (nz > 0) {
  219. sb.append(' ');
  220. if (nz == 1) {
  221. sb.append('0');
  222. } else {
  223. sb.append('Z');
  224. sb.append(nz);
  225. }
  226. }
  227. }
  228. private static void encodeAdjustment(StringBuffer sb, int a) {
  229. if (a != 0) {
  230. sb.append(' ');
  231. sb.append(a);
  232. }
  233. }
  234. /**
  235. * Decode a string as a glyph position adjustments array, where the string
  236. * shall adhere to the syntax specified by {@link #encodePositionAdjustments}.
  237. * @param value the encoded value
  238. * @return the position adjustments array
  239. */
  240. public static int[][] decodePositionAdjustments(String value) {
  241. int[][] dp = null;
  242. if (value != null) {
  243. String[] sa = value.split("\\s");
  244. if (sa != null) {
  245. if (sa.length > 0) {
  246. int na = Integer.parseInt(sa[0]);
  247. dp = new int [ na ] [ 4 ];
  248. for (int i = 1, n = sa.length, k = 0; i < n; i++) {
  249. String s = sa [ i ];
  250. if (s.charAt(0) == 'Z') {
  251. int nz = Integer.parseInt(s.substring(1));
  252. k += nz;
  253. } else {
  254. dp [ k / 4 ] [ k % 4 ] = Integer.parseInt(s);
  255. k += 1;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. return dp;
  262. }
  263. /**
  264. * Returns an attribute value as a glyph position adjustments array. The string value
  265. * is expected to be a non-empty sequence of either Z&lt;repeat&gt; or &lt;number&gt;, where the
  266. * former encodes a repeat count (of zeroes) and the latter encodes a integer number,
  267. * and where each item is separated by whitespace.
  268. * @param attributes the Attributes object
  269. * @param name the name of the attribute
  270. * @return the position adjustments array
  271. */
  272. public static int[][] getAttributeAsPositionAdjustments(Attributes attributes, String name) {
  273. String s = attributes.getValue(name);
  274. if (s == null) {
  275. return null;
  276. } else {
  277. return decodePositionAdjustments(s.trim());
  278. }
  279. }
  280. /**
  281. * Escape '&lt;', '&gt;' and '&amp;' using NCRs.
  282. * @param unescaped string
  283. * @return escaped string
  284. */
  285. public static String escape(String unescaped) {
  286. int needsEscape = 0;
  287. for (int i = 0, n = unescaped.length(); i < n; ++i) {
  288. char c = unescaped.charAt(i);
  289. if ((c == '<') || (c == '>') || (c == '&')) {
  290. ++needsEscape;
  291. }
  292. }
  293. if (needsEscape > 0) {
  294. StringBuffer sb = new StringBuffer(unescaped.length() + 6 * needsEscape);
  295. for (int i = 0, n = unescaped.length(); i < n; ++i) {
  296. char c = unescaped.charAt(i);
  297. if ((c == '<') || (c == '>') || (c == '&')) {
  298. sb.append("&#x");
  299. sb.append(Integer.toString(c, 16));
  300. sb.append(';');
  301. } else {
  302. sb.append(c);
  303. }
  304. }
  305. return sb.toString();
  306. } else {
  307. return unescaped;
  308. }
  309. }
  310. }