aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2008-08-12 12:53:50 +0000
committerJeremias Maerki <jeremias@apache.org>2008-08-12 12:53:50 +0000
commit94b370293612d2ccba4f41c7fffddc4be9d10f75 (patch)
treefff42ad718169edc0acf6417bff7da6b0fda84e7 /src
parent7c7db27451c36cc6ace1c3082bd2ddf0a3c1a5cd (diff)
downloadxmlgraphics-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')
-rw-r--r--src/java/org/apache/fop/area/AreaTreeParser.java125
-rw-r--r--src/java/org/apache/fop/fo/XMLObj.java12
-rw-r--r--src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java17
-rw-r--r--src/java/org/apache/fop/render/intermediate/IFConstants.java15
-rw-r--r--src/java/org/apache/fop/render/intermediate/IFParser.java78
-rw-r--r--src/java/org/apache/fop/render/intermediate/IFSerializer.java72
-rw-r--r--src/java/org/apache/fop/svg/SVGUtilities.java11
-rw-r--r--src/java/org/apache/fop/util/DOM2SAX.java2
-rw-r--r--src/java/org/apache/fop/util/XMLConstants.java49
-rw-r--r--src/java/org/apache/fop/util/XMLUtil.java149
-rw-r--r--src/sandbox/org/apache/fop/render/svg/SVGConstants.java11
11 files changed, 327 insertions, 214 deletions
diff --git a/src/java/org/apache/fop/area/AreaTreeParser.java b/src/java/org/apache/fop/area/AreaTreeParser.java
index ab41b4214..65587ec3d 100644
--- a/src/java/org/apache/fop/area/AreaTreeParser.java
+++ b/src/java/org/apache/fop/area/AreaTreeParser.java
@@ -36,8 +36,22 @@ import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+import org.apache.xmlgraphics.util.QName;
+
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.area.Trait.Background;
import org.apache.fop.area.Trait.InternalLink;
@@ -65,16 +79,7 @@ import org.apache.fop.util.ContentHandlerFactory;
import org.apache.fop.util.ContentHandlerFactoryRegistry;
import org.apache.fop.util.ConversionUtils;
import org.apache.fop.util.DefaultErrorListener;
-import org.apache.xmlgraphics.image.loader.ImageInfo;
-import org.apache.xmlgraphics.image.loader.ImageManager;
-import org.apache.xmlgraphics.image.loader.ImageSessionContext;
-import org.apache.xmlgraphics.util.QName;
-import org.w3c.dom.DOMImplementation;
-import org.w3c.dom.Document;
-import org.xml.sax.Attributes;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
+import org.apache.fop.util.XMLUtil;
/**
* This is a parser for the area tree XML (intermediate format) which is used to reread an area
@@ -381,12 +386,12 @@ public class AreaTreeParser {
if (currentPageViewport != null) {
throw new IllegalStateException("currentPageViewport must be null");
}
- Rectangle2D viewArea = getAttributeAsRectangle2D(attributes, "bounds");
- int pageNumber = getAttributeAsInteger(attributes, "nr", -1);
+ Rectangle2D viewArea = XMLUtil.getAttributeAsRectangle2D(attributes, "bounds");
+ int pageNumber = XMLUtil.getAttributeAsInt(attributes, "nr", -1);
String key = attributes.getValue("key");
String pageNumberString = attributes.getValue("formatted-nr");
String pageMaster = attributes.getValue("simple-page-master-name");
- boolean blank = getAttributeAsBoolean(attributes, "blank", false);
+ boolean blank = XMLUtil.getAttributeAsBoolean(attributes, "blank", false);
currentPageViewport = new PageViewport(viewArea,
pageNumber, pageNumberString,
pageMaster, blank);
@@ -417,10 +422,10 @@ public class AreaTreeParser {
if (rv != null) {
throw new IllegalStateException("Current RegionViewport must be null");
}
- Rectangle2D viewArea = getAttributeAsRectangle2D(attributes, "rect");
+ Rectangle2D viewArea = XMLUtil.getAttributeAsRectangle2D(attributes, "rect");
rv = new RegionViewport(viewArea);
transferForeignObjects(attributes, rv);
- rv.setClip(getAttributeAsBoolean(attributes, "clipped", false));
+ rv.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clipped", false));
setAreaAttributes(attributes, rv);
setTraits(attributes, rv, SUBSET_COMMON);
setTraits(attributes, rv, SUBSET_BOX);
@@ -485,8 +490,8 @@ public class AreaTreeParser {
throw new IllegalStateException("Current BodyRegion must be null");
}
String regionName = attributes.getValue("name");
- int columnCount = getAttributeAsInteger(attributes, "columnCount", 1);
- int columnGap = getAttributeAsInteger(attributes, "columnGap", 0);
+ int columnCount = XMLUtil.getAttributeAsInt(attributes, "columnCount", 1);
+ int columnGap = XMLUtil.getAttributeAsInt(attributes, "columnGap", 0);
RegionViewport rv = getCurrentRegionViewport();
body = new BodyRegion(Constants.FO_REGION_BODY,
regionName, rv, columnCount, columnGap);
@@ -537,8 +542,8 @@ public class AreaTreeParser {
private class SpanMaker extends AbstractMaker {
public void startElement(Attributes attributes) {
- int ipd = getAttributeAsInteger(attributes, "ipd", 0);
- int columnCount = getAttributeAsInteger(attributes, "columnCount", 1);
+ int ipd = XMLUtil.getAttributeAsInt(attributes, "ipd", 0);
+ int columnCount = XMLUtil.getAttributeAsInt(attributes, "columnCount", 1);
BodyRegion body = getCurrentBodyRegion();
Span span = new Span(columnCount,
body.getColumnGap(), ipd);
@@ -554,7 +559,7 @@ public class AreaTreeParser {
public void startElement(Attributes attributes) {
Footnote fn = getCurrentBodyRegion().getFootnote();
transferForeignObjects(attributes, fn);
- fn.setTop(getAttributeAsInteger(attributes, "top-offset", 0));
+ fn.setTop(XMLUtil.getAttributeAsInt(attributes, "top-offset", 0));
areaStack.push(fn);
}
@@ -579,18 +584,18 @@ public class AreaTreeParser {
private class BlockMaker extends AbstractMaker {
public void startElement(Attributes attributes) {
- boolean isViewport = getAttributeAsBoolean(attributes,
+ boolean isViewport = XMLUtil.getAttributeAsBoolean(attributes,
"is-viewport-area", false);
Block block;
if (isViewport) {
BlockViewport bv = new BlockViewport();
- bv.setClip(getAttributeAsBoolean(attributes, "clipped", false));
+ bv.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clipped", false));
bv.setCTM(getAttributeAsCTM(attributes, "ctm"));
if (bv.getPositioning() != BlockViewport.RELATIVE) {
bv.setXOffset(
- getAttributeAsInteger(attributes, "left-position", 0));
+ XMLUtil.getAttributeAsInt(attributes, "left-position", 0));
bv.setYOffset(
- getAttributeAsInteger(attributes, "top-position", 0));
+ XMLUtil.getAttributeAsInt(attributes, "top-position", 0));
}
block = bv;
} else {
@@ -607,10 +612,10 @@ public class AreaTreeParser {
block.setPositioning(Block.STACK);
}
if (attributes.getValue("left-offset") != null) {
- block.setXOffset(getAttributeAsInteger(attributes, "left-offset", 0));
+ block.setXOffset(XMLUtil.getAttributeAsInt(attributes, "left-offset", 0));
}
if (attributes.getValue("top-offset") != null) {
- block.setYOffset(getAttributeAsInteger(attributes, "top-offset", 0));
+ block.setYOffset(XMLUtil.getAttributeAsInt(attributes, "top-offset", 0));
}
transferForeignObjects(attributes, block);
setAreaAttributes(attributes, block);
@@ -652,7 +657,7 @@ public class AreaTreeParser {
public void startElement(Attributes attributes) {
InlineArea inl = new InlineArea();
transferForeignObjects(attributes, inl);
- inl.setOffset(getAttributeAsInteger(attributes, "offset", 0));
+ inl.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
setAreaAttributes(attributes, inl);
setTraits(attributes, inl, SUBSET_COMMON);
setTraits(attributes, inl, SUBSET_BOX);
@@ -672,7 +677,7 @@ public class AreaTreeParser {
public void startElement(Attributes attributes) {
InlineParent ip = new InlineParent();
transferForeignObjects(attributes, ip);
- ip.setOffset(getAttributeAsInteger(attributes, "offset", 0));
+ ip.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
setAreaAttributes(attributes, ip);
setTraits(attributes, ip, SUBSET_COMMON);
setTraits(attributes, ip, SUBSET_BOX);
@@ -693,7 +698,7 @@ public class AreaTreeParser {
public void startElement(Attributes attributes) {
InlineBlockParent ibp = new InlineBlockParent();
transferForeignObjects(attributes, ibp);
- ibp.setOffset(getAttributeAsInteger(attributes, "offset", 0));
+ ibp.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
setAreaAttributes(attributes, ibp);
setTraits(attributes, ibp, SUBSET_COMMON);
setTraits(attributes, ibp, SUBSET_BOX);
@@ -720,11 +725,11 @@ public class AreaTreeParser {
setTraits(attributes, text, SUBSET_BOX);
setTraits(attributes, text, SUBSET_COLOR);
setTraits(attributes, text, SUBSET_FONT);
- text.setBaselineOffset(getAttributeAsInteger(attributes, "baseline", 0));
- text.setOffset(getAttributeAsInteger(attributes, "offset", 0));
- text.setTextLetterSpaceAdjust(getAttributeAsInteger(attributes,
+ text.setBaselineOffset(XMLUtil.getAttributeAsInt(attributes, "baseline", 0));
+ text.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
+ text.setTextLetterSpaceAdjust(XMLUtil.getAttributeAsInt(attributes,
"tlsadjust", 0));
- text.setTextWordSpaceAdjust(getAttributeAsInteger(attributes,
+ text.setTextWordSpaceAdjust(XMLUtil.getAttributeAsInt(attributes,
"twsadjust", 0));
Area parent = (Area)areaStack.peek();
parent.addChildArea(text);
@@ -739,7 +744,7 @@ public class AreaTreeParser {
private class WordMaker extends AbstractMaker {
public void endElement() {
- int offset = getAttributeAsInteger(lastAttributes, "offset", 0);
+ int offset = XMLUtil.getAttributeAsInt(lastAttributes, "offset", 0);
int[] letterAdjust
= ConversionUtils.toIntArray(
lastAttributes.getValue("letter-adjust"), "\\s");
@@ -758,11 +763,11 @@ public class AreaTreeParser {
private class SpaceMaker extends AbstractMaker {
public void endElement() {
- int offset = getAttributeAsInteger(lastAttributes, "offset", 0);
+ int offset = XMLUtil.getAttributeAsInt(lastAttributes, "offset", 0);
//TODO the isAdjustable parameter is currently not used/implemented
if (content.position() > 0) {
content.flip();
- boolean adjustable = getAttributeAsBoolean(lastAttributes, "adj", true);
+ boolean adjustable = XMLUtil.getAttributeAsBoolean(lastAttributes, "adj", true);
SpaceArea space = new SpaceArea(content.charAt(0), offset, adjustable);
AbstractTextArea text = getCurrentText();
space.setParentArea(text);
@@ -794,13 +799,13 @@ public class AreaTreeParser {
setTraits(attributes, leader, SUBSET_BOX);
setTraits(attributes, leader, SUBSET_COLOR);
setTraits(attributes, leader, SUBSET_FONT);
- leader.setOffset(getAttributeAsInteger(attributes, "offset", 0));
+ leader.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
String ruleStyle = attributes.getValue("ruleStyle");
if (ruleStyle != null) {
leader.setRuleStyle(ruleStyle);
}
leader.setRuleThickness(
- getAttributeAsInteger(attributes, "ruleThickness", 0));
+ XMLUtil.getAttributeAsInt(attributes, "ruleThickness", 0));
Area parent = (Area)areaStack.peek();
parent.addChildArea(leader);
}
@@ -815,9 +820,9 @@ public class AreaTreeParser {
setTraits(attributes, viewport, SUBSET_COMMON);
setTraits(attributes, viewport, SUBSET_BOX);
setTraits(attributes, viewport, SUBSET_COLOR);
- viewport.setContentPosition(getAttributeAsRectangle2D(attributes, "pos"));
- viewport.setClip(getAttributeAsBoolean(attributes, "clip", false));
- viewport.setOffset(getAttributeAsInteger(attributes, "offset", 0));
+ viewport.setContentPosition(XMLUtil.getAttributeAsRectangle2D(attributes, "pos"));
+ viewport.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clip", false));
+ viewport.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
Area parent = (Area)areaStack.peek();
parent.addChildArea(viewport);
areaStack.push(viewport);
@@ -884,7 +889,8 @@ public class AreaTreeParser {
public void startElement(Attributes attributes) {
String title = attributes.getValue("title");
- boolean showChildren = getAttributeAsBoolean(attributes, "show-children", false);
+ boolean showChildren = XMLUtil.getAttributeAsBoolean(
+ attributes, "show-children", false);
String[] linkdata
= InternalLink.parseXMLAttribute(attributes.getValue("internal-link"));
PageViewport pv = (PageViewport) pageViewportsByKey.get(linkdata[0]);
@@ -1044,9 +1050,9 @@ public class AreaTreeParser {
if (repeat != null) {
bkg.setRepeat(repeat);
}
- bkg.setHoriz(getAttributeAsInteger(attributes,
+ bkg.setHoriz(XMLUtil.getAttributeAsInt(attributes,
"bkg-horz-offset", 0));
- bkg.setVertical(getAttributeAsInteger(attributes,
+ bkg.setVertical(XMLUtil.getAttributeAsInt(attributes,
"bkg-vert-offset", 0));
}
area.addTrait(trait, bkg);
@@ -1058,7 +1064,7 @@ public class AreaTreeParser {
String fontName = attributes.getValue("font-name");
if (fontName != null) {
String fontStyle = attributes.getValue("font-style");
- int fontWeight = getAttributeAsInteger(
+ int fontWeight = XMLUtil.getAttributeAsInt(
attributes, "font-weight", Font.WEIGHT_NORMAL);
area.addTrait(trait,
FontInfo.createFontKey(fontName, fontStyle, fontWeight));
@@ -1068,26 +1074,6 @@ public class AreaTreeParser {
}
}
- private 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();
- }
- }
-
- private static int getAttributeAsInteger(Attributes attributes, String name,
- int defaultValue) {
- String s = attributes.getValue(name);
- if (s == null) {
- return defaultValue;
- } else {
- return Integer.parseInt(s);
- }
- }
-
private static CTM getAttributeAsCTM(Attributes attributes, String name) {
String s = attributes.getValue(name).trim();
if (s.startsWith("[") && s.endsWith("]")) {
@@ -1102,15 +1088,6 @@ public class AreaTreeParser {
}
}
- private 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]);
- }
-
private static void transferForeignObjects(Attributes atts, AreaTreeObject ato) {
for (int i = 0, c = atts.getLength(); i < c; i++) {
String ns = atts.getURI(i);
diff --git a/src/java/org/apache/fop/fo/XMLObj.java b/src/java/org/apache/fop/fo/XMLObj.java
index 3330f41a5..256700aa7 100644
--- a/src/java/org/apache/fop/fo/XMLObj.java
+++ b/src/java/org/apache/fop/fo/XMLObj.java
@@ -33,6 +33,7 @@ import org.xml.sax.Locator;
import org.apache.fop.apps.FOPException;
import org.apache.fop.datatypes.Length;
+import org.apache.fop.util.XMLConstants;
import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
/**
@@ -41,8 +42,6 @@ import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
*/
public abstract class XMLObj extends FONode implements ObjectBuiltListener {
- private static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
-
// temp reference for attributes
private Attributes attr = null;
@@ -116,7 +115,7 @@ public abstract class XMLObj extends FONode implements ObjectBuiltListener {
private static HashMap ns = new HashMap();
static {
- ns.put("xlink", "http://www.w3.org/1999/xlink");
+ ns.put(XMLConstants.XLINK_PREFIX, XMLConstants.XLINK_NAMESPACE);
}
/**
@@ -143,7 +142,7 @@ public abstract class XMLObj extends FONode implements ObjectBuiltListener {
} else {
String pref = qname.substring(0, idx);
String tail = qname.substring(idx + 1);
- if (pref.equals("xmlns")) {
+ if (pref.equals(XMLConstants.XMLNS_PREFIX)) {
ns.put(tail, rf);
} else {
element.setAttributeNS((String)ns.get(pref), tail, rf);
@@ -181,8 +180,9 @@ public abstract class XMLObj extends FONode implements ObjectBuiltListener {
element = doc.getDocumentElement();
buildTopLevel(doc, element);
- if (!element.hasAttributeNS(XMLNS_NAMESPACE_URI, "xmlns")) {
- element.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns",
+ if (!element.hasAttributeNS(
+ XMLConstants.XMLNS_NAMESPACE_URI, XMLConstants.XMLNS_PREFIX)) {
+ element.setAttributeNS(XMLConstants.XMLNS_NAMESPACE_URI, XMLConstants.XMLNS_PREFIX,
getNamespaceURI());
}
diff --git a/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java b/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java
index 63238354b..1c90c47f4 100644
--- a/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java
+++ b/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java
@@ -39,6 +39,7 @@ import org.apache.xmlgraphics.util.QName;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.util.DecimalFormatCache;
+import org.apache.fop.util.XMLUtil;
/**
* Abstract base class for XML-writing IFPainter implementations.
@@ -47,9 +48,6 @@ public abstract class AbstractXMLWritingIFPainter extends AbstractIFPainter {
private static final Attributes EMPTY_ATTS = new AttributesImpl();
- /** Constant for the "CDATA" attribute type. */
- protected static final String CDATA = "CDATA";
-
/**
* Default SAXTransformerFactory that can be used by subclasses.
*/
@@ -228,7 +226,18 @@ public abstract class AbstractXMLWritingIFPainter extends AbstractIFPainter {
*/
protected void addAttribute(AttributesImpl atts, QName attribute, String value) {
atts.addAttribute(attribute.getNamespaceURI(),
- attribute.getLocalName(), attribute.getQName(), CDATA, value);
+ attribute.getLocalName(), attribute.getQName(), XMLUtil.CDATA, value);
+ }
+
+ /**
+ * Adds an attribute to a given {@code AttributesImpl} instance. The attribute will be
+ * added in the default namespace.
+ * @param atts the attributes collection
+ * @param localName the local name of the attribute
+ * @param value the attribute's CDATA value
+ */
+ protected void addAttribute(AttributesImpl atts, String localName, String value) {
+ atts.addAttribute("", localName, localName, XMLUtil.CDATA, value);
}
/**
diff --git a/src/java/org/apache/fop/render/intermediate/IFConstants.java b/src/java/org/apache/fop/render/intermediate/IFConstants.java
index b90e77d07..60870fbd8 100644
--- a/src/java/org/apache/fop/render/intermediate/IFConstants.java
+++ b/src/java/org/apache/fop/render/intermediate/IFConstants.java
@@ -19,14 +19,13 @@
package org.apache.fop.render.intermediate;
-import org.apache.xmlgraphics.util.QName;
-
import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.util.XMLConstants;
/**
* Constants for the intermediate format.
*/
-public interface IFConstants {
+public interface IFConstants extends XMLConstants {
/** MIME type of the intermediate format. */
String MIME_TYPE = MimeConstants.MIME_FOP_IF;
@@ -34,16 +33,6 @@ public interface IFConstants {
/** XML namespace of the intermediate format. */
String NAMESPACE = "http://xmlgraphics.apache.org/fop/intermediate";
- /** XML namespace. */
- String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
-
- /** Namespace prefix for XLink */
- String XLINK_PREFIX = "xlink";
- /** XML namespace for XLink */
- String XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
- /** xlink:href attribute */
- QName XLINK_HREF = new QName(XLINK_NAMESPACE, XLINK_PREFIX, "href");
-
String EL_DOCUMENT = "document";
String EL_HEADER = "header";
String EL_PAGE_SEQUENCE = "page-sequence";
diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java
index ef0efacb2..6545371fd 100644
--- a/src/java/org/apache/fop/render/intermediate/IFParser.java
+++ b/src/java/org/apache/fop/render/intermediate/IFParser.java
@@ -23,7 +23,6 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
-import java.awt.geom.Rectangle2D;
import java.util.Map;
import javax.xml.transform.Source;
@@ -47,14 +46,15 @@ import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.util.QName;
import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.fo.ElementMapping;
import org.apache.fop.fo.ElementMappingRegistry;
import org.apache.fop.fo.expr.PropertyException;
import org.apache.fop.util.ColorUtil;
import org.apache.fop.util.ContentHandlerFactory;
import org.apache.fop.util.ContentHandlerFactoryRegistry;
-import org.apache.fop.util.ConversionUtils;
import org.apache.fop.util.DOMBuilderContentHandlerFactory;
import org.apache.fop.util.DefaultErrorListener;
+import org.apache.fop.util.XMLUtil;
/**
* This is a parser for the intermediate format XML which converts the intermediate file into
@@ -173,8 +173,11 @@ public class IFParser implements IFConstants {
DOMImplementation domImplementation
= elementMappingRegistry.getDOMImplementationForNamespace(uri);
if (domImplementation == null) {
+ domImplementation = ElementMapping.getDefaultDOMImplementation();
+ /*
throw new SAXException("No DOMImplementation could be"
+ " identified to handle namespace: " + uri);
+ */
}
factory = new DOMBuilderContentHandlerFactory(uri, domImplementation);
}
@@ -363,7 +366,7 @@ public class IFParser implements IFConstants {
= AffineTransformArrayParser.createAffineTransform(transform);
int width = Integer.parseInt(attributes.getValue("width"));
int height = Integer.parseInt(attributes.getValue("height"));
- Rectangle clipRect = getAttributeAsRectangle(attributes, "clip-rect");
+ Rectangle clipRect = XMLUtil.getAttributeAsRectangle(attributes, "clip-rect");
painter.startViewport(transforms, new Dimension(width, height), clipRect);
}
@@ -393,9 +396,9 @@ public class IFParser implements IFConstants {
public void startElement(Attributes attributes) throws IFException {
String family = attributes.getValue("family");
String style = attributes.getValue("style");
- Integer weight = getAttributeAsInteger(attributes, "weight");
+ Integer weight = XMLUtil.getAttributeAsInteger(attributes, "weight");
String variant = attributes.getValue("variant");
- Integer size = getAttributeAsInteger(attributes, "size");
+ Integer size = XMLUtil.getAttributeAsInteger(attributes, "size");
Color color;
try {
color = getAttributeAsColor(attributes, "color");
@@ -412,8 +415,8 @@ public class IFParser implements IFConstants {
public void endElement() throws IFException {
int x = Integer.parseInt(lastAttributes.getValue("x"));
int y = Integer.parseInt(lastAttributes.getValue("y"));
- int[] dx = getAttributeAsIntArray(lastAttributes, "dx");
- int[] dy = getAttributeAsIntArray(lastAttributes, "dy");
+ int[] dx = XMLUtil.getAttributeAsIntArray(lastAttributes, "dx");
+ int[] dy = XMLUtil.getAttributeAsIntArray(lastAttributes, "dy");
painter.drawText(x, y, dx, dy, content.toString());
}
@@ -504,37 +507,8 @@ public class IFParser implements IFConstants {
}
}
- private 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();
- }
- }
-
- private static int getAttributeAsInteger(Attributes attributes, String name,
- int defaultValue) {
- String s = attributes.getValue(name);
- if (s == null) {
- return defaultValue;
- } else {
- return Integer.parseInt(s);
- }
- }
-
- private static Integer getAttributeAsInteger(Attributes attributes, String name) {
- String s = attributes.getValue(name);
- if (s == null) {
- return null;
- } else {
- return new Integer(s);
- }
- }
-
private Color getAttributeAsColor(Attributes attributes, String name)
- throws PropertyException {
+ throws PropertyException {
String s = attributes.getValue(name);
if (s == null) {
return null;
@@ -543,36 +517,6 @@ public class IFParser implements IFConstants {
}
}
- private 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]);
- }
-
- private 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]);
- }
-
- private static int[] getAttributeAsIntArray(Attributes attributes, String name) {
- String s = attributes.getValue(name);
- if (s == null) {
- return null;
- } else {
- return ConversionUtils.toIntArray(s.trim(), "\\s");
- }
- }
-
private static Map getForeignAttributes(Attributes atts) {
Map foreignAttributes = null;
for (int i = 0, c = atts.getLength(); i < c; i++) {
diff --git a/src/java/org/apache/fop/render/intermediate/IFSerializer.java b/src/java/org/apache/fop/render/intermediate/IFSerializer.java
index 509746de6..94eb50c56 100644
--- a/src/java/org/apache/fop/render/intermediate/IFSerializer.java
+++ b/src/java/org/apache/fop/render/intermediate/IFSerializer.java
@@ -38,6 +38,7 @@ import org.apache.xmlgraphics.util.XMLizable;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.util.ColorUtil;
import org.apache.fop.util.DOM2SAX;
+import org.apache.fop.util.XMLUtil;
/**
* IFPainter implementation that serializes the intermediate format to XML.
@@ -112,7 +113,7 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
try {
AttributesImpl atts = new AttributesImpl();
if (id != null) {
- atts.addAttribute(XML_NAMESPACE, "id", "xml:id", CDATA, id);
+ atts.addAttribute(XML_NAMESPACE, "id", "xml:id", XMLUtil.CDATA, id);
}
startElement(EL_PAGE_SEQUENCE, atts);
} catch (SAXException e) {
@@ -133,10 +134,10 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
public void startPage(int index, String name, Dimension size) throws IFException {
try {
AttributesImpl atts = new AttributesImpl();
- atts.addAttribute("", "index", "index", CDATA, Integer.toString(index));
- atts.addAttribute("", "name", "name", CDATA, name);
- atts.addAttribute("", "width", "width", CDATA, Integer.toString(size.width));
- atts.addAttribute("", "height", "height", CDATA, Integer.toString(size.height));
+ addAttribute(atts, "index", Integer.toString(index));
+ addAttribute(atts, "name", name);
+ addAttribute(atts, "width", Integer.toString(size.width));
+ addAttribute(atts, "height", Integer.toString(size.height));
startElement(EL_PAGE, atts);
} catch (SAXException e) {
throw new IFException("SAX error in startPage()", e);
@@ -223,12 +224,12 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
try {
AttributesImpl atts = new AttributesImpl();
if (transform != null && transform.length() > 0) {
- atts.addAttribute("", "transform", "transform", CDATA, transform);
+ addAttribute(atts,"transform", transform);
}
- atts.addAttribute("", "width", "width", CDATA, Integer.toString(size.width));
- atts.addAttribute("", "height", "height", CDATA, Integer.toString(size.height));
+ addAttribute(atts, "width", Integer.toString(size.width));
+ addAttribute(atts, "height", Integer.toString(size.height));
if (clipRect != null) {
- atts.addAttribute("", "clip-rect", "clip-rect", CDATA, toString(clipRect));
+ addAttribute(atts, "clip-rect", toString(clipRect));
}
startElement(EL_VIEWPORT, atts);
} catch (SAXException e) {
@@ -259,7 +260,7 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
try {
AttributesImpl atts = new AttributesImpl();
if (transform != null && transform.length() > 0) {
- atts.addAttribute("", "transform", "transform", CDATA, transform);
+ addAttribute(atts, "transform", transform);
}
startElement(EL_GROUP, atts);
} catch (SAXException e) {
@@ -281,10 +282,10 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
try {
AttributesImpl atts = new AttributesImpl();
addAttribute(atts, XLINK_HREF, uri);
- atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
- atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
- atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
- atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
+ addAttribute(atts, "x", Integer.toString(rect.x));
+ addAttribute(atts, "y", Integer.toString(rect.y));
+ addAttribute(atts, "width", Integer.toString(rect.width));
+ addAttribute(atts, "height", Integer.toString(rect.height));
if (foreignAttributes != null) {
Iterator iter = foreignAttributes.entrySet().iterator();
while (iter.hasNext()) {
@@ -302,10 +303,10 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
public void drawImage(Document doc, Rectangle rect, Map foreignAttributes) throws IFException {
try {
AttributesImpl atts = new AttributesImpl();
- atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
- atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
- atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
- atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
+ addAttribute(atts, "x", Integer.toString(rect.x));
+ addAttribute(atts, "y", Integer.toString(rect.y));
+ addAttribute(atts, "width", Integer.toString(rect.width));
+ addAttribute(atts, "height", Integer.toString(rect.height));
if (foreignAttributes != null) {
Iterator iter = foreignAttributes.entrySet().iterator();
while (iter.hasNext()) {
@@ -342,15 +343,15 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
}
try {
AttributesImpl atts = new AttributesImpl();
- atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
- atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
- atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
- atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
+ addAttribute(atts, "x", Integer.toString(rect.x));
+ addAttribute(atts, "y", Integer.toString(rect.y));
+ addAttribute(atts, "width", Integer.toString(rect.width));
+ addAttribute(atts, "height", Integer.toString(rect.height));
if (fill != null) {
- atts.addAttribute("", "fill", "fill", CDATA, toString(fill));
+ addAttribute(atts, "fill", toString(fill));
}
if (stroke != null) {
- atts.addAttribute("", "stroke", "sroke", CDATA, toString(stroke));
+ addAttribute(atts, "sroke", toString(stroke));
}
element("rect", atts);
} catch (SAXException e) {
@@ -362,13 +363,13 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
public void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException {
try {
AttributesImpl atts = new AttributesImpl();
- atts.addAttribute("", "x", "x", CDATA, Integer.toString(x));
- atts.addAttribute("", "y", "y", CDATA, Integer.toString(y));
+ addAttribute(atts, "x", Integer.toString(x));
+ addAttribute(atts, "y", Integer.toString(y));
if (dx != null) {
- atts.addAttribute("", "dx", "dx", CDATA, toString(dx));
+ addAttribute(atts, "dx", toString(dx));
}
if (dy != null) {
- atts.addAttribute("", "dy", "dy", CDATA, toString(dy));
+ addAttribute(atts, "dy", toString(dy));
}
startElement("text", atts);
char[] chars = text.toCharArray();
@@ -385,22 +386,22 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
try {
AttributesImpl atts = new AttributesImpl();
if (family != null) {
- atts.addAttribute("", "family", "family", CDATA, family);
+ addAttribute(atts, "family", family);
}
if (style != null) {
- atts.addAttribute("", "style", "style", CDATA, style);
+ addAttribute(atts, "style", style);
}
if (weight != null) {
- atts.addAttribute("", "weight", "weight", CDATA, weight.toString());
+ addAttribute(atts, "weight", weight.toString());
}
if (variant != null) {
- atts.addAttribute("", "variant", "variant", CDATA, variant);
+ addAttribute(atts, "variant", variant);
}
if (size != null) {
- atts.addAttribute("", "size", "size", CDATA, size.toString());
+ addAttribute(atts, "size", size.toString());
}
if (color != null) {
- atts.addAttribute("", "color", "color", CDATA, toString(color));
+ addAttribute(atts, "color", toString(color));
}
element("font", atts);
} catch (SAXException e) {
@@ -418,7 +419,8 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst
}
} else {
throw new UnsupportedOperationException(
- "Don't know how to handle extension object: " + extension);
+ "Don't know how to handle extension object: "
+ + extension + " (" + extension.getClass().getName() + ")");
}
}
diff --git a/src/java/org/apache/fop/svg/SVGUtilities.java b/src/java/org/apache/fop/svg/SVGUtilities.java
index cb07ab1f1..97cd31eed 100644
--- a/src/java/org/apache/fop/svg/SVGUtilities.java
+++ b/src/java/org/apache/fop/svg/SVGUtilities.java
@@ -19,16 +19,17 @@
package org.apache.fop.svg;
-import java.util.StringTokenizer;
+import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
-import java.awt.font.FontRenderContext;
+import java.util.StringTokenizer;
-import org.w3c.dom.Element;
-import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.apache.batik.dom.svg.SVGDOMImplementation;
+import org.apache.batik.util.XMLConstants;
/**
* Some utilities for creating svg DOM documents and elements.
@@ -218,7 +219,7 @@ public class SVGUtilities {
public static final Element createImage(Document doc, String ref,
float width, float height) {
Element border = doc.createElementNS(SVG_NS, "image");
- border.setAttributeNS("http://www.w3.org/1999/xlink", "href",
+ border.setAttributeNS(XMLConstants.XLINK_NAMESPACE_URI, "href",
ref);
border.setAttributeNS(null, "width", "" + width);
border.setAttributeNS(null, "height", "" + height);
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");
+ }
+ }
+
+}
diff --git a/src/sandbox/org/apache/fop/render/svg/SVGConstants.java b/src/sandbox/org/apache/fop/render/svg/SVGConstants.java
index 54051faf2..46e0b8da8 100644
--- a/src/sandbox/org/apache/fop/render/svg/SVGConstants.java
+++ b/src/sandbox/org/apache/fop/render/svg/SVGConstants.java
@@ -22,11 +22,12 @@ package org.apache.fop.render.svg;
import org.apache.xmlgraphics.util.QName;
import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.util.XMLConstants;
/**
* Constants for the intermediate format.
*/
-public interface SVGConstants {
+public interface SVGConstants extends XMLConstants {
/** MIME type for SVG. */
String MIME_TYPE = MimeConstants.MIME_SVG;
@@ -40,14 +41,6 @@ public interface SVGConstants {
/** XML namespace for SVG. */
String NAMESPACE = "http://www.w3.org/2000/svg";
- /** XML namespace. */
- String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
-
- /** Namespace prefix for XLink */
- String XLINK_PREFIX = "xlink";
- /** XML namespace for XLink */
- String XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
-
/** the SVG element */
QName SVG_ELEMENT = new QName(NAMESPACE, null, "svg");