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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 java.util.Locale;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.SAXException;
  24. import org.xml.sax.helpers.AttributesImpl;
  25. /**
  26. * A collection of utility method for XML handling.
  27. */
  28. public class XMLUtil implements XMLConstants {
  29. /**
  30. * Returns an attribute value as a boolean value.
  31. * @param attributes the Attributes object
  32. * @param name the name of the attribute
  33. * @param defaultValue the default value if the attribute is not specified
  34. * @return the attribute value as a boolean
  35. */
  36. public static boolean getAttributeAsBoolean(Attributes attributes, String name,
  37. boolean defaultValue) {
  38. String s = attributes.getValue(name);
  39. if (s == null) {
  40. return defaultValue;
  41. } else {
  42. return Boolean.valueOf(s).booleanValue();
  43. }
  44. }
  45. /**
  46. * Returns an attribute value as a int value.
  47. * @param attributes the Attributes object
  48. * @param name the name of the attribute
  49. * @param defaultValue the default value if the attribute is not specified
  50. * @return the attribute value as an int
  51. */
  52. public static int getAttributeAsInt(Attributes attributes, String name,
  53. int defaultValue) {
  54. String s = attributes.getValue(name);
  55. if (s == null) {
  56. return defaultValue;
  57. } else {
  58. return Integer.parseInt(s);
  59. }
  60. }
  61. /**
  62. * Returns an attribute value as a int value.
  63. * @param attributes the Attributes object
  64. * @param name the name of the attribute
  65. * @return the attribute value as an int
  66. * @throws SAXException if the attribute is missing
  67. */
  68. public static int getAttributeAsInt(Attributes attributes, String name) throws SAXException {
  69. String s = attributes.getValue(name);
  70. if (s == null) {
  71. throw new SAXException("Attribute '" + name + "' is missing");
  72. } else {
  73. return Integer.parseInt(s);
  74. }
  75. }
  76. /**
  77. * Returns an attribute value as a Integer value.
  78. * @param attributes the Attributes object
  79. * @param name the name of the attribute
  80. * @return the attribute value as an Integer or null if the attribute is missing
  81. */
  82. public static Integer getAttributeAsInteger(Attributes attributes, String name) {
  83. String s = attributes.getValue(name);
  84. if (s == null) {
  85. return null;
  86. } else {
  87. return new Integer(s);
  88. }
  89. }
  90. /**
  91. * Returns an attribute value as a Rectangle2D value. The string value is expected as 4
  92. * double-precision numbers separated by whitespace.
  93. * @param attributes the Attributes object
  94. * @param name the name of the attribute
  95. * @return the attribute value as an Rectangle2D
  96. */
  97. public static Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name) {
  98. String s = attributes.getValue(name).trim();
  99. double[] values = ConversionUtils.toDoubleArray(s, "\\s");
  100. if (values.length != 4) {
  101. throw new IllegalArgumentException("Rectangle must consist of 4 double values!");
  102. }
  103. return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
  104. }
  105. /**
  106. * Returns an attribute value as a Rectangle value. The string value is expected as 4
  107. * integer numbers separated by whitespace.
  108. * @param attributes the Attributes object
  109. * @param name the name of the attribute
  110. * @return the attribute value as an Rectangle
  111. */
  112. public static Rectangle getAttributeAsRectangle(Attributes attributes, String name) {
  113. String s = attributes.getValue(name);
  114. if (s == null) {
  115. return null;
  116. }
  117. int[] values = ConversionUtils.toIntArray(s.trim(), "\\s");
  118. if (values.length != 4) {
  119. throw new IllegalArgumentException("Rectangle must consist of 4 int values!");
  120. }
  121. return new Rectangle(values[0], values[1], values[2], values[3]);
  122. }
  123. /**
  124. * Returns an attribute value as a integer array. The string value is expected as 4
  125. * integer numbers separated by whitespace.
  126. * @param attributes the Attributes object
  127. * @param name the name of the attribute
  128. * @return the attribute value as an int array
  129. */
  130. public static int[] getAttributeAsIntArray(Attributes attributes, String name) {
  131. String s = attributes.getValue(name);
  132. if (s == null) {
  133. return null;
  134. } else {
  135. return ConversionUtils.toIntArray(s.trim(), "\\s");
  136. }
  137. }
  138. /**
  139. * Adds an attribute to a given {@link AttributesImpl} instance.
  140. * @param atts the attributes collection
  141. * @param attribute the attribute to add
  142. * @param value the attribute's CDATA value
  143. */
  144. public static void addAttribute(AttributesImpl atts,
  145. org.apache.xmlgraphics.util.QName attribute, String value) {
  146. atts.addAttribute(attribute.getNamespaceURI(),
  147. attribute.getLocalName(), attribute.getQName(), XMLUtil.CDATA, value);
  148. }
  149. /**
  150. * Adds an attribute to a given {@link AttributesImpl} instance. The attribute will be
  151. * added in the default namespace.
  152. * @param atts the attributes collection
  153. * @param localName the local name of the attribute
  154. * @param value the attribute's CDATA value
  155. */
  156. public static void addAttribute(AttributesImpl atts, String localName, String value) {
  157. atts.addAttribute("", localName, localName, XMLUtil.CDATA, value);
  158. }
  159. /**
  160. * Converts a {@link Locale} instance to an RFC 3066 compliant language identifier.
  161. * @param language the language
  162. * @return the formatted language identifier
  163. */
  164. public static String toRFC3066(Locale language) {
  165. if (language == null || language.getLanguage().length() == 0) {
  166. return null;
  167. }
  168. StringBuffer sb = new StringBuffer();
  169. sb.append(language.getLanguage());
  170. if (language.getCountry().length() > 0) {
  171. sb.append('-');
  172. sb.append(language.getCountry());
  173. }
  174. return sb.toString();
  175. }
  176. /**
  177. * Converts an RFC 3066 compliant language identifier to a {@link Locale} instance.
  178. * @param lang the language string
  179. * @return the converted locale instance
  180. */
  181. public static Locale convertRFC3066ToLocale(String lang) {
  182. if (lang == null || lang.length() == 0) {
  183. return null;
  184. }
  185. String[] parts = lang.split("-");
  186. if (parts.length == 1) {
  187. return new Locale(parts[0]);
  188. } else {
  189. return new Locale(parts[0], parts[1]);
  190. }
  191. }
  192. }