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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. }