diff options
author | Vincent Hennebert <vhennebert@apache.org> | 2011-04-18 18:36:05 +0000 |
---|---|---|
committer | Vincent Hennebert <vhennebert@apache.org> | 2011-04-18 18:36:05 +0000 |
commit | 427df0fa7d2f67a4d21740cbf7264d3b11739a41 (patch) | |
tree | 42dba5cd4eaf64a6d2a361415b313a32b9cfbe0d /src | |
parent | 06a7b3804cdff092d2f3d4265e463b1c20454842 (diff) | |
download | xmlgraphics-fop-427df0fa7d2f67a4d21740cbf7264d3b11739a41.tar.gz xmlgraphics-fop-427df0fa7d2f67a4d21740cbf7264d3b11739a41.zip |
Added id element to intermediate format to track the origin of content.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1094690 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
6 files changed, 93 insertions, 12 deletions
diff --git a/src/documentation/intermediate-format-ng/fop-intermediate-format-ng-content.xsd b/src/documentation/intermediate-format-ng/fop-intermediate-format-ng-content.xsd index 4e0b7d115..ac7357f8c 100644 --- a/src/documentation/intermediate-format-ng/fop-intermediate-format-ng-content.xsd +++ b/src/documentation/intermediate-format-ng/fop-intermediate-format-ng-content.xsd @@ -42,6 +42,11 @@ </xs:complexContent> </xs:complexType> </xs:element> + <xs:element name="id"> + <xs:complexType> + <xs:attribute name="name" use="required" type="xs:string"/> + </xs:complexType> + </xs:element> <xs:element name="font"> <xs:complexType> <xs:attribute name="family" type="xs:string"/> diff --git a/src/java/org/apache/fop/render/intermediate/IFConstants.java b/src/java/org/apache/fop/render/intermediate/IFConstants.java index 34fe2bd2c..2b16b343d 100644 --- a/src/java/org/apache/fop/render/intermediate/IFConstants.java +++ b/src/java/org/apache/fop/render/intermediate/IFConstants.java @@ -67,6 +67,8 @@ public interface IFConstants extends XMLConstants { String EL_FONT = "font"; /** element name text */ String EL_TEXT = "text"; + /** element name id */ + String EL_ID = "id"; /** Parent element of the logical structure tree. */ String EL_STRUCTURE_TREE = "structure-tree"; } diff --git a/src/java/org/apache/fop/render/intermediate/IFContext.java b/src/java/org/apache/fop/render/intermediate/IFContext.java index 804b353c1..b8be97253 100644 --- a/src/java/org/apache/fop/render/intermediate/IFContext.java +++ b/src/java/org/apache/fop/render/intermediate/IFContext.java @@ -48,6 +48,8 @@ public class IFContext { private String structurePointer; + private String id = ""; + /** * Main constructor. * @param ua the user agent @@ -155,4 +157,23 @@ public class IFContext { return this.structurePointer; } + /** + * Sets the ID of the object enclosing the content that will follow. + * + * @param id the ID of the nearest ancestor object for which the id property was set + */ + void setID(String id) { + assert id != null; + this.id = id; + } + + /** + * Returns the ID of the object enclosing the current content. + * + * @return the ID of the nearest ancestor object for which the id property was set + */ + String getID() { + return id; + } + } diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java index 645d1ebc6..313cee685 100644 --- a/src/java/org/apache/fop/render/intermediate/IFParser.java +++ b/src/java/org/apache/fop/render/intermediate/IFParser.java @@ -24,6 +24,7 @@ import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.AffineTransform; +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -76,7 +77,7 @@ public class IFParser implements IFConstants { private static SAXTransformerFactory tFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); - private static Set handledNamespaces = new java.util.HashSet(); + private static Set<String> handledNamespaces = new java.util.HashSet<String>(); static { handledNamespaces.add(XMLNS_NAMESPACE_URI); @@ -132,7 +133,7 @@ public class IFParser implements IFConstants { private static class Handler extends DefaultHandler { - private Map elementHandlers = new java.util.HashMap(); + private Map<String, ElementHandler> elementHandlers = new HashMap<String, ElementHandler>(); private IFDocumentHandler documentHandler; private IFPainter painter; @@ -188,6 +189,7 @@ public class IFParser implements IFConstants { //Page content elementHandlers.put(EL_VIEWPORT, new ViewportHandler()); elementHandlers.put(EL_GROUP, new GroupHandler()); + elementHandlers.put(EL_ID, new IDHandler()); elementHandlers.put(EL_FONT, new FontHandler()); elementHandlers.put(EL_TEXT, new TextHandler()); elementHandlers.put(EL_CLIP_RECT, new ClipRectHandler()); @@ -202,7 +204,7 @@ public class IFParser implements IFConstants { } } - private void establishForeignAttributes(Map foreignAttributes) { + private void establishForeignAttributes(Map<QName, String> foreignAttributes) { documentHandler.getContext().setForeignAttributes(foreignAttributes); } @@ -300,7 +302,7 @@ public class IFParser implements IFConstants { private boolean startIFElement(String localName, Attributes attributes) throws SAXException { lastAttributes = new AttributesImpl(attributes); - ElementHandler elementHandler = (ElementHandler)elementHandlers.get(localName); + ElementHandler elementHandler = elementHandlers.get(localName); content.setLength(0); ignoreCharacters = true; if (elementHandler != null) { @@ -346,7 +348,7 @@ public class IFParser implements IFConstants { } } else { if (NAMESPACE.equals(uri)) { - ElementHandler elementHandler = (ElementHandler)elementHandlers.get(localName); + ElementHandler elementHandler = elementHandlers.get(localName); if (elementHandler != null) { try { elementHandler.endElement(); @@ -432,7 +434,7 @@ public class IFParser implements IFConstants { documentHandler.getContext().setLanguage( XMLUtil.convertRFC3066ToLocale(xmllang)); } - Map foreignAttributes = getForeignAttributes(lastAttributes); + Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes); establishForeignAttributes(foreignAttributes); documentHandler.startPageSequence(id); resetForeignAttributes(); @@ -453,7 +455,7 @@ public class IFParser implements IFConstants { String pageMasterName = attributes.getValue("page-master-name"); int width = Integer.parseInt(attributes.getValue("width")); int height = Integer.parseInt(attributes.getValue("height")); - Map foreignAttributes = getForeignAttributes(lastAttributes); + Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes); establishForeignAttributes(foreignAttributes); documentHandler.startPage(index, name, pageMasterName, new Dimension(width, height)); @@ -486,6 +488,7 @@ public class IFParser implements IFConstants { public void endElement() throws IFException { painter = null; + documentHandler.getContext().setID(""); documentHandler.endPageContent(); } @@ -536,6 +539,16 @@ public class IFParser implements IFConstants { } + private class IDHandler extends AbstractElementHandler { + + @Override + public void startElement(Attributes attributes) throws IFException, SAXException { + String id = attributes.getValue("name"); + documentHandler.getContext().setID(id); + } + + } + private class FontHandler extends AbstractElementHandler { public void startElement(Attributes attributes) throws IFException { @@ -660,7 +673,7 @@ public class IFParser implements IFConstants { int y = Integer.parseInt(lastAttributes.getValue("y")); int width = Integer.parseInt(lastAttributes.getValue("width")); int height = Integer.parseInt(lastAttributes.getValue("height")); - Map foreignAttributes = getForeignAttributes(lastAttributes); + Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes); establishForeignAttributes(foreignAttributes); setStructurePointer(lastAttributes); if (foreignObject != null) { @@ -712,8 +725,8 @@ public class IFParser implements IFConstants { } } - private static Map getForeignAttributes(Attributes atts) { - Map foreignAttributes = null; + private static Map<QName, String> getForeignAttributes(Attributes atts) { + Map<QName, String> foreignAttributes = null; for (int i = 0, c = atts.getLength(); i < c; i++) { String ns = atts.getURI(i); if (ns.length() > 0) { @@ -721,7 +734,7 @@ public class IFParser implements IFConstants { continue; } if (foreignAttributes == null) { - foreignAttributes = new java.util.HashMap(); + foreignAttributes = new java.util.HashMap<QName, String>(); } QName qname = new QName(ns, atts.getQName(i)); foreignAttributes.put(qname, atts.getValue(i)); diff --git a/src/java/org/apache/fop/render/intermediate/IFRenderer.java b/src/java/org/apache/fop/render/intermediate/IFRenderer.java index c81a5cae9..24a2e8a75 100644 --- a/src/java/org/apache/fop/render/intermediate/IFRenderer.java +++ b/src/java/org/apache/fop/render/intermediate/IFRenderer.java @@ -153,6 +153,8 @@ public class IFRenderer extends AbstractPathOrientedRenderer { private TextUtil textUtil = new TextUtil(); + private Stack<String> ids = new Stack<String>(); + /** * Main constructor */ @@ -841,11 +843,13 @@ public class IFRenderer extends AbstractPathOrientedRenderer { public void renderInlineViewport(InlineViewport viewport) { String ptr = (String) viewport.getTrait(Trait.PTR); establishStructurePointer(ptr); + pushdID(viewport); Dimension dim = new Dimension(viewport.getIPD(), viewport.getBPD()); viewportDimensionStack.push(dim); super.renderInlineViewport(viewport); viewportDimensionStack.pop(); resetStructurePointer(); + popID(viewport); } /** {@inheritDoc} */ @@ -888,7 +892,9 @@ public class IFRenderer extends AbstractPathOrientedRenderer { /** {@inheritDoc} */ protected void renderInlineArea(InlineArea inlineArea) { saveInlinePosIfTargetable(inlineArea); + pushdID(inlineArea); super.renderInlineArea(inlineArea); + popID(inlineArea); } /** {@inheritDoc} */ @@ -952,7 +958,25 @@ public class IFRenderer extends AbstractPathOrientedRenderer { log.trace("renderBlock() " + block); } saveBlockPosIfTargetable(block); + pushdID(block); super.renderBlock(block); + popID(block); + } + + private void pushdID(Area area) { + String prodID = (String) area.getTrait(Trait.PROD_ID); + if (prodID != null) { + ids.push(prodID); + documentHandler.getContext().setID(prodID); + } + } + + private void popID(Area area) { + String prodID = (String) area.getTrait(Trait.PROD_ID); + if (prodID != null) { + ids.pop(); + documentHandler.getContext().setID(ids.empty() ? "" : ids.peek()); + } } private Typeface getTypeface(String fontName) { diff --git a/src/java/org/apache/fop/render/intermediate/IFSerializer.java b/src/java/org/apache/fop/render/intermediate/IFSerializer.java index d906d4189..471a982ca 100644 --- a/src/java/org/apache/fop/render/intermediate/IFSerializer.java +++ b/src/java/org/apache/fop/render/intermediate/IFSerializer.java @@ -69,6 +69,8 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** Holds the intermediate format state */ private IFState state; + private String currentID = ""; + /** * Default constructor. */ @@ -306,6 +308,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler public void endPageContent() throws IFException { try { this.state = null; + currentID = ""; handler.endElement(EL_PAGE_CONTENT); } catch (SAXException e) { throw new IFException("SAX error in endPageContent()", e); @@ -417,6 +420,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void drawImage(String uri, Rectangle rect) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, XLINK_HREF, uri); addAttribute(atts, "x", Integer.toString(rect.x)); @@ -445,6 +449,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void drawImage(Document doc, Rectangle rect) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, "x", Integer.toString(rect.x)); addAttribute(atts, "y", Integer.toString(rect.y)); @@ -534,6 +539,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler public void drawLine(Point start, Point end, int width, Color color, RuleStyle style) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, "x1", Integer.toString(start.x)); addAttribute(atts, "y1", Integer.toString(start.y)); @@ -552,6 +558,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[] dx, String text) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, "x", Integer.toString(x)); addAttribute(atts, "y", Integer.toString(y)); @@ -671,7 +678,16 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } } - // ---=== IFDocumentNavigationHandler ===--- + private void addID() throws SAXException { + String id = getContext().getID(); + if (!currentID.equals(id)) { + AttributesImpl atts = new AttributesImpl(); + addAttribute(atts, "name", id); + handler.startElement(EL_ID, atts); + handler.endElement(EL_ID); + currentID = id; + } + } private Map incompleteActions = new java.util.HashMap(); private List completeActions = new java.util.LinkedList(); |