diff options
author | Jeremias Maerki <jeremias@apache.org> | 2008-08-12 12:53:50 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2008-08-12 12:53:50 +0000 |
commit | 94b370293612d2ccba4f41c7fffddc4be9d10f75 (patch) | |
tree | fff42ad718169edc0acf6417bff7da6b0fda84e7 /src/java/org/apache/fop/util | |
parent | 7c7db27451c36cc6ace1c3082bd2ddf0a3c1a5cd (diff) | |
download | xmlgraphics-fop-94b370293612d2ccba4f41c7fffddc4be9d10f75.tar.gz xmlgraphics-fop-94b370293612d2ccba4f41c7fffddc4be9d10f75.zip |
Extracted some often-used XML constants and utility methods into an interface and a class in org.apache.fop.util.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@685137 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/util')
-rw-r--r-- | src/java/org/apache/fop/util/DOM2SAX.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/fop/util/XMLConstants.java | 49 | ||||
-rw-r--r-- | src/java/org/apache/fop/util/XMLUtil.java | 149 |
3 files changed, 199 insertions, 1 deletions
diff --git a/src/java/org/apache/fop/util/DOM2SAX.java b/src/java/org/apache/fop/util/DOM2SAX.java index 04096e053..839cf107f 100644 --- a/src/java/org/apache/fop/util/DOM2SAX.java +++ b/src/java/org/apache/fop/util/DOM2SAX.java @@ -232,7 +232,7 @@ public class DOM2SAX { // Add attribute to list attrs.addAttribute(attr.getNamespaceURI(), - getLocalName(attr), qnameAttr, "CDATA", attr + getLocalName(attr), qnameAttr, XMLUtil.CDATA, attr .getNodeValue()); } } diff --git a/src/java/org/apache/fop/util/XMLConstants.java b/src/java/org/apache/fop/util/XMLConstants.java new file mode 100644 index 000000000..1f85c3b07 --- /dev/null +++ b/src/java/org/apache/fop/util/XMLConstants.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.util; + + +/** + * A collection of constants for XML handling. + */ +public interface XMLConstants { + + /** "CDATA" constant */ + String CDATA = "CDATA"; + + /** XML namespace prefix */ + String XML_PREFIX = "xml"; + /** XML namespace URI */ + String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"; + + /** XMLNS namespace prefix */ + String XMLNS_PREFIX = "xmlns"; + /** XMLNS namespace URI */ + String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/"; + + /** Namespace prefix for XLink */ + String XLINK_PREFIX = "xlink"; + /** XML namespace for XLink */ + String XLINK_NAMESPACE = "http://www.w3.org/1999/xlink"; + /** xlink:href attribute */ + org.apache.xmlgraphics.util.QName XLINK_HREF = new org.apache.xmlgraphics.util.QName( + XLINK_NAMESPACE, XLINK_PREFIX, "href"); + +} diff --git a/src/java/org/apache/fop/util/XMLUtil.java b/src/java/org/apache/fop/util/XMLUtil.java new file mode 100644 index 000000000..13784ea19 --- /dev/null +++ b/src/java/org/apache/fop/util/XMLUtil.java @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.util; + +import java.awt.Rectangle; +import java.awt.geom.Rectangle2D; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +/** + * A collection of utility method for XML handling. + */ +public class XMLUtil implements XMLConstants { + + /** + * Returns an attribute value as a boolean value. + * @param attributes the Attributes object + * @param name the name of the attribute + * @param defaultValue the default value if the attribute is not specified + * @return the attribute value as a boolean + */ + public static boolean getAttributeAsBoolean(Attributes attributes, String name, + boolean defaultValue) { + String s = attributes.getValue(name); + if (s == null) { + return defaultValue; + } else { + return Boolean.valueOf(s).booleanValue(); + } + } + + /** + * Returns an attribute value as a int value. + * @param attributes the Attributes object + * @param name the name of the attribute + * @param defaultValue the default value if the attribute is not specified + * @return the attribute value as an int + */ + public static int getAttributeAsInt(Attributes attributes, String name, + int defaultValue) { + String s = attributes.getValue(name); + if (s == null) { + return defaultValue; + } else { + return Integer.parseInt(s); + } + } + + /** + * Returns an attribute value as a int value. + * @param attributes the Attributes object + * @param name the name of the attribute + * @return the attribute value as an int + * @throws SAXException if the attribute is missing + */ + public static int getAttributeAsInt(Attributes attributes, String name) throws SAXException { + String s = attributes.getValue(name); + if (s == null) { + throw new SAXException("Attribute '" + name + "' is missing"); + } else { + return Integer.parseInt(s); + } + } + + /** + * Returns an attribute value as a Integer value. + * @param attributes the Attributes object + * @param name the name of the attribute + * @return the attribute value as an Integer or null if the attribute is missing + */ + public static Integer getAttributeAsInteger(Attributes attributes, String name) { + String s = attributes.getValue(name); + if (s == null) { + return null; + } else { + return new Integer(s); + } + } + + /** + * Returns an attribute value as a Rectangle2D value. The string value is expected as 4 + * double-precision numbers separated by whitespace. + * @param attributes the Attributes object + * @param name the name of the attribute + * @return the attribute value as an Rectangle2D + */ + public static Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name) { + String s = attributes.getValue(name).trim(); + double[] values = ConversionUtils.toDoubleArray(s, "\\s"); + if (values.length != 4) { + throw new IllegalArgumentException("Rectangle must consist of 4 double values!"); + } + return new Rectangle2D.Double(values[0], values[1], values[2], values[3]); + } + + /** + * Returns an attribute value as a Rectangle value. The string value is expected as 4 + * integer numbers separated by whitespace. + * @param attributes the Attributes object + * @param name the name of the attribute + * @return the attribute value as an Rectangle + */ + public static Rectangle getAttributeAsRectangle(Attributes attributes, String name) { + String s = attributes.getValue(name); + if (s == null) { + return null; + } + int[] values = ConversionUtils.toIntArray(s.trim(), "\\s"); + if (values.length != 4) { + throw new IllegalArgumentException("Rectangle must consist of 4 int values!"); + } + return new Rectangle(values[0], values[1], values[2], values[3]); + } + + /** + * Returns an attribute value as a integer array. The string value is expected as 4 + * integer numbers separated by whitespace. + * @param attributes the Attributes object + * @param name the name of the attribute + * @return the attribute value as an int array + */ + public static int[] getAttributeAsIntArray(Attributes attributes, String name) { + String s = attributes.getValue(name); + if (s == null) { + return null; + } else { + return ConversionUtils.toIntArray(s.trim(), "\\s"); + } + } + +} |