diff options
Diffstat (limited to 'src')
30 files changed, 1647 insertions, 1117 deletions
diff --git a/src/java/org/apache/fop/render/afp/AFPGraphics2DAdapter.java b/src/java/org/apache/fop/render/afp/AFPGraphics2DAdapter.java index 1abe7c0d1..af8c7fedf 100644 --- a/src/java/org/apache/fop/render/afp/AFPGraphics2DAdapter.java +++ b/src/java/org/apache/fop/render/afp/AFPGraphics2DAdapter.java @@ -52,10 +52,12 @@ public class AFPGraphics2DAdapter extends AbstractGraphics2DAdapter { int resolution = (int)Math.round(context.getUserAgent().getTargetResolution()); BufferedImage bi = paintToBufferedImage(painter, wrappedContext, resolution, gray, false); - //TODO: uri - String uri = null; - java.util.Map foreignAttributes = null; - afp.drawBufferedImage(uri, bi, resolution, x, y, width, height, foreignAttributes); + + //TODO: AC - fix +// String uri = null; +// java.util.Map foreignAttributes = null; +// ImageInfo imageInfo = new ImageInfo(); +// afp.drawBufferedImage(uri, bi, resolution, x, y, width, height, foreignAttributes); } } diff --git a/src/java/org/apache/fop/render/afp/AFPRenderer.java b/src/java/org/apache/fop/render/afp/AFPRenderer.java index cb3d3a433..e50e77830 100644 --- a/src/java/org/apache/fop/render/afp/AFPRenderer.java +++ b/src/java/org/apache/fop/render/afp/AFPRenderer.java @@ -26,6 +26,7 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.RenderedImage; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -87,6 +88,7 @@ import org.apache.fop.render.afp.fonts.FopCharacterSet; import org.apache.fop.render.afp.fonts.OutlineFont; import org.apache.fop.render.afp.modca.AFPConstants; import org.apache.fop.render.afp.modca.AFPDataStream; +import org.apache.fop.render.afp.modca.AbstractNamedAFPObject; import org.apache.fop.render.afp.modca.ImageObject; import org.apache.fop.render.afp.modca.IncludeObject; import org.apache.fop.render.afp.modca.PageObject; @@ -969,7 +971,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { } else if (img instanceof ImageRendered) { ImageRendered imgRend = (ImageRendered)img; RenderedImage ri = imgRend.getRenderedImage(); - drawBufferedImage(uri, ri, getResolution(), + drawBufferedImage(info, ri, getResolution(), posInt.x + currentIPPosition, posInt.y + currentBPPosition, posInt.width, @@ -983,17 +985,26 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { int afpw = mpts2units(posInt.getWidth()); int afph = mpts2units(posInt.getHeight()); int afpres = getResolution(); - ImageObjectParameters params = new ImageObjectParameters( - uri, afpx, afpy, afpw, afph, afpres, afpres, - buf, - ccitt.getSize().getWidthPx(), - ccitt.getSize().getHeightPx(), - colorImages, bitsPerPixel - ); - params.setCompression(ccitt.getCompression()); - params.setResourceLevelFromForeignAttributes(foreignAttributes); -// params.setData(buf); - afpDataStream.createImageObject(params); + + // create image object parameters + ImageObjectInfo imageObjectInfo = new ImageObjectInfo(); + imageObjectInfo.setUri(uri); + imageObjectInfo.setMimeType(info.getMimeType()); + imageObjectInfo.setX(afpx); + imageObjectInfo.setY(afpy); + imageObjectInfo.setWidth(afpw); + imageObjectInfo.setHeight(afph); + imageObjectInfo.setWidthRes(afpres); + imageObjectInfo.setHeightRes(afpres); + imageObjectInfo.setData(buf); + imageObjectInfo.setDataHeight(ccitt.getSize().getHeightPx()); + imageObjectInfo.setDataWidth(ccitt.getSize().getWidthPx()); + imageObjectInfo.setColor(colorImages); + imageObjectInfo.setBitsPerPixel(bitsPerPixel); + imageObjectInfo.setCompression(ccitt.getCompression()); + imageObjectInfo.setResourceInfoFromForeignAttributes(foreignAttributes); + + afpDataStream.createObject(imageObjectInfo); // ImageObject io = afpDataStream.getImageObject(afpx, afpy, afpw, afph, @@ -1193,7 +1204,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { * @param h * the height of the viewport (in mpt) */ - public void drawBufferedImage(String uri, RenderedImage image, int imageRes, int x, + public void drawBufferedImage(ImageInfo info, RenderedImage image, int imageRes, int x, int y, int w, int h, Map foreignAttributes) { // int afpx = mpts2units(x); // int afpy = mpts2units(y); @@ -1213,19 +1224,25 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { // String uri = null;// = image.getProperty(name); // String uri = image.getProperty(name); // Generate image - ImageObjectParameters params = new ImageObjectParameters( - uri, - mpts2units(x), mpts2units(y), - mpts2units(w), mpts2units(h), - imageRes, imageRes, - baout.toByteArray(), - image.getWidth(), - image.getHeight(), - colorImages, - bitsPerPixel); - - params.setResourceLevelFromForeignAttributes(foreignAttributes); - IncludeObject io = afpDataStream.createImageObject(params); + + // create image object parameters + ImageObjectInfo imageObjectInfo = new ImageObjectInfo(); + imageObjectInfo.setUri(info.getOriginalURI()); + imageObjectInfo.setMimeType(info.getMimeType()); + imageObjectInfo.setX(mpts2units(x)); + imageObjectInfo.setY(mpts2units(y)); + imageObjectInfo.setWidth(mpts2units(w)); + imageObjectInfo.setHeight(mpts2units(h)); + imageObjectInfo.setWidthRes(imageRes); + imageObjectInfo.setHeightRes(imageRes); + imageObjectInfo.setData(baout.toByteArray()); + imageObjectInfo.setDataHeight(image.getHeight()); + imageObjectInfo.setDataWidth(image.getWidth()); + imageObjectInfo.setColor(colorImages); + imageObjectInfo.setBitsPerPixel(bitsPerPixel); + imageObjectInfo.setResourceInfoFromForeignAttributes(foreignAttributes); + + AbstractNamedAFPObject obj = afpDataStream.createObject(imageObjectInfo); // ImageObject io = afpDataStream.getImageObject(afpx, afpy, afpw, // afph, afpres, afpres); // io.setImageParameters(imageResolution, imageResolution, @@ -1586,9 +1603,10 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { if (content != null) { afpDataStream.createNoOperation(content); } - } else if (AFPElementMapping.RESOURCE.equals(element)) { - log.info("resource: " + attachment); } +// else if (AFPElementMapping.RESOURCE.equals(element)) { +// log.info("resource: " + attachment); +// } } } } diff --git a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java index cde464ba5..f83aaf9f6 100644 --- a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java +++ b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java @@ -19,6 +19,7 @@ package org.apache.fop.render.afp; +import java.io.File; import java.util.List; import org.apache.avalon.framework.configuration.Configuration; @@ -224,7 +225,8 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { LogUtil.handleException(log, e, userAgent.getFactory().validateUserConfigStrictly()); } - + + // image information Configuration imagesCfg = cfg.getChild("images"); if (!"color".equalsIgnoreCase(imagesCfg.getAttribute("mode", "b+w"))) { afpRenderer.setBitsPerPixel(imagesCfg.getAttributeAsInteger("bits-per-pixel", 8)); @@ -232,35 +234,36 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { afpRenderer.setColorImages(true); } + // renderer resolution Configuration rendererResolutionCfg = cfg.getChild("renderer-resolution", false); if (rendererResolutionCfg != null) { afpRenderer.setResolution(rendererResolutionCfg.getValueAsInteger(240)); } + // a default external resource group file setting + Configuration resourceGroupFileCfg = cfg.getChild("resource-group-file", false); + if (resourceGroupFileCfg != null) { + String resourceGroupDest = null; + try { + resourceGroupDest = resourceGroupFileCfg.getValue(); + } catch (ConfigurationException e) { + LogUtil.handleException(log, e, + userAgent.getFactory().validateUserConfigStrictly()); + } + File resourceGroupFile = new File(resourceGroupDest); + if (resourceGroupFile.canWrite()) { + afpRenderer.getAFPDataStream().setDefaultResourceGroupFile(resourceGroupFile); + } else { + log.warn("Unable to write to default external resource group file '" + + resourceGroupDest); + } + } + + // goca enabled Configuration gocaSupportCfg = cfg.getChild("goca-enabled", false); if (gocaSupportCfg != null) { afpRenderer.setGOCAEnabled(true); } - -// Configuration resourceGroupsCfg = cfg.getChild("resource-groups", false); -// if (resourceGroupsCfg != null) { -// resourceGroupsCfg.getValue("print-file-level"); -// } -// if (externalCfg != null) { -// Configuration[] resourceGroups = externalCfg.getChildren("resource-groups"); -// for (int i = 0; i < resourceGroups.length; i++) { -// String resourceresourceGroups[i].getAttribute("url", null); -// Configuration resourceGroup = externalCfg.getChild("resource-group", false); -// } -// } -// Configuration externalResourceGroupCfg = cfg.getChild("external-resource-group", false); -// if (externalResourceGroupCfg != null) { -//// afpRenderer.setExternalResources(true); -// String resourceLibraryUrl = externalResourceGroupCfg.getAttribute("url", null); -// if (resourceLibraryUrl != null) { -// afpRenderer.setExternalResourceLibraryUrl(resourceLibraryUrl); -// } -// } } } } diff --git a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java index 1d469ea94..0d3b864b0 100644 --- a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java +++ b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java @@ -159,20 +159,26 @@ public class AFPSVGHandler extends AbstractGenericSVGHandler { int width = (int)Math.round((afpInfo.getWidth() * res) / 72000f); int height = (int)Math.round((afpInfo.getHeight() * res) / 72000f); - DataObjectParameters params = new DataObjectParameters( - uri, x, y, width, height, res, res); - + // set the data object parameters + DataObjectInfo info = new DataObjectInfo(); + info.setUri(uri); + info.setX(x); + info.setY(y); + info.setWidth(width); + info.setHeight(height); + info.setWidthRes(res); + info.setHeightRes(res); Map/*<QName, String>*/ foreignAttributes = (Map/*<QName, String>*/)context.getProperty( RendererContextConstants.FOREIGN_ATTRIBUTES); - if (foreignAttributes != null) { - params.setResourceLevelFromForeignAttributes(foreignAttributes); + info.setResourceInfoFromForeignAttributes(foreignAttributes); } - IncludeObject includeObj = afpInfo.getAFPDataStream().createGraphicsObject(params); - GraphicsObject graphicsObj = (GraphicsObject)includeObj.getReferencedObject(); - graphics.setGraphicsObject(graphicsObj); + //TODO: AC - fix +// IncludeObject includeObj = afpInfo.getAFPDataStream().createObject(info); +// GraphicsObject graphicsObj = (GraphicsObject)includeObj.getReferencedObject(); +// graphics.setGraphicsObject(graphicsObj); try { root.paint(graphics); diff --git a/src/java/org/apache/fop/render/afp/DataObjectParameters.java b/src/java/org/apache/fop/render/afp/DataObjectInfo.java index 2ec56a4e3..d363dd5ea 100644 --- a/src/java/org/apache/fop/render/afp/DataObjectParameters.java +++ b/src/java/org/apache/fop/render/afp/DataObjectInfo.java @@ -19,18 +19,21 @@ package org.apache.fop.render.afp; +import java.io.File; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.render.afp.extensions.AFPElementMapping; +import org.apache.fop.render.afp.modca.ObjectTypeRegistry; +import org.apache.fop.render.afp.modca.ObjectTypeRegistry.ObjectType; import org.apache.fop.util.QName; /** * A list of parameters associated with an AFP data objects */ -public class DataObjectParameters { - private static final ResourceLevel DEFAULT_RESOURCE_LEVEL = new ResourceLevel(); +public class DataObjectInfo { + private static final ResourceInfo DEFAULT_RESOURCE_INFO = new ResourceInfo(); private String uri; private int x; @@ -39,31 +42,74 @@ public class DataObjectParameters { private int height; private int widthRes; private int heightRes; - private ResourceLevel resourceLevel = DEFAULT_RESOURCE_LEVEL; + + // object type entry + private ObjectType objectTypeEntry; + private ResourceInfo resourceInfo = DEFAULT_RESOURCE_INFO; /** - * Main constructor - * + * Sets the data object uri * @param uri the data object uri - * @param x the data object x coordinate - * @param y the data object y coordinate - * @param width the data object width - * @param height the data object height - * @param widthRes the data object width resolution - * @param heightRes the data object height resolution - */ - public DataObjectParameters(String uri, int x, int y, int width, int height, - int widthRes, int heightRes) { + */ + protected void setUri(String uri) { this.uri = uri; + } + + /** + * Sets the x position of the data object + * @param x the x position of the data object + */ + protected void setX(int x) { this.x = x; + } + + /** + * Sets the y position of the data object + * @param y the y position of the data object + */ + protected void setY(int y) { this.y = y; + } + + /** + * Sets the data object width + * @param width the width of the data object + */ + protected void setWidth(int width) { this.width = width; + } + + /** + * Sets the data object height + * @param height the height of the data object + */ + protected void setHeight(int height) { this.height = height; + } + + /** + * Sets the width resolution + * @param widthRes the width resolution + */ + protected void setWidthRes(int widthRes) { this.widthRes = widthRes; + } + + /** + * Sets the height resolution + * @param heightRes the height resolution + */ + protected void setHeightRes(int heightRes) { this.heightRes = heightRes; } /** + * Default constructor + */ + public DataObjectInfo() { + } + + /** * @return the uri of this data object */ public String getUri() { @@ -113,40 +159,65 @@ public class DataObjectParameters { } /** + * Sets the object type entry + * @param entry the object type entry + */ + public void setObjectType(ObjectTypeRegistry.ObjectType entry) { + this.objectTypeEntry = entry; + } + + /** + * @return the object type entry + */ + protected ObjectType getObjectTypeEntry() { + return objectTypeEntry; + } + + /** * @return returns the resource level at which this data object should reside */ - public ResourceLevel getResourceLevel() { - return resourceLevel; + public ResourceInfo getResourceInfo() { + return resourceInfo; } /** * Sets the resource level at which this object should reside - * @param resourceLevel the resource level at which this data object should reside + * @param resourceInfo the resource level at which this data object should reside */ - public void setResourceLevel(ResourceLevel resourceLevel) { - this.resourceLevel = resourceLevel; + public void setResourceInfo(ResourceInfo resourceInfo) { + this.resourceInfo = resourceInfo; } - + + + private static final String RESOURCE_NAME = "afp:resource-name"; + private static final String RESOURCE_LEVEL = "afp:resource-level"; + private static final String RESOURCE_GROUP_FILE = "afp:resource-group-file"; + /** - * Sets the resource level using the given foreign attributes + * Sets the resource group settings using the given foreign attributes * @param foreignAttributes a mapping of element attributes names to values */ - public void setResourceLevelFromForeignAttributes(Map/*<QName, String>*/ foreignAttributes) { + public void setResourceInfoFromForeignAttributes(Map/*<QName, String>*/ foreignAttributes) { if (foreignAttributes != null) { - QName resourceLevelKey = new QName( - AFPElementMapping.NAMESPACE, - "afp:resource-level"); + this.resourceInfo = new ResourceInfo(); + QName resourceNameKey = new QName(AFPElementMapping.NAMESPACE, RESOURCE_NAME); + String resourceName = (String)foreignAttributes.get(resourceNameKey); + if (resourceName != null) { + resourceInfo.setName(resourceName); + } + QName resourceLevelKey = new QName(AFPElementMapping.NAMESPACE, RESOURCE_LEVEL); if (foreignAttributes.containsKey(resourceLevelKey)) { String level = (String)foreignAttributes.get(resourceLevelKey); - this.resourceLevel = new ResourceLevel(); - if (resourceLevel.setLevel(level)) { - if (resourceLevel.isExternal()) { - QName resourceDestKey = new QName( - AFPElementMapping.NAMESPACE, - "afp:resource-dest"); + if (resourceInfo.setLevel(level)) { + if (resourceInfo.isExternal()) { + QName resourceGroupFileKey = new QName(AFPElementMapping.NAMESPACE, + RESOURCE_GROUP_FILE); String resourceExternalDest - = (String)foreignAttributes.get(resourceDestKey); - resourceLevel.setExternalDest(resourceExternalDest); + = (String)foreignAttributes.get(resourceGroupFileKey); + File resourceExternalGroupFile = new File(resourceExternalDest); + if (resourceExternalGroupFile.canWrite()) { + resourceInfo.setExternalResourceGroupFile(resourceExternalGroupFile); + } } } else { Log log = LogFactory.getLog("org.apache.fop.afp"); @@ -168,6 +239,7 @@ public class DataObjectParameters { + ", height=" + height + ", widthRes=" + widthRes + ", heightRes=" + heightRes - + (resourceLevel != null ? ", resourceLevel=" + resourceLevel : ""); + + (resourceInfo != null ? ", resourceInfo=" + resourceInfo : "") + + (objectTypeEntry != null ? ", objectTypeEntry=" + objectTypeEntry : ""); } } diff --git a/src/java/org/apache/fop/render/afp/ImageObjectParameters.java b/src/java/org/apache/fop/render/afp/ImageObjectInfo.java index 8b430f042..16f9d1103 100644 --- a/src/java/org/apache/fop/render/afp/ImageObjectParameters.java +++ b/src/java/org/apache/fop/render/afp/ImageObjectInfo.java @@ -22,38 +22,55 @@ package org.apache.fop.render.afp; /** * A list of parameters associated with an image */ -public class ImageObjectParameters extends DataObjectParameters { +public class ImageObjectInfo extends DataObjectInfo { private int bitsPerPixel; private boolean color; private int compression = -1; - private byte[] imageData; - private int imageDataWidth; - private int imageDataHeight; - + private byte[] data; + private int dataWidth; + private int dataHeight; + private String mimeType; + /** - * Main constructor - * - * @param uri the image uri - * @param x the image x coordinate - * @param y the image y coordinate - * @param width the image width - * @param height the image height - * @param widthRes the image width resolution - * @param heightRes the image height resolution - */ - public ImageObjectParameters(String uri, int x, int y, int width, int height, - int widthRes, int heightRes, byte[] imageData, - int imageDataWidth, int imageDataHeight, boolean color, int bitsPerPixel) { - super(uri, x, y, width, height, widthRes, heightRes); - this.imageData = imageData; - this.imageDataWidth = imageDataWidth; - this.imageDataHeight = imageDataHeight; - this.color = color; + * Default constructor + */ + public ImageObjectInfo() { + } + + /** + * Sets the number of bits per pixel + * @param bitsPerPixel the number of bits per pixel + */ + public void setBitsPerPixel(int bitsPerPixel) { this.bitsPerPixel = bitsPerPixel; } /** - * @return the numner of bits used per pixel + * Sets if this image is color + * @param color true if this is a color image + */ + public void setColor(boolean color) { + this.color = color; + } + + /** + * Sets the image data + * @param data the image data + */ + public void setData(byte[] data) { + this.data = data; + } + + /** + * Sets the image mime type + * @param mimeType the image mime type + */ + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + /** + * @return the number of bits used per pixel */ public int getBitsPerPixel() { return bitsPerPixel; @@ -70,7 +87,7 @@ public class ImageObjectParameters extends DataObjectParameters { * @return the image data */ public byte[] getData() { - return imageData; + return data; } /** @@ -98,40 +115,48 @@ public class ImageObjectParameters extends DataObjectParameters { /** * @return the image data width */ - public int getImageDataWidth() { - return imageDataWidth; + public int getDataWidth() { + return dataWidth; } /** * Sets the image data width * @param imageDataWidth the image data width */ - protected void setImageDataWidth(int imageDataWidth) { - this.imageDataWidth = imageDataWidth; + public void setDataWidth(int imageDataWidth) { + this.dataWidth = imageDataWidth; } /** * @return the image data height */ - public int getImageDataHeight() { - return imageDataHeight; + public int getDataHeight() { + return dataHeight; } /** * Sets the image data height * @param imageDataHeight the image data height */ - protected void setImageDataHeight(int imageDataHeight) { - this.imageDataHeight = imageDataHeight; + public void setDataHeight(int imageDataHeight) { + this.dataHeight = imageDataHeight; } - + + /** + * @return the mime type of this image + */ + public String getMimeType() { + return mimeType; + } + /** * {@inheritDoc} */ public String toString() { return super.toString() - + ", imageDataWidth=" + imageDataWidth - + ", imageDataHeight=" + imageDataHeight + + ", mimeType=" + mimeType + + ", dataWidth=" + dataWidth + + ", dataHeight=" + dataHeight + ", color=" + color + ", bitPerPixel=" + bitsPerPixel; } diff --git a/src/java/org/apache/fop/render/afp/ResourceInfo.java b/src/java/org/apache/fop/render/afp/ResourceInfo.java new file mode 100644 index 000000000..5c39d48b0 --- /dev/null +++ b/src/java/org/apache/fop/render/afp/ResourceInfo.java @@ -0,0 +1,164 @@ +/* + * 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.render.afp; + +import java.io.File; + +/** + * The level at which a resource is to reside in the AFP output + */ +public class ResourceInfo { + private static final String LEVEL_EXTERNAL = "external"; + + private static final String LEVEL_PRINT_FILE = "print-file"; + + private static final String LEVEL_DOCUMENT = "document"; + + private static final String LEVEL_PAGE_GROUP = "page-group"; + + private static final String LEVEL_PAGE = "page"; + + /** + * the reference name of this resource + */ + private String name = null; + + /** + * where the resource will reside in the AFP output + */ + private String level = LEVEL_PAGE; // default is page level + + /** + * the external resource group file + */ + private File externalResourceGroupFile = null; + + /** + * @return true if this is a page level resource group + */ + public boolean isPage() { + return level.equals(LEVEL_PAGE); + } + + /** + * @return true if this is a page group level resource group + */ + public boolean isPageGroup() { + return level.equals(LEVEL_PAGE_GROUP); + } + + /** + * @return true if this is a document level resource group + */ + public boolean isDocument() { + return level.equals(LEVEL_DOCUMENT); + } + + /** + * @return true if this is an external level resource group + */ + public boolean isExternal() { + return level.equals(LEVEL_EXTERNAL); + } + + /** + * @return true if this is a print-file level resource group + */ + public boolean isPrintFile() { + return level.equals(LEVEL_PRINT_FILE); + } + + private boolean isValid(String lvl) { + return lvl.equals(LEVEL_EXTERNAL) + || lvl.equals(LEVEL_PRINT_FILE) + || lvl.equals(LEVEL_DOCUMENT) + || lvl.equals(LEVEL_PAGE_GROUP) + || lvl.equals(LEVEL_PAGE); + } + + /** + * Sets the resource placement level within the AFP output + * @param lvl the resource level (page, page-group, document, print-file or external) + * @return true if the resource level was successfully set + */ + public boolean setLevel(String lvl) { + if (isValid(lvl)) { + this.level = lvl; + return true; + } + return false; + } + + /** + * @return the external resource group file of this resource + */ + public File getExternalResourceGroupFile() { + return this.externalResourceGroupFile; + } + + /** + * @return the destination file path of the external resource group file + */ + public String getExternalResourceGroupDest() { + if (externalResourceGroupFile != null) { + return externalResourceGroupFile.getAbsolutePath(); + } + return null; + } + + /** + * @return true if this resource has a defined external resource group file destination + */ + public boolean hasExternalResourceGroupFile() { + return getExternalResourceGroupFile() != null; + } + + /** + * Sets the external destination of the resource + * @param file the external resource group file + */ + public void setExternalResourceGroupFile(File file) { + this.externalResourceGroupFile = file; + } + + /** + * Sets the resource reference name + * @param resourceName the resource reference name + */ + public void setName(String resourceName) { + this.name = resourceName; + } + + /** + * @return the resource reference name + */ + public String getName() { + return this.name; + } + + /** + * {@inheritDoc} + */ + public String toString() { + return "ResourceInfo(name=" + name + ", level=" + level + + (isExternal() ? ", externalResourceGroupFile=" + externalResourceGroupFile : "") + + ")"; + } +}
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/ResourceLevel.java b/src/java/org/apache/fop/render/afp/ResourceLevel.java deleted file mode 100644 index f397edb04..000000000 --- a/src/java/org/apache/fop/render/afp/ResourceLevel.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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.render.afp; - -/** - * The level at which a resource is to reside in the AFP output - */ -public class ResourceLevel { - private static final String EXTERNAL = "external"; - - private static final String PRINT_FILE = "print-file"; - - private static final String DOCUMENT = "document"; - - private static final String PAGE_GROUP = "page-group"; - - private static final String PAGE = "page"; - - /** - * where the resource will reside in the AFP output - */ - private String level = PAGE; // default is page level - - /** - * the destination location of the resource - */ - private String dest = null; - - /** - * @return true if this is a page level resource group - */ - public boolean isPage() { - return level.equals(PAGE); - } - - /** - * @return true if this is a page group level resource group - */ - public boolean isPageGroup() { - return level.equals(PAGE_GROUP); - } - - /** - * @return true if this is a document level resource group - */ - public boolean isDocument() { - return level.equals(DOCUMENT); - } - - /** - * @return true if this is an external level resource group - */ - public boolean isExternal() { - return level.equals(EXTERNAL); - } - - /** - * @return true if this is a print-file level resource group - */ - public boolean isPrintFile() { - return level.equals(PRINT_FILE); - } - - private boolean isValid(String lvl) { - return lvl.equals(EXTERNAL) - || lvl.equals(PRINT_FILE) - || lvl.equals(DOCUMENT) - || lvl.equals(PAGE_GROUP) - || lvl.equals(PAGE); - } - - /** - * Sets the resource placement level within the AFP output - * @param level the resource level (page, page-group, document, print-file or external) - * @return true if the resource level was successfully set - */ - public boolean setLevel(String level) { - if (isValid(level)) { - this.level = level; - return true; - } - return false; - } - - /** - * @return the external destination of the resource - */ - public String getExternalDest() { - return dest; - } - - /** - * Sets the external destination of the resource - * @param dest the external destination of the resource - */ - public void setExternalDest(String dest) { - this.dest = dest; - } - - /** - * {@inheritDoc} - */ - public String toString() { - return "level=" + level + (isExternal() ? ", dest=" + dest : ""); - } -}
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/extensions/AFPElement.java b/src/java/org/apache/fop/render/afp/extensions/AFPElement.java index 659c79a8e..7d32c90d7 100755 --- a/src/java/org/apache/fop/render/afp/extensions/AFPElement.java +++ b/src/java/org/apache/fop/render/afp/extensions/AFPElement.java @@ -55,8 +55,7 @@ public class AFPElement extends AbstractAFPExtensionObject { } } - protected ExtensionAttachment instantiateExtensionAttachment() { - return new AFPPageSetup(getName()); - } - + protected ExtensionAttachment instantiateExtensionAttachment() { + return new AFPPageSetup(getName()); + } } diff --git a/src/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java b/src/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java index ad80399e3..44ba63aca 100755 --- a/src/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java +++ b/src/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java @@ -48,8 +48,8 @@ public class AFPElementMapping extends ElementMapping { /** include page segment element */ public static final String INCLUDE_PAGE_SEGMENT = "include-page-segment"; - /** include resource element (external) */ - public static final String RESOURCE = "resource"; +// /** include resource element (external) */ +// public static final String RESOURCE = "resource"; /** NOP */ public static final String NO_OPERATION = "no-operation"; @@ -70,7 +70,7 @@ public class AFPElementMapping extends ElementMapping { } /** - * Private static synchronized method to set up the element and atribute + * Private static synchronized method to set up the element and attribute * HashMaps, this defines what elements and attributes are extracted. */ protected void initialize() { @@ -79,8 +79,8 @@ public class AFPElementMapping extends ElementMapping { super.foObjs = new java.util.HashMap(); foObjs.put(PAGE, new AFPPageSetupMaker()); // foObjs.put( -// PAGE_GROUP, -// new AFPPageGroupMaker() +// PAGE_GROUP, +// new AFPPageGroupMaker() // ); foObjs.put( TAG_LOGICAL_ELEMENT, diff --git a/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java b/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java index bf534e3c7..e6790e91c 100644 --- a/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java +++ b/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java @@ -20,6 +20,7 @@ package org.apache.fop.render.afp.modca; import java.awt.Color; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; @@ -29,9 +30,8 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.render.afp.AFPFontAttributes; -import org.apache.fop.render.afp.DataObjectParameters; -import org.apache.fop.render.afp.ImageObjectParameters; -import org.apache.fop.render.afp.ResourceLevel; +import org.apache.fop.render.afp.DataObjectInfo; +import org.apache.fop.render.afp.ResourceInfo; import org.apache.fop.render.afp.fonts.AFPFont; import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; import org.apache.fop.render.afp.tools.StringUtils; @@ -146,9 +146,9 @@ public class AFPDataStream extends AbstractResourceGroupContainer { private OutputStream outputStream = null; /** - * A mapping of external resource destinations to resource groups + * The external resource group manager */ - private Map/*<String,ResourceGroup>*/ externalResourceGroups = null; + private ExternalResourceGroupManager externalResourceGroupManager = null; /** * Default constructor for the AFPDataStream. @@ -226,9 +226,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { } // Write out any external resource groups - if (externalResourceGroups != null) { - writeExternalResources(); - } + getExternalResourceGroupManager().writeExternalResources(); // Write out any print-file level resources if (hasResources()) { @@ -405,7 +403,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { * the point size of the font */ public void createFont(int fontReference, AFPFont font, int size) { - currentPage.createFont(fontReference, font, size); + getCurrentPage().createFont(fontReference, font, size); } /** @@ -429,51 +427,37 @@ public class AFPDataStream extends AbstractResourceGroupContainer { */ public void createText(int fontReference, int x, int y, Color col, int vsci, int ica, byte[] data) { - currentPage.createText(fontReference, x + xOffset, y + yOffset, rotation, + getCurrentPage().createText(fontReference, x + xOffset, y + yOffset, rotation, col, vsci, ica, data); } - - /** - * Returns true if the resource exists within this resource group, - * false otherwise. - * - * @param uri the uri of the resource - * @return true if the resource exists within this resource group - */ - public boolean resourceExists(String uri) { - return getResourceGroup().resourceExists(uri); - } - - /** - * Returns an IncludeObject referencing an image in the datastream. - * - * @param params - * the unique uri of the image - * @return - * a new include object referencing an image object - */ - public IncludeObject createImageObject(ImageObjectParameters params) { - ResourceLevel resourceLevel = params.getResourceLevel(); - IncludeObject includeObj = getResourceGroup(resourceLevel).addObject(params); - currentPage.addObject(includeObj); - return includeObj; - } /** - * Returns an IncludeObject referencing a graphic in the datastream. - * - * @param params - * the data object parameters + * Creates a data object in the datastream. The data object resides according + * to its type, info and MO:DCA-L (resource) support. + * + * @param dataObjectInfo + * the data object info * @return - * a new include object referencing the graphics object - */ - public IncludeObject createGraphicsObject(DataObjectParameters params) { - ResourceLevel resourceLevel = params.getResourceLevel(); - IncludeObject includeObj = getResourceGroup(resourceLevel).addObject(params); - currentPage.addObject(includeObj); - return includeObj; + * a data object + */ + public AbstractNamedAFPObject createObject(DataObjectInfo dataObjectInfo) { + ObjectTypeRegistry registry = ObjectTypeRegistry.getInstance(); + ObjectTypeRegistry.ObjectType objectType = registry.getObjectType(dataObjectInfo); + if (objectType != null) { + dataObjectInfo.setObjectType(objectType); + } + AbstractNamedAFPObject dataObj; + // can this data object use the include object (IOB) mechanism? + if (objectType.canBeIncluded()) { + ResourceInfo resourceInfo = dataObjectInfo.getResourceInfo(); + ResourceGroup resourceGroup = getResourceGroup(resourceInfo); + dataObj = resourceGroup.createObject(dataObjectInfo); + } else { + dataObj = getCurrentPage().createObject(dataObjectInfo); + } + return dataObj; } - + /** * Sets the object view port taking into account rotation. * @@ -502,7 +486,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { int heightRes; switch (this.rotation) { case 90: - xOrigin = currentPage.getWidth() - y - yOffset; + xOrigin = getCurrentPage().getWidth() - y - yOffset; yOrigin = x + xOffset; width = h; height = w; @@ -510,8 +494,8 @@ public class AFPDataStream extends AbstractResourceGroupContainer { heightRes = wr; break; case 180: - xOrigin = currentPage.getWidth() - x - xOffset; - yOrigin = currentPage.getHeight() - y - yOffset; + xOrigin = getCurrentPage().getWidth() - x - xOffset; + yOrigin = getCurrentPage().getHeight() - y - yOffset; width = w; height = h; widthRes = wr; @@ -519,7 +503,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { break; case 270: xOrigin = y + yOffset; - yOrigin = currentPage.getHeight() - x - xOffset; + yOrigin = getCurrentPage().getHeight() - x - xOffset; width = h; height = w; widthRes = hr; @@ -555,7 +539,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { */ public void createLine(int x1, int y1, int x2, int y2, int thickness, Color col) { - currentPage.createLine(x1 + xOffset, y1 + yOffset, x2 + xOffset, y2 + getCurrentPage().createLine(x1 + xOffset, y1 + yOffset, x2 + xOffset, y2 + yOffset, thickness, rotation, col); } @@ -581,7 +565,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { */ public void createShading(int x, int y, int w, int h, int red, int green, int blue) { - currentPage.createShading(x + xOffset, y + xOffset, w, h, red, green, + getCurrentPage().createShading(x + xOffset, y + xOffset, w, h, red, green, blue); } @@ -622,23 +606,23 @@ public class AFPDataStream extends AbstractResourceGroupContainer { int yOrigin; switch (rotation) { case 90: - xOrigin = currentPage.getWidth() - y - yOffset; + xOrigin = getCurrentPage().getWidth() - y - yOffset; yOrigin = x + xOffset; break; case 180: - xOrigin = currentPage.getWidth() - x - xOffset; - yOrigin = currentPage.getHeight() - y - yOffset; + xOrigin = getCurrentPage().getWidth() - x - xOffset; + yOrigin = getCurrentPage().getHeight() - y - yOffset; break; case 270: xOrigin = y + yOffset; - yOrigin = currentPage.getHeight() - x - xOffset; + yOrigin = getCurrentPage().getHeight() - x - xOffset; break; default: xOrigin = x + xOffset; yOrigin = y + yOffset; break; } - currentPage.createIncludePageSegment(name, xOrigin, yOrigin); + getCurrentPage().createIncludePageSegment(name, xOrigin, yOrigin); } /** @@ -681,7 +665,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { if (currentPageGroup != null) { currentPageGroup.createTagLogicalElement(name, value); } else { - currentPage.createTagLogicalElement(name, value); + getCurrentPage().createTagLogicalElement(name, value); } } @@ -692,7 +676,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { * byte data */ public void createNoOperation(String content) { - currentPage.createNoOperation(content); + getCurrentPage().createNoOperation(content); } private PageGroup getCurrentPageGroup() { @@ -719,7 +703,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { public void endPageGroup() { if (currentPageGroup != null) { currentPageGroup.endPageGroup(); - document.addPageGroup(currentPageGroup); + getDocument().addPageGroup(currentPageGroup); currentPageGroup = null; } } @@ -758,70 +742,143 @@ public class AFPDataStream extends AbstractResourceGroupContainer { } } + /** - * Writes out external AFP resources - */ - private void writeExternalResources() { - // write any external resources - Iterator it = getExternalResourceGroups().keySet().iterator(); - while (it.hasNext()) { - String externalDest = (String)it.next(); - ResourceGroup resourceGroup - = (ResourceGroup)getExternalResourceGroups().get(externalDest); - OutputStream os = null; - try { - log.debug("Writing external AFP resource file " + externalDest); - os = new java.io.FileOutputStream(externalDest); - resourceGroup.writeDataStream(os); - } catch (FileNotFoundException e) { - log.error("Failed to open external AFP resource file " - + externalDest); - } catch (IOException e) { - log.error("An error occurred when attempting to write external AFP resource file " - + externalDest); - } finally { - if (os != null) { - try { - os.close(); - } catch (IOException e) { - log.error("Failed to close outputstream for external AFP resource file " - + externalDest); - } - } - } - } - } - - /** - * Returns the resource group for a given resource level - * @param resourceLevel a resource level - * @return a resource group container for the given level + * Returns the resource group for a given resource into + * @param resourceInfo resource info + * @return a resource group container for the given resource info */ - private ResourceGroup getResourceGroup(ResourceLevel resourceLevel) { + private ResourceGroup getResourceGroup(ResourceInfo resourceInfo) { ResourceGroup resourceGroup = null; - if (resourceLevel.isPrintFile()) { - resourceGroup = this.getResourceGroup(); - } else if (resourceLevel.isDocument()) { - resourceGroup = document.getResourceGroup(); - } else if (resourceLevel.isPageGroup()) { + if (resourceInfo.isPrintFile()) { + resourceGroup = getResourceGroup(); + } else if (resourceInfo.isDocument()) { + resourceGroup = getDocument().getResourceGroup(); + } else if (resourceInfo.isPageGroup()) { resourceGroup = getCurrentPageGroup().getResourceGroup(); - } else if (resourceLevel.isPage()) { + } else if (resourceInfo.isPage()) { resourceGroup = getCurrentPage().getResourceGroup(); - } else if (resourceLevel.isExternal()) { - String externalDest = resourceLevel.getExternalDest(); - resourceGroup = (ResourceGroup)getExternalResourceGroups().get(externalDest); - if (resourceGroup == null) { - resourceGroup = new ResourceGroup(); - externalResourceGroups.put(externalDest, resourceGroup); - } + } else if (resourceInfo.isExternal()) { + resourceGroup = getExternalResourceGroupManager().getExternalResourceGroup( + resourceInfo); } return resourceGroup; } + + /** + * Sets the default resource group file + * @param resourceGroupFile the default resource group file + */ + public void setDefaultResourceGroupFile(File resourceGroupFile) { + getExternalResourceGroupManager().setDefaultResourceGroupFile(resourceGroupFile); + } + + /** + * @return the resource group manager + */ + protected ExternalResourceGroupManager getExternalResourceGroupManager() { + if (externalResourceGroupManager == null) { + this.externalResourceGroupManager = new ExternalResourceGroupManager(this); + } + return this.externalResourceGroupManager; + } + - private Map/*<String,ResourceGroup>*/ getExternalResourceGroups() { - if (externalResourceGroups == null) { - externalResourceGroups = new java.util.HashMap(); + /** + * Manages the use of resource groups (external and internal) + */ + private final class ExternalResourceGroupManager { + /** + * A mapping of external resource destinations to resource groups + */ + private Map/*<File, ResourceGroup>*/ externalResourceGroups = null; + + /** sets the default resource group file */ + private File defaultResourceGroupFile; + + /** the container of this manager */ + private AbstractResourceGroupContainer container; + + /** + * Main constructor + * @param container the container of this manager + */ + private ExternalResourceGroupManager(AbstractResourceGroupContainer container) { + this.container = container; } - return externalResourceGroups; + + /** + * Sets the default resource group file + * @param resourceGroupFile the default resource group file + */ + private void setDefaultResourceGroupFile(File resourceGroupFile) { + this.defaultResourceGroupFile = resourceGroupFile; + } + + /** + * Writes out external AFP resources + */ + private void writeExternalResources() { + // write any external resources + Iterator it = getExternalResourceGroups().keySet().iterator(); + while (it.hasNext()) { + String externalDest = (String)it.next(); + ResourceGroup resourceGroup + = (ResourceGroup)getExternalResourceGroups().get(externalDest); + OutputStream os = null; + try { + log.debug("Writing external AFP resource file " + externalDest); + os = new java.io.FileOutputStream(externalDest); + resourceGroup.writeDataStream(os); + } catch (FileNotFoundException e) { + log.error("Failed to open external AFP resource file " + + externalDest); + } catch (IOException e) { + log.error( + "An error occurred when attempting to write external AFP resource file " + + externalDest); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + log.error("Failed to close outputstream for external AFP resource file " + + externalDest); + } + } + } + } + } + + private ResourceGroup getExternalResourceGroup(ResourceInfo resourceInfo) { + ResourceGroup resourceGroup; + // this resource info does not have a an external resource group file definition + if (!resourceInfo.hasExternalResourceGroupFile()) { + if (defaultResourceGroupFile != null) { + // fallback to default resource group file + resourceInfo.setExternalResourceGroupFile(defaultResourceGroupFile); + resourceGroup = getExternalResourceGroup(resourceInfo); + } else { + // use print-file level resource group in the absence + // of an external resource group file definition + resourceGroup = container.getResourceGroup(); + } + } else { + File resourceGroupFile = resourceInfo.getExternalResourceGroupFile(); + resourceGroup = (ResourceGroup)getExternalResourceGroups().get(resourceGroupFile); + if (resourceGroup == null) { + resourceGroup = new ResourceGroup(container); + externalResourceGroups.put(resourceGroupFile, resourceGroup); + } + } + return resourceGroup; + } + + private Map/*<File, ResourceGroup>*/ getExternalResourceGroups() { + if (externalResourceGroups == null) { + externalResourceGroups = new java.util.HashMap/*<File, ResourceGroup>*/(); + } + return externalResourceGroups; + } } } diff --git a/src/java/org/apache/fop/render/afp/modca/AbstractAFPObject.java b/src/java/org/apache/fop/render/afp/modca/AbstractAFPObject.java index 106454ad6..60cfeb623 100644 --- a/src/java/org/apache/fop/render/afp/modca/AbstractAFPObject.java +++ b/src/java/org/apache/fop/render/afp/modca/AbstractAFPObject.java @@ -44,7 +44,7 @@ public abstract class AbstractAFPObject { * DataStream objects must implement the writeDataStream() * method to write its data to the given OutputStream * @param os The outputsteam stream - * @throws java.io.IOException + * @throws java.io.IOException an I/O exception of some sort has occurred. */ public abstract void writeDataStream(OutputStream os) throws IOException; @@ -52,18 +52,16 @@ public abstract class AbstractAFPObject { * Help method to write a set of AFPObjects to the AFP datastream. * @param objects a list of AFPObjects * @param os The stream to write to - * @throws java.io.IOException + * @throws java.io.IOException an I/O exception of some sort has occurred. */ protected void writeObjects(Collection/*<AbstractAFPObject>*/ objects, OutputStream os) throws IOException { if (objects != null) { for (Iterator it = objects.iterator(); it.hasNext();) { - Object obj1 = it.next(); - AbstractAFPObject obj = (AbstractAFPObject)obj1; + AbstractAFPObject obj = (AbstractAFPObject)it.next(); obj.writeDataStream(os); } } - } } diff --git a/src/java/org/apache/fop/render/afp/modca/AbstractNamedAFPObject.java b/src/java/org/apache/fop/render/afp/modca/AbstractNamedAFPObject.java index 09fa33f8f..8e6127b8a 100644 --- a/src/java/org/apache/fop/render/afp/modca/AbstractNamedAFPObject.java +++ b/src/java/org/apache/fop/render/afp/modca/AbstractNamedAFPObject.java @@ -81,5 +81,4 @@ public abstract class AbstractNamedAFPObject extends AbstractStructuredAFPObject public String getName() { return name; } - } diff --git a/src/java/org/apache/fop/render/afp/modca/AbstractPageObject.java b/src/java/org/apache/fop/render/afp/modca/AbstractPageObject.java index 984aa44c9..95dd303b2 100644 --- a/src/java/org/apache/fop/render/afp/modca/AbstractPageObject.java +++ b/src/java/org/apache/fop/render/afp/modca/AbstractPageObject.java @@ -432,10 +432,7 @@ public abstract class AbstractPageObject extends AbstractResourceGroupContainer protected void addObject(AbstractStructuredAFPObject obj) { if (obj instanceof IncludeObject) { IncludeObject includeObj = (IncludeObject)obj; - AbstractStructuredAFPObject refObj = includeObj.getReferencedObject(); - if (refObj instanceof ImageObject || refObj instanceof GraphicsObject) { - getActiveEnvironmentGroup().createResource(refObj); - } + getActiveEnvironmentGroup().createResource(includeObj); } endPresentationObject(); super.addObject(obj); diff --git a/src/java/org/apache/fop/render/afp/modca/AbstractResourceGroupContainer.java b/src/java/org/apache/fop/render/afp/modca/AbstractResourceGroupContainer.java index e8e25b6b8..896771cd7 100644 --- a/src/java/org/apache/fop/render/afp/modca/AbstractResourceGroupContainer.java +++ b/src/java/org/apache/fop/render/afp/modca/AbstractResourceGroupContainer.java @@ -23,8 +23,11 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Set; -import org.apache.fop.render.afp.DataObjectParameters; -import org.apache.fop.render.afp.ImageObjectParameters; +import org.apache.fop.render.afp.DataObjectInfo; +import org.apache.fop.render.afp.ImageObjectInfo; +import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; +import org.apache.fop.render.afp.tools.StringUtils; +import org.apache.xmlgraphics.image.codec.tiff.TIFFImage; /** * An abstract container of resource objects @@ -87,31 +90,165 @@ public abstract class AbstractResourceGroupContainer extends AbstractNamedAFPObj */ protected ResourceGroup getResourceGroup() { if (resourceGroup == null) { - resourceGroup = new ResourceGroup(); + resourceGroup = new ResourceGroup(this); } return resourceGroup; } + + /** + * Converts a byte array containing 24 bit RGB image data to a grayscale + * image. + * + * @param io + * the target image object + * @param raw + * the buffer containing the RGB image data + * @param width + * the width of the image in pixels + * @param height + * the height of the image in pixels + * @param bitsPerPixel + * the number of bits to use per pixel + * + * TODO: move this method somewhere appropriate in commons + */ + private static void convertToGrayScaleImage(ImageObject io, byte[] raw, int width, + int height, int bitsPerPixel) { + int pixelsPerByte = 8 / bitsPerPixel; + int bytewidth = (width / pixelsPerByte); + if ((width % pixelsPerByte) != 0) { + bytewidth++; + } + byte[] bw = new byte[height * bytewidth]; + byte ib; + for (int y = 0; y < height; y++) { + ib = 0; + int i = 3 * y * width; + for (int x = 0; x < width; x++, i += 3) { + + // see http://www.jguru.com/faq/view.jsp?EID=221919 + double greyVal = 0.212671d * ((int) raw[i] & 0xff) + 0.715160d + * ((int) raw[i + 1] & 0xff) + 0.072169d + * ((int) raw[i + 2] & 0xff); + switch (bitsPerPixel) { + case 1: + if (greyVal < 128) { + ib |= (byte) (1 << (7 - (x % 8))); + } + break; + case 4: + greyVal /= 16; + ib |= (byte) ((byte) greyVal << ((1 - (x % 2)) * 4)); + break; + case 8: + ib = (byte) greyVal; + break; + default: + throw new UnsupportedOperationException( + "Unsupported bits per pixel: " + bitsPerPixel); + } + + if ((x % pixelsPerByte) == (pixelsPerByte - 1) + || ((x + 1) == width)) { + bw[(y * bytewidth) + (x / pixelsPerByte)] = ib; + ib = 0; + } + } + } + io.setImageIDESize((byte) bitsPerPixel); + io.setImageData(bw); + } + + private static final String IMAGE_NAME_PREFIX = "IMG"; + private static final String GRAPHIC_NAME_PREFIX = "GRA"; + private static final String PAGE_SEGMENT_NAME_PREFIX = "PSG"; + // not currently used/implemented + private static final String BARCODE_NAME_PREFIX = "BAR"; + private static final String OTHER_NAME_PREFIX = "OTH"; + /** * Helper method to create an image on the current container and to return * the object. - * @param params the image object parameters - * @return the image object + * @param info the image object info + * @return a newly created image object */ - public IncludeObject createImageObject(ImageObjectParameters params) { - return getResourceGroup().addObject(params); + protected ImageObject createImage(ImageObjectInfo info) { + String name = IMAGE_NAME_PREFIX + + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); + ImageObject imageObj = new ImageObject(name); + if (info.hasCompression()) { + int compression = info.getCompression(); + switch (compression) { + case TIFFImage.COMP_FAX_G3_1D: + imageObj.setImageEncoding(ImageContent.COMPID_G3_MH); + break; + case TIFFImage.COMP_FAX_G3_2D: + imageObj.setImageEncoding(ImageContent.COMPID_G3_MR); + break; + case TIFFImage.COMP_FAX_G4_2D: + imageObj.setImageEncoding(ImageContent.COMPID_G3_MMR); + break; + default: + throw new IllegalStateException( + "Invalid compression scheme: " + compression); + } + } + imageObj.setFullyQualifiedName( + FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, + FullyQualifiedNameTriplet.FORMAT_URL, info.getUri()); + imageObj.setImageParameters(info.getWidthRes(), info.getHeightRes(), + info.getDataWidth(), info.getDataHeight()); + if (info.isColor()) { + imageObj.setImageIDESize((byte)24); + imageObj.setImageData(info.getData()); + } else { + convertToGrayScaleImage(imageObj, info.getData(), + info.getDataWidth(), info.getDataHeight(), + info.getBitsPerPixel()); + } + return imageObj; } - + /** * Helper method to create a graphic in the current container and to return * the object. - * @param params the data object parameters - * @return the graphics object + * @return a newly created graphics object */ - public IncludeObject createGraphicsObject(DataObjectParameters params) { - return getResourceGroup().addObject(params); + protected GraphicsObject createGraphic() { + String name = GRAPHIC_NAME_PREFIX + + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); + GraphicsObject graphicsObj = new GraphicsObject(name); + return graphicsObj; } - + + /** + * Helper method to create a page segment in the current container and to return + * the object. + * @return a newly created page segment + */ + protected PageSegment createPageSegment() { + String name = PAGE_SEGMENT_NAME_PREFIX + + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); + PageSegment pageSegment = new PageSegment(name); + return pageSegment; + } + + /** + * Creates and returns a new data object + * @param info the data object parameters + * @return a newly created data object + */ + public AbstractNamedAFPObject createObject(DataObjectInfo info) { + AbstractNamedAFPObject dataObject; + if (info instanceof ImageObjectInfo) { + dataObject = createImage((ImageObjectInfo)info); + } else { + dataObject = createGraphic(); + } + return dataObject; + } + /** * {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/afp/modca/AbstractStructuredAFPObject.java b/src/java/org/apache/fop/render/afp/modca/AbstractStructuredAFPObject.java index 3c448b2ab..6ec3a6d63 100644 --- a/src/java/org/apache/fop/render/afp/modca/AbstractStructuredAFPObject.java +++ b/src/java/org/apache/fop/render/afp/modca/AbstractStructuredAFPObject.java @@ -31,6 +31,7 @@ import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; import org.apache.fop.render.afp.modca.triplets.MeasurementUnitsTriplet; import org.apache.fop.render.afp.modca.triplets.ObjectAreaSizeTriplet; import org.apache.fop.render.afp.modca.triplets.ObjectClassificationTriplet; +import org.apache.fop.render.afp.modca.triplets.StrucFlgs; import org.apache.fop.render.afp.modca.triplets.Triplet; /** @@ -58,6 +59,12 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { * @return the triplet data length */ protected int getTripletDataLength() { + if (tripletData == null) { + try { + getTripletData(); + } catch (IOException e) { + } + } if (tripletData != null) { return tripletData.length; } @@ -65,27 +72,18 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { } /** - * Helper method to write the start of the Object. - * @param os The stream to write to - * @throws IOException an I/O exception if one occurred + * @return the triplet data + * @throws IOException throws an I/O exception if one occurred */ - protected void writeStart(OutputStream os) throws IOException { - if (triplets != null) { + protected byte[] getTripletData() throws IOException { + if (tripletData == null && triplets != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); writeObjects(triplets, baos); this.tripletData = baos.toByteArray(); } + return this.tripletData; } - - /** - * Helper method to write the contents of the Object. - * @param os The stream to write to - * @throws IOException The stream to write to - */ - protected void writeContent(OutputStream os) throws IOException { - writeTriplets(os); - } - + /** * Writes any triplet data * @param os The stream to write to @@ -98,6 +96,24 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { writeObjects(triplets, os); } } + + /** + * Helper method to write the start of the Object. + * @param os The stream to write to + * @throws IOException throws an I/O exception if one occurred + */ + protected void writeStart(OutputStream os) throws IOException { + getTripletData(); + } + + /** + * Helper method to write the contents of the Object. + * @param os The stream to write to + * @throws IOException throws an I/O exception if one occurred + */ + protected void writeContent(OutputStream os) throws IOException { + writeTriplets(os); + } /** * Helper method to write the end of the Object. @@ -176,9 +192,8 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { * @param fqName the fully qualified name of this resource */ public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) { - byte[] fqNameBytes; try { - fqNameBytes = fqName.getBytes(AFPConstants.EBCIDIC_ENCODING); + byte[] fqNameBytes = fqName.getBytes(AFPConstants.EBCIDIC_ENCODING); addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqNameBytes)); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); @@ -191,15 +206,17 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { public String getFullyQualifiedName() { FullyQualifiedNameTriplet fqNameTriplet = (FullyQualifiedNameTriplet)getTriplet(Triplet.FULLY_QUALIFIED_NAME); - byte[] nameBytes = fqNameTriplet.getFullyQualifiedName(); - if (nameBytes != null) { - try { - return new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException e) { - log.error(e.getMessage()); + if (fqNameTriplet != null) { + byte[] nameBytes = fqNameTriplet.getFullyQualifiedName(); + if (nameBytes != null) { + try { + return new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); + } catch (UnsupportedEncodingException e) { + log.error(e.getMessage()); + } + } else { + log.warn(this + " has no fully qualified name"); } - } else { - log.warn(this + " has no fully qualified name"); } return null; } @@ -207,12 +224,15 @@ public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { /** * Sets the objects classification * @param objectClass the classification of the object - * @param componentId the component Id of the object + * @param objectType the MOD:CA registry object type entry for the given + * object/component type of the object + * @param strucFlgs information on the structure of the object and its container */ - public void setObjectClassification(byte objectClass, byte componentId) { - addTriplet(new ObjectClassificationTriplet(objectClass, componentId)); + public void setObjectClassification(byte objectClass, ObjectTypeRegistry.ObjectType objectType, + StrucFlgs strucFlgs) { + addTriplet(new ObjectClassificationTriplet(objectClass, objectType, strucFlgs)); } - + /** * Specifies the extent of an object area in the X and Y directions * @param x the x direction extent diff --git a/src/java/org/apache/fop/render/afp/modca/ActiveEnvironmentGroup.java b/src/java/org/apache/fop/render/afp/modca/ActiveEnvironmentGroup.java index 845af4dfc..df4975e59 100644 --- a/src/java/org/apache/fop/render/afp/modca/ActiveEnvironmentGroup.java +++ b/src/java/org/apache/fop/render/afp/modca/ActiveEnvironmentGroup.java @@ -146,8 +146,8 @@ public final class ActiveEnvironmentGroup extends AbstractEnvironmentGroup { super.writeTriplets(os); writeObjects(mapCodedFonts, os); - writeObjects(mapPageOverlays, os); writeObjects(mapDataResources, os); + writeObjects(mapPageOverlays, os); if (pageDescriptor != null) { pageDescriptor.writeDataStream(os); @@ -258,12 +258,11 @@ public final class ActiveEnvironmentGroup extends AbstractEnvironmentGroup { } } - /** * Method to create a map data resource object * @param obj creates a map data resource entry for a given AFP data resource object */ - public void createResource(AbstractStructuredAFPObject obj) { + public void createResource(IncludeObject obj) { getMapDataResources().add(new MapDataResource(obj)); } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/modca/IncludeObject.java b/src/java/org/apache/fop/render/afp/modca/IncludeObject.java index 0fceff893..799578966 100644 --- a/src/java/org/apache/fop/render/afp/modca/IncludeObject.java +++ b/src/java/org/apache/fop/render/afp/modca/IncludeObject.java @@ -19,9 +19,12 @@ package org.apache.fop.render.afp.modca; +import java.io.File; import java.io.IOException; import java.io.OutputStream; +import org.apache.fop.render.afp.ResourceInfo; +import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; import org.apache.fop.render.afp.tools.BinaryUtils; /** @@ -94,38 +97,84 @@ public class IncludeObject extends AbstractNamedAFPObject { */ private int yContentOffset = -1; +// /** +// * The object referenced by this include object +// */ +// private Resource resourceObj = null; + /** - * The object referenced by this include object + * the level at which this resource object resides */ - private AbstractStructuredAFPObject referencedObject = null; + private ResourceInfo level = null; + + /** + * the referenced data object + */ + private AbstractNamedAFPObject dataObj = null; /** * Constructor for the include object with the specified name, the name must * be a fixed length of eight characters and is the name of the referenced * object. * - * @param resourceObj - * the resource object wrapper + * @param resourceObj the resource object wrapper */ - public IncludeObject(ResourceObject resourceObj) { - super(resourceObj.getResource().getName()); - this.referencedObject = resourceObj.getResource(); - if (referencedObject instanceof ImageObject) { + public IncludeObject(AbstractNamedAFPObject dataObj) { + super(dataObj.getName()); + this.dataObj = dataObj; +// AbstractStructuredAFPObject referencedObject = resourceObj.getReferencedObject(); + if (dataObj instanceof ImageObject) { this.objectType = TYPE_IMAGE; - } else if (referencedObject instanceof GraphicsObject) { + } else if (dataObj instanceof GraphicsObject) { this.objectType = TYPE_GRAPHIC; - } else if (referencedObject instanceof PageSegment) { + } else if (dataObj instanceof PageSegment) { this.objectType = TYPE_PAGE_SEGMENT; } else { this.objectType = TYPE_OTHER; } + + // set data object reference triplet + ResourceInfo resourceInfo = getResourceInfo(); + if (resourceInfo != null && resourceInfo.isExternal()) { + String dest = resourceInfo.getExternalResourceGroupDest(); + super.setFullyQualifiedName( + FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_EXTERNAL_RESOURCE_REF, + FullyQualifiedNameTriplet.FORMAT_URL, dest); + } else { + super.setFullyQualifiedName( + FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_INTERNAL_RESOURCE_REF, + FullyQualifiedNameTriplet.FORMAT_CHARSTR, dataObj.getName()); + } + } + + /** + * Sets the resource level for this resource object + * @param level the resource level + */ + public void setResourceInfo(ResourceInfo level) { + this.level = level; + } + + /** + * @return the resource info of this resource object + */ + public ResourceInfo getResourceInfo() { + return this.level; } +// /** +// * @return the resource container object referenced by this include object +// */ +// public Resource getResource() { +// return this.resourceObj; +// } + /** - * @return the object referenced by this include object + * @return the actual resource data object referenced by this include object */ public AbstractStructuredAFPObject getReferencedObject() { - return referencedObject; + return this.dataObj; + //getResource().getReferencedObject(); } /** @@ -167,12 +216,12 @@ public class IncludeObject extends AbstractNamedAFPObject { /** * {@inheritDoc} */ - public void writeDataStream(OutputStream os) throws IOException { + public void writeDataStream(OutputStream os) throws IOException { byte[] data = new byte[36]; data[0] = 0x5A; // Set the total record length - byte[] len = BinaryUtils.convert(35, 2); //Ignore first byte + byte[] len = BinaryUtils.convert(35 + getTripletDataLength(), 2); //Ignore first byte data[1] = len[0]; data[2] = len[1]; @@ -272,8 +321,13 @@ public class IncludeObject extends AbstractNamedAFPObject { data[33] = (byte)0xFF; data[34] = (byte)0xFF; } - data[35] = 0x01; + + // Write structured field data os.write(data); + + // Write triplet for FQN internal/external object reference + byte[] tripletData = super.getTripletData(); + os.write(tripletData); } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/modca/MapDataResource.java b/src/java/org/apache/fop/render/afp/modca/MapDataResource.java index 66ae57d75..3ccfd450a 100644 --- a/src/java/org/apache/fop/render/afp/modca/MapDataResource.java +++ b/src/java/org/apache/fop/render/afp/modca/MapDataResource.java @@ -22,7 +22,11 @@ package org.apache.fop.render.afp.modca; import java.io.IOException; import java.io.OutputStream; +//import org.apache.fop.render.afp.ResourceLevel; +import org.apache.fop.render.afp.ResourceInfo; import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; +import org.apache.fop.render.afp.modca.triplets.ObjectClassificationTriplet; +import org.apache.fop.render.afp.modca.triplets.StrucFlgs; import org.apache.fop.render.afp.tools.BinaryUtils; /** @@ -39,13 +43,42 @@ public class MapDataResource extends AbstractStructuredAFPObject { * Main constructor * @param obj a map data resource for a given structured AFP object */ - public MapDataResource(AbstractStructuredAFPObject obj) { - String fqName = obj.getFullyQualifiedName(); + public MapDataResource(IncludeObject includeObj) { + AbstractStructuredAFPObject refObj = includeObj.getReferencedObject(); +// ResourceObject resObj = includeObj.getResource(); + String fqName = refObj.getFullyQualifiedName(); + ResourceInfo resourceInfo = includeObj.getResourceInfo(); if (fqName != null) { - super.setFullyQualifiedName( - FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, - FullyQualifiedNameTriplet.FORMAT_CHARSTR, fqName); + if (resourceInfo.isExternal()) { + String dest = resourceInfo.getExternalResourceGroupDest(); + super.setFullyQualifiedName( + FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_EXTERNAL_RESOURCE_REF, + FullyQualifiedNameTriplet.FORMAT_CHARSTR, dest); + } else { +// super.setFullyQualifiedName( +// FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_INTERNAL_RESOURCE_REF, +// FullyQualifiedNameTriplet.FORMAT_CHARSTR, fqName); + super.setFullyQualifiedName( + FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, + FullyQualifiedNameTriplet.FORMAT_CHARSTR, fqName); + } } +// if (fqName != null) { +// super.setFullyQualifiedName( +// FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, +// FullyQualifiedNameTriplet.FORMAT_CHARSTR, fqName); +// } + final boolean dataInContainer = true; + final boolean containerHasOEG = false; + final boolean dataInOCD = true; + StrucFlgs strucFlgs = new StrucFlgs( + dataInContainer, containerHasOEG, dataInOCD + ); + + //TODO: AC - fix +// super.setObjectClassification( +// ObjectClassificationTriplet.CLASS_TIME_VARIANT_PRESENTATION_OBJECT, +// objectType, strucFlgs); } /** @@ -54,8 +87,7 @@ public class MapDataResource extends AbstractStructuredAFPObject { public void writeStart(OutputStream os) throws IOException { super.writeStart(os); - // RGLength - byte[] len = BinaryUtils.convert(8 + getTripletDataLength(), 2); + byte[] len = BinaryUtils.convert(10 + getTripletDataLength(), 2); byte[] data = new byte[] { 0x5A, // Structured field identifier len[0], // Length byte 1 @@ -69,4 +101,15 @@ public class MapDataResource extends AbstractStructuredAFPObject { }; os.write(data); } + + /** + * {@inheritDoc} + */ + public void writeContent(OutputStream os) throws IOException { + // RGLength + byte[] len = BinaryUtils.convert(2 + getTripletDataLength(), 2); + os.write(len); + + super.writeTriplets(os); + } } diff --git a/src/java/org/apache/fop/render/afp/modca/ObjectContainer.java b/src/java/org/apache/fop/render/afp/modca/ObjectContainer.java new file mode 100644 index 000000000..59e770d6d --- /dev/null +++ b/src/java/org/apache/fop/render/afp/modca/ObjectContainer.java @@ -0,0 +1,171 @@ +/* + * 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.render.afp.modca; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.render.afp.DataObjectInfo; +import org.apache.fop.render.afp.modca.triplets.ObjectClassificationTriplet; +import org.apache.fop.render.afp.modca.triplets.StrucFlgs; +import org.apache.fop.render.afp.tools.BinaryUtils; + +/** + * Object containers are MO:DCA objects that envelop and carry object data. + */ +public class ObjectContainer extends AbstractNamedAFPObject { + + /** + * the object container data + */ + private ObjectContainerData objectContainerData; + + /** + * Main constructor + * @param name the name of this object container + * @param dataObj the data object to reside within this object container + * @param info the data object info about the data object + */ + public ObjectContainer(String name, AbstractDataObject dataObj, DataObjectInfo info) { + super(name); + + final boolean dataInContainer = true; + final boolean containerHasOEG = false; + final boolean dataInOCD = true; + StrucFlgs strucFlgs = new StrucFlgs( + dataInContainer, containerHasOEG, dataInOCD + ); + ObjectTypeRegistry registry = ObjectTypeRegistry.getInstance(); + ObjectTypeRegistry.ObjectType entry = registry.getObjectType(info); + super.setObjectClassification( + ObjectClassificationTriplet.CLASS_TIME_VARIANT_PRESENTATION_OBJECT, + entry, strucFlgs + ); + + // write data object to object container data + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + // TODO: AC - fix +// dataObj.writeDataStream(bos); + this.objectContainerData = new ObjectContainerData(bos.toByteArray()); + } + + /** + * {@inheritDoc} + */ + protected void writeStart(OutputStream os) throws IOException { + super.writeStart(os); + + // Set the total record length + byte[] len = BinaryUtils.convert(18 + getTripletDataLength(), 2); + byte[] data = new byte[] { + 0x5A, // Structured field identifier + len[0], // Length byte 1 + len[1], // Length byte 2 + (byte)0xD3, // Structured field id byte 1 + (byte)0xA8, // Structured field id byte 2 + (byte)0x92, // Structured field id byte 3 + 0x00, // Flags + 0x00, // Reserved + 0x00, // Reserved + nameBytes[0], + nameBytes[1], + nameBytes[2], + nameBytes[3], + nameBytes[4], + nameBytes[5], + nameBytes[6], + nameBytes[7], + 0x00, // Reserved + 0x00, // Reserved + }; + os.write(data); + } + + /** + * {@inheritDoc} + */ + protected void writeContent(OutputStream os) throws IOException { + super.writeContent(os); // write triplets + if (objectContainerData != null) { + objectContainerData.writeDataStream(os); + } + } + + /** + * {@inheritDoc} + */ + protected void writeEnd(OutputStream os) throws IOException { + byte[] data = new byte[] { + 0x5A, // Structured field identifier + 0x00, // Length byte 1 + 0x10, // Length byte 2 + (byte)0xD3, // Structured field id byte 1 + (byte)0xA9, // Structured field id byte 2 + (byte)0x92, // Structured field id byte 3 + 0x00, // Flags + 0x00, // Reserved + 0x00, // Reserved + nameBytes[0], + nameBytes[1], + nameBytes[2], + nameBytes[3], + nameBytes[4], + nameBytes[5], + nameBytes[6], + nameBytes[7], + }; + os.write(data); + } + + private class ObjectContainerData extends AbstractStructuredAFPObject { + /** the object data */ + private byte[] objData = null; + + /** + * Main constructor + * @param objData the object data + */ + public ObjectContainerData(byte[] objData) { + this.objData = objData; + } + + /** + * {@inheritDoc} + */ + public void writeDataStream(OutputStream os) throws IOException { + // Set the total record length + byte[] len = BinaryUtils.convert(8 + objData.length, 2); + byte[] data = new byte[] { + 0x5A, // Structured field identifier + len[0], // Length byte 1 + len[1], // Length byte 2 + (byte)0xD3, // Structured field id byte 1 + (byte)0xEE, // Structured field id byte 2 + (byte)0x92, // Structured field id byte 3 + 0x00, // Flags + 0x00, // Reserved + 0x00, // Reserved + }; + + os.write(data); + } + } +} diff --git a/src/java/org/apache/fop/render/afp/modca/ObjectTypeRegistry.java b/src/java/org/apache/fop/render/afp/modca/ObjectTypeRegistry.java new file mode 100644 index 000000000..278a56732 --- /dev/null +++ b/src/java/org/apache/fop/render/afp/modca/ObjectTypeRegistry.java @@ -0,0 +1,204 @@ +/* + * 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.render.afp.modca; + +import java.io.UnsupportedEncodingException; +import java.util.Collections; + +import org.apache.commons.logging.LogFactory; +import org.apache.fop.render.afp.DataObjectInfo; +import org.apache.fop.render.afp.ImageObjectInfo; +import org.apache.xmlgraphics.util.MimeConstants; + +/** + * MOD:CA object type registry + */ +public final class ObjectTypeRegistry { + /** IOB supported object types */ + private static final byte COMPID_GIF = 22; + private static final byte COMPID_JFIF = 23; // jpeg file interchange format + private static final byte COMPID_PDF_SINGLE_PAGE = 25; + private static final byte COMPID_PCL_PAGE_OBJECT = 34; + + /** IOB unsupported object types */ + private static final byte COMPID_EPS = 13; + private static final byte COMPID_TIFF = 14; + + /** mime type entry mapping */ + private java.util.Map/*<String, ObjectTypeRegistry.Entry>*/ mimeEntryMap + = Collections.synchronizedMap( + Collections.unmodifiableMap( + new java.util.HashMap/*<String, ObjectTypeRegistry.Entry>*/())); + + private static ObjectTypeRegistry instance = null; + + /** + * @return a single instance of Registry + */ + public static ObjectTypeRegistry getInstance() { + synchronized (instance) { + if (instance == null) { + instance = new ObjectTypeRegistry(); + } + } + return instance; + } + + private ObjectTypeRegistry() { + init(); + } + + private void init() { + mimeEntryMap.put( + MimeConstants.MIME_EPS, + new ObjectType( + COMPID_EPS, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0D}, + "Encapsulated Postscript", + false + ) + ); + mimeEntryMap.put( + MimeConstants.MIME_TIFF, + new ObjectType( + COMPID_TIFF, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0E}, + "TIFF", + false + ) + ); + mimeEntryMap.put( + MimeConstants.MIME_GIF, + new ObjectType( + COMPID_GIF, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x16}, + "GIF", + true + ) + ); + mimeEntryMap.put( + MimeConstants.MIME_JPEG, + new ObjectType( + COMPID_JFIF, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x17}, + "JFIF", + true + ) + ); + mimeEntryMap.put(MimeConstants.MIME_PDF, + new ObjectType( + COMPID_PDF_SINGLE_PAGE, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x19}, + "PDF Single-page Object", + true + ) + ); + mimeEntryMap.put( + MimeConstants.MIME_PCL, + new ObjectType( + COMPID_PCL_PAGE_OBJECT, + new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x22}, + "PCL Page Object", + true + ) + ); + } + + /** + * Returns the registry Entry for a given data object info + * + * @param info + * the data object info + * @return the registry Entry for a given data object info + */ + public ObjectType getObjectType(DataObjectInfo info) { + ObjectType entry = null; + if (info instanceof ImageObjectInfo) { + ImageObjectInfo imageInfo = (ImageObjectInfo)info; + String mimeType = imageInfo.getMimeType(); + entry = (ObjectType)mimeEntryMap.get(mimeType); + } + return entry; + } + + /** + * Encapsulates a MOD:CA Registry Object Type entry + */ + public class ObjectType { + private byte componentId; + private byte[] oid; + private byte[] name; + private boolean canBeIncluded; + + /** + * Main constructor + * @param componentId the component id of this object type + * @param oid the object id of this object type + * @param objectTypeName the object type name + * @param canBeIncluded true if this object can be included with an IOB structured field + */ + public ObjectType(byte componentId, byte[] oid, String objectTypeName, + boolean canBeIncluded) { + this.componentId = componentId; + this.oid = oid; + try { + this.name = objectTypeName.getBytes(AFPConstants.EBCIDIC_ENCODING); + } catch (UnsupportedEncodingException e) { + // should never happen! + LogFactory.getLog("org.apache.fop.render.afp.modca.Registry.Entry").error( + "character encoding error occurred on componentId " + + componentId + + " : " + + e.getMessage()); + } + this.canBeIncluded = canBeIncluded; + } + + /** + * Returns a MOD:CA object type OID from a given a componentId + * @return the corresponding object type id for a given component id + * or null if the component id is unknown and the object type OID was not found. + */ + public byte[] getOID() { + return this.oid; + } + + /** + * @return the object type name for the given componentId + */ + public byte[] getName() { + return this.name; + } + + /** + * @return the compontentId for this entry + */ + public byte getComponentId() { + return this.componentId; + } + + /** + * @return true if this component can be included with an IOB structured field + */ + public boolean canBeIncluded() { + return this.canBeIncluded; + } + } +} diff --git a/src/java/org/apache/fop/render/afp/modca/PageSegment.java b/src/java/org/apache/fop/render/afp/modca/PageSegment.java index f88e3cc55..1ff4b8e5f 100644 --- a/src/java/org/apache/fop/render/afp/modca/PageSegment.java +++ b/src/java/org/apache/fop/render/afp/modca/PageSegment.java @@ -31,7 +31,7 @@ import java.util.List; */ public class PageSegment extends AbstractNamedAFPObject { - private List objects = null; + private List/*<AbstractAFPObject>*/ objects = null; /** * Main constructor @@ -42,14 +42,21 @@ public class PageSegment extends AbstractNamedAFPObject { } /** - * Adds a resource object (image/graphic) to this page segment - * @param object the resource objec to add to this page segment + * @return a list of objects contained within this page segment */ - public void addObject(AbstractAFPObject object) { + public List/*<AbstractAFPObject>*/ getObjects() { if (objects == null) { objects = new java.util.ArrayList(); } - objects.add(object); + return objects; + } + + /** + * Adds a resource object (image/graphic) to this page segment + * @param object the resource objec to add to this page segment + */ + public void addObject(AbstractAFPObject object) { + getObjects().add(object); } /** @@ -75,6 +82,14 @@ public class PageSegment extends AbstractNamedAFPObject { /** * {@inheritDoc} */ + protected void writeContent(OutputStream os) throws IOException { + super.writeContent(os); + writeObjects(objects, os); + } + + /** + * {@inheritDoc} + */ protected void writeEnd(OutputStream os) throws IOException { byte[] data = new byte[17]; data[0] = 0x5A; // Structured field identifier @@ -90,13 +105,5 @@ public class PageSegment extends AbstractNamedAFPObject { data[9 + i] = nameBytes[i]; } os.write(data); - } - - /** - * {@inheritDoc} - */ - protected void writeContent(OutputStream os) throws IOException { - super.writeContent(os); - writeObjects(objects, os); - } + } } diff --git a/src/java/org/apache/fop/render/afp/modca/PresentationTextData.java b/src/java/org/apache/fop/render/afp/modca/PresentationTextData.java index 98beb96f7..ec93ebebd 100644 --- a/src/java/org/apache/fop/render/afp/modca/PresentationTextData.java +++ b/src/java/org/apache/fop/render/afp/modca/PresentationTextData.java @@ -54,52 +54,50 @@ public class PresentationTextData extends AbstractAFPObject { private static final int MAX_SIZE = 8192; /** - * The afp data relating to this presentaion text data. + * The afp data relating to this presentation text data. */ - private ByteArrayOutputStream _baos = new ByteArrayOutputStream(1024); + private ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); /** * The current x coordinate. */ - private int _currentXCoordinate = -1; + private int currentXCoordinate = -1; /** * The current y cooridnate */ - private int _currentYCoordinate = -1; + private int currentYCoordinate = -1; /** * The current font */ - private String _currentFont = ""; + private String currentFont = ""; /** * The current orientation */ - private int _currentOrientation = 0; + private int currentOrientation = 0; /** * The current color */ - private Color _currentColor = new Color(0, 0, 0); + private Color currentColor = new Color(0, 0, 0); /** * The current variable space increment */ - private int _currentVariableSpaceCharacterIncrement = 0; + private int currentVariableSpaceCharacterIncrement = 0; /** * The current inter character adjustment */ - private int _currentInterCharacterAdjustment = 0; + private int currentInterCharacterAdjustment = 0; /** * Default constructor for the PresentationTextData. */ public PresentationTextData() { - this(false); - } /** @@ -111,8 +109,7 @@ public class PresentationTextData extends AbstractAFPObject { * The control sequence indicator. */ public PresentationTextData(boolean controlInd) { - - _baos.write(new byte[] { 0x5A, // Structured field identifier + baos.write(new byte[] { 0x5A, // Structured field identifier 0x00, // Record length byte 1 0x00, // Record length byte 2 (byte) 0xD3, // PresentationTextData identifier byte 1 @@ -124,9 +121,8 @@ public class PresentationTextData extends AbstractAFPObject { }, 0, 9); if (controlInd) { - _baos.write(new byte[] { 0x2B, (byte) 0xD3 }, 0, 2); + baos.write(new byte[] { 0x2B, (byte) 0xD3 }, 0, 2); } - } /** @@ -140,16 +136,14 @@ public class PresentationTextData extends AbstractAFPObject { * The output stream to which data should be written. */ private void setCodedFont(byte font, ByteArrayOutputStream afpdata) { - // Avoid unnecessary specification of the font - if (String.valueOf(font).equals(_currentFont)) { + if (String.valueOf(font).equals(currentFont)) { return; } else { - _currentFont = String.valueOf(font); + currentFont = String.valueOf(font); } afpdata.write(new byte[] { 0x03, (byte) 0xF1, font, }, 0, 3); - } /** @@ -162,15 +156,10 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void absoluteMoveInline(int coordinate, - ByteArrayOutputStream afpdata) { - + private void absoluteMoveInline(int coordinate, ByteArrayOutputStream afpdata) { byte[] b = BinaryUtils.convert(coordinate, 2); - afpdata.write(new byte[] { 0x04, (byte) 0xC7, b[0], b[1], }, 0, 4); - - _currentXCoordinate = coordinate; - + currentXCoordinate = coordinate; } /** @@ -183,15 +172,10 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void absoluteMoveBaseline(int coordinate, - ByteArrayOutputStream afpdata) { - + private void absoluteMoveBaseline(int coordinate, ByteArrayOutputStream afpdata) { byte[] b = BinaryUtils.convert(coordinate, 2); - afpdata.write(new byte[] { 0x04, (byte) 0xD3, b[0], b[1], }, 0, 4); - - _currentYCoordinate = coordinate; - + currentYCoordinate = coordinate; } /** @@ -204,21 +188,16 @@ public class PresentationTextData extends AbstractAFPObject { * The output stream to which data should be written. */ private void addTransparentData(byte[] data, ByteArrayOutputStream afpdata) { - // Calculate the length int l = data.length + 2; - if (l > 255) { // Check that we are not exceeding the maximum length throw new IllegalArgumentException( "Transparent data is longer than 253 bytes: " + data); } - afpdata.write(new byte[] { BinaryUtils.convert(l)[0], (byte) 0xDB, }, 0, 2); - afpdata.write(data, 0, data.length); - } /** @@ -233,13 +212,10 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void drawBaxisRule(int length, int width, - ByteArrayOutputStream afpdata) { - + private void drawBaxisRule(int length, int width, ByteArrayOutputStream afpdata) { afpdata.write(new byte[] { 0x07, // Length (byte) 0xE7, // Type }, 0, 2); - // Rule length byte[] data1 = BinaryUtils.shortToByteArray((short) length); afpdata.write(data1, 0, data1.length); @@ -248,7 +224,6 @@ public class PresentationTextData extends AbstractAFPObject { afpdata.write(data2, 0, data2.length); // Rule width fraction afpdata.write(0x00); - } /** @@ -263,13 +238,10 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void drawIaxisRule(int length, int width, - ByteArrayOutputStream afpdata) { - + private void drawIaxisRule(int length, int width, ByteArrayOutputStream afpdata) { afpdata.write(new byte[] { 0x07, // Length (byte) 0xE5, // Type }, 0, 2); - // Rule length byte[] data1 = BinaryUtils.shortToByteArray((short) length); afpdata.write(data1, 0, data1.length); @@ -278,7 +250,6 @@ public class PresentationTextData extends AbstractAFPObject { afpdata.write(data2, 0, data2.length); // Rule width fraction afpdata.write(0x00); - } /** @@ -303,62 +274,60 @@ public class PresentationTextData extends AbstractAFPObject { * @throws MaximumSizeExceededException */ public void createTextData(int fontNumber, int x, int y, int orientation, - Color col, int vsci, int ica, byte[] data) - throws MaximumSizeExceededException { + Color col, int vsci, int ica, byte[] data) throws MaximumSizeExceededException { ByteArrayOutputStream afpdata = new ByteArrayOutputStream(); - if (_currentOrientation != orientation) { + if (currentOrientation != orientation) { setTextOrientation(orientation, afpdata); - _currentOrientation = orientation; - _currentXCoordinate = -1; - _currentYCoordinate = -1; + currentOrientation = orientation; + currentXCoordinate = -1; + currentYCoordinate = -1; } // Avoid unnecessary specification of the Y co-ordinate - if (y != _currentYCoordinate) { + if (y != currentYCoordinate) { absoluteMoveBaseline(y, afpdata); - _currentXCoordinate = -1; + currentXCoordinate = -1; } // Avoid unnecessary specification of the X co-ordinate - if (x != _currentXCoordinate) { + if (x != currentXCoordinate) { absoluteMoveInline(x, afpdata); } // Avoid unnecessary specification of the variable space increment - if (vsci != _currentVariableSpaceCharacterIncrement) { + if (vsci != currentVariableSpaceCharacterIncrement) { setVariableSpaceCharacterIncrement(vsci, afpdata); - _currentVariableSpaceCharacterIncrement = vsci; + currentVariableSpaceCharacterIncrement = vsci; } // Avoid unnecessary specification of the inter character adjustment - if (ica != _currentInterCharacterAdjustment) { + if (ica != currentInterCharacterAdjustment) { setInterCharacterAdjustment(ica, afpdata); - _currentInterCharacterAdjustment = ica; + currentInterCharacterAdjustment = ica; } // Avoid unnecessary specification of the text color - if (!col.equals(_currentColor)) { + if (!col.equals(currentColor)) { setExtendedTextColor(col, afpdata); - _currentColor = col; + currentColor = col; } setCodedFont(BinaryUtils.convert(fontNumber)[0], afpdata); addTransparentData(data, afpdata); - _currentXCoordinate = -1; + currentXCoordinate = -1; int s = afpdata.size(); - if (_baos.size() + s > MAX_SIZE) { - _currentXCoordinate = -1; - _currentYCoordinate = -1; + if (baos.size() + s > MAX_SIZE) { + currentXCoordinate = -1; + currentYCoordinate = -1; throw new MaximumSizeExceededException(); } byte[] outputdata = afpdata.toByteArray(); - _baos.write(outputdata, 0, outputdata.length); - + baos.write(outputdata, 0, outputdata.length); } /** @@ -385,24 +354,24 @@ public class PresentationTextData extends AbstractAFPObject { ByteArrayOutputStream afpdata = new ByteArrayOutputStream(); - if (_currentOrientation != orientation) { + if (currentOrientation != orientation) { setTextOrientation(orientation, afpdata); - _currentOrientation = orientation; + currentOrientation = orientation; } // Avoid unnecessary specification of the Y coordinate - if (y1 != _currentYCoordinate) { + if (y1 != currentYCoordinate) { absoluteMoveBaseline(y1, afpdata); } // Avoid unnecessary specification of the X coordinate - if (x1 != _currentXCoordinate) { + if (x1 != currentXCoordinate) { absoluteMoveInline(x1, afpdata); } - if (!col.equals(_currentColor)) { + if (!col.equals(currentColor)) { setExtendedTextColor(col, afpdata); - _currentColor = col; + currentColor = col; } if (y1 == y2) { @@ -415,15 +384,14 @@ public class PresentationTextData extends AbstractAFPObject { int s = afpdata.size(); - if (_baos.size() + s > MAX_SIZE) { - _currentXCoordinate = -1; - _currentYCoordinate = -1; + if (baos.size() + s > MAX_SIZE) { + currentXCoordinate = -1; + currentYCoordinate = -1; throw new MaximumSizeExceededException(); } byte[] outputdata = afpdata.toByteArray(); - _baos.write(outputdata, 0, outputdata.length); - + baos.write(outputdata, 0, outputdata.length); } /** @@ -440,11 +408,8 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void setTextOrientation(int orientation, - ByteArrayOutputStream afpdata) { - + private void setTextOrientation(int orientation, ByteArrayOutputStream afpdata) { afpdata.write(new byte[] { 0x06, (byte) 0xF7, }, 0, 2); - switch (orientation) { case 90: afpdata.write(0x2D); @@ -471,7 +436,6 @@ public class PresentationTextData extends AbstractAFPObject { afpdata.write(0x00); break; } - } /** @@ -528,7 +492,6 @@ public class PresentationTextData extends AbstractAFPObject { , b[0] , b[1] }, 0, 4); - } /** @@ -540,11 +503,8 @@ public class PresentationTextData extends AbstractAFPObject { * @param afpdata * The output stream to which data should be written. */ - private void setInterCharacterAdjustment(int incr, - ByteArrayOutputStream afpdata) { - + private void setInterCharacterAdjustment(int incr, ByteArrayOutputStream afpdata) { byte[] b = BinaryUtils.convert(Math.abs(incr), 2); - afpdata.write(new byte[] { 5 // Control sequence length , (byte)0xC3 // Control sequence function type @@ -552,25 +512,17 @@ public class PresentationTextData extends AbstractAFPObject { , b[1] , (byte)(incr >= 0 ? 0 : 1) // Direction }, 0, 5); - } /** - * Accessor method to write the AFP datastream for - * the text data. - * @param os The stream to write to - * @throws java.io.IOException + * {@inheritDoc} */ - public void writeDataStream(OutputStream os) - throws IOException { - - byte[] data = _baos.toByteArray(); + public void writeDataStream(OutputStream os) throws IOException { + byte[] data = baos.toByteArray(); byte[] size = BinaryUtils.convert(data.length - 1, 2); data[1] = size[0]; data[2] = size[1]; - os.write(data); - } /** @@ -583,17 +535,12 @@ public class PresentationTextData extends AbstractAFPObject { * @throws MaximumSizeExceededException */ public void endControlSequence() throws MaximumSizeExceededException { - byte[] data = new byte[2]; data[0] = 0x02; data[1] = (byte) 0xF8; - - if (data.length + _baos.size() > MAX_SIZE) { + if (data.length + baos.size() > MAX_SIZE) { throw new MaximumSizeExceededException(); } - - _baos.write(data, 0, data.length); - + baos.write(data, 0, data.length); } - }
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/modca/Registry.java b/src/java/org/apache/fop/render/afp/modca/Registry.java deleted file mode 100644 index b643cd2d2..000000000 --- a/src/java/org/apache/fop/render/afp/modca/Registry.java +++ /dev/null @@ -1,353 +0,0 @@ -/* - * 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.render.afp.modca; - -import java.io.UnsupportedEncodingException; - -import org.apache.commons.logging.LogFactory; - -/** - * MOD:CA object type registry - */ -public final class Registry { - - /** IOB supported object types */ - public static final byte IOCA_FS10 = 5; - public static final byte IOCA_FS11 = 11; - public static final byte IOCA_FS45 = 12; - public static final byte DIB_WIN = 17; //device independent bitmap - public static final byte DIB_OS2 = 18; - public static final byte PCX = 19; - public static final byte GIF = 22; - public static final byte JFIF = 23; // jpeg file interchange format - public static final byte PDF_SINGLE_PAGE = 25; - public static final byte PCL_PAGE_OBJECT = 34; - public static final byte EPS_TRANS = 48; - public static final byte PDF_SINGLE_PAGE_TRANS = 49; - public static final byte JPEG2000 = 58; - - /** IOB unsupported object types */ - public static final byte EPS = 13; - public static final byte TIFF = 14; - public static final byte COM_SETUP_FILE = 15; - public static final byte TAPE_LABEL_SETUP_FILE = 16; - public static final byte CMT = 20; // color mapping table - public static final byte ANACOMP_CONTROL_RECORD = 24; - public static final byte PDF_RESOURCE_OBJECT = 26; - public static final byte IOCA_FS42 = 45; - public static final byte RESIDENT_COLOR_PROFILE = 46; - public static final byte IOCA_FS45_TILE_RESOURCE = 47; - public static final byte FONT = 51; - public static final byte FONT_COLLECTION = 53; - public static final byte RESOURCE_ACCESS_TABLE = 54; - public static final byte IOCA_FS40 = 55; - public static final byte UP3I_PRINT_DATA = 56; - public static final byte COLOR_MANAGEMENT_RESOURCE = 57; - - - /** internal mapping array references */ - private static final int COMPONENT_ID = 0; - private static final int OBJECT_TYPE_NAME = 1; - private static final int OBJECT_ID = 2; - private static final int CAN_BE_INCLUDED = 3; - - private byte[][][] componentIdMap; - - private static Registry instance = null; - - /** - * @return a single instance of Registry - */ - public static Registry getInstance() { - synchronized (instance) { - if (instance == null) { - instance = new Registry(); - } - } - return instance; - } - - private Registry() { - init(); - } - - private void init() { - try { - componentIdMap = new byte[][][] { - /* object type id (OID), object type name, compontent Id, can be included with IOB?, */ - { - {IOCA_FS10}, - "IOCA FS10".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x05}, - {1} - }, - { - {IOCA_FS11}, - "IOCA FS11".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0B}, - {1} - }, - { - {IOCA_FS45}, - "IOCA FS45".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0C}, - {1} - }, - { - {EPS}, - "Encapsulated Postscript".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0D}, - {0} - }, - { - {TIFF}, - "TIFF".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0E}, - {0} - }, - { - {COM_SETUP_FILE}, - "COM setup".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x0F}, - {0} - }, - { - {TAPE_LABEL_SETUP_FILE}, - "Tape Label setup".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x10}, - {0} - }, - { - {DIB_WIN}, - "DIB, Windows Version".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x11}, - {1} - }, - { - {DIB_OS2}, - "DIB, OS/2 PM Version".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x12}, - {1} - }, - { - {PCX}, - "PCX".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x13}, - {1} - }, - { - {CMT}, - "Color Mapping Table (CMT)".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x14}, - {0} - }, - { - {GIF}, - "GIF".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x16}, - {1} - }, - { - {JFIF}, - "JFIF".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x17}, - {1} - }, - { - {ANACOMP_CONTROL_RECORD}, - "AnaStak Control Record".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x18}, - {0} - }, - { - {PDF_SINGLE_PAGE}, - "PDF Single-page Object".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x19}, - {1} - }, - { - {PDF_RESOURCE_OBJECT}, - "PDF Resource Object".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x1A}, - {0} - }, - { - {PCL_PAGE_OBJECT}, - "PCL Page Object".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x22}, - {1} - }, - { - {IOCA_FS42}, - "IOCA FS42".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x2D}, - {0} - }, - { - {RESIDENT_COLOR_PROFILE}, - "Resident Color Profile".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x2E}, - {0} - }, - { - {IOCA_FS45_TILE_RESOURCE}, - "IOCA FS45".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x2E}, - {0} - }, - { - {EPS_TRANS}, - "EPS with Transparency".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x30}, - {1} - }, - { - {PDF_SINGLE_PAGE_TRANS}, - "PDF with Transparency".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x31}, - {1} - }, - { - {FONT}, - "TrueType/OpenType Font".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x33}, - {0} - }, - { - {FONT_COLLECTION}, - "TrueType/OpenType Font Collection".getBytes( - AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x35}, - {0} - }, - { - {RESOURCE_ACCESS_TABLE}, - "Resource Access Table".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x36}, - {0} - }, - { - {IOCA_FS40}, - "IOCA FS40".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x37}, - {0} - }, - { - {UP3I_PRINT_DATA}, - "IP3i Print Data".getBytes(AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x38}, - {0} - }, - { - {COLOR_MANAGEMENT_RESOURCE}, - "Color Management Resource (CMR)".getBytes( - AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x39}, - {0} - }, - { - {JPEG2000}, - "JPEG2000 (JP2) File Format".getBytes( - AFPConstants.EBCIDIC_ENCODING), - {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x3A}, - {1} - } - }; - } catch (UnsupportedEncodingException e) { - // should never happen! - LogFactory.getLog("org.apache.fop.render.afp.modca.Registry").error(e.getMessage()); - } - } - - private byte[][][] getComponentIdMap() { - if (componentIdMap == null) { - init(); - } - return componentIdMap; - } - - private byte[][] getMapData(byte compontentId) { - getComponentIdMap(); - for (int i = 0; i < componentIdMap.length; i++) { - if (compontentId == componentIdMap[i][0][0]) { - return componentIdMap[i]; - } - } - return null; - } - - /** - * Returns a registry Entry for a given componentId - * @param compontentId a compontent id - * @return the registry entry for a give componentId - */ - public Entry getEntry(byte compontentId) { - byte[][] data = getMapData(compontentId); - if (data != null) { - return new Registry.Entry(data); - } - return null; - } - - /** - * Encapsulates a MOD:CA Registry Entry - */ - public final class Entry { - private byte[][] data; - - /** - * Main constructor - * @param the map data structure array - */ - private Entry(byte[][] data) { - this.data = data; - } - - /** - * Returns a MOD:CA object type OID from a given a componentId - * @return the corresponding object type id for a given component id - * or null if the component id is unknown and the object type OID was not found. - */ - public byte[] getOID() { - return data[OBJECT_ID]; - } - - /** - * @return the object type name for the given componentId - */ - public byte[] getObjectTypeName() { - return data[OBJECT_TYPE_NAME]; - } - - /** - * @return the compontentId for this entry - */ - public byte[] getComponentId() { - return data[COMPONENT_ID]; - } - - /** - * @return true if this component can be included with an IOB structured field - */ - public boolean canBeIncluded() { - return data[CAN_BE_INCLUDED][0] == 1; - } - } -} diff --git a/src/java/org/apache/fop/render/afp/modca/ResourceEnvironmentGroup.java b/src/java/org/apache/fop/render/afp/modca/ResourceEnvironmentGroup.java index 080226e9f..09d4fabb6 100644 --- a/src/java/org/apache/fop/render/afp/modca/ResourceEnvironmentGroup.java +++ b/src/java/org/apache/fop/render/afp/modca/ResourceEnvironmentGroup.java @@ -95,7 +95,8 @@ public class ResourceEnvironmentGroup extends AbstractEnvironmentGroup { * @param obj the object to add */ public void addObject(AbstractStructuredAFPObject obj) { - getMapDataResources().add(new MapDataResource(obj)); + // TODO: AC - fix +// getMapDataResources().add(new MapDataResource(obj)); getPreprocessPresentationObjects().add(new PreprocessPresentationObject(obj)); } diff --git a/src/java/org/apache/fop/render/afp/modca/ResourceGroup.java b/src/java/org/apache/fop/render/afp/modca/ResourceGroup.java index 48f4f0eeb..38b9524e6 100644 --- a/src/java/org/apache/fop/render/afp/modca/ResourceGroup.java +++ b/src/java/org/apache/fop/render/afp/modca/ResourceGroup.java @@ -21,10 +21,13 @@ package org.apache.fop.render.afp.modca; import java.io.IOException; import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; import java.util.Map; -import org.apache.fop.render.afp.DataObjectParameters; -import org.apache.fop.render.afp.ImageObjectParameters; +import org.apache.fop.render.afp.DataObjectInfo; +import org.apache.fop.render.afp.ImageObjectInfo; +import org.apache.fop.render.afp.ResourceInfo; import org.apache.fop.render.afp.modca.triplets.FullyQualifiedNameTriplet; import org.apache.fop.render.afp.tools.StringUtils; import org.apache.xmlgraphics.image.codec.tiff.TIFFImage; @@ -42,13 +45,19 @@ public final class ResourceGroup extends AbstractNamedAFPObject { /** * Mapping of resource uri to data resource object (image/graphic) */ - private Map/*<String,AbstractAFPObject>*/ resourceMap = null; + private Map/*<String,IncludeObject>*/ resourceMap = null; + + /** + * This resource groups container + */ + private AbstractResourceGroupContainer container = null; /** * Default constructor + * @param container the resource group container */ - public ResourceGroup() { - this(DEFAULT_NAME); + public ResourceGroup(AbstractResourceGroupContainer container) { + this(DEFAULT_NAME, container); } /** @@ -56,159 +65,86 @@ public final class ResourceGroup extends AbstractNamedAFPObject { * name parameter which must be 8 characters long. * @param name the resource group name */ - public ResourceGroup(String name) { + public ResourceGroup(String name, AbstractResourceGroupContainer container) { super(name); + this.container = container; } - private static final String IMAGE_NAME_PREFIX = "IMG"; - private static final String GRAPHIC_NAME_PREFIX = "GRA"; - private static final String PAGE_SEGMENT_NAME_PREFIX = "PAG"; - private static final String BARCODE_NAME_PREFIX = "BAR"; - private static final String OTHER_NAME_PREFIX = "OTH"; + private static final String RESOURCE_NAME_PREFIX = "RES"; + private static final String INCLUDE_NAME_PREFIX = "INC"; /** - * Converts a byte array containing 24 bit RGB image data to a grayscale - * image. - * - * @param io - * the target image object - * @param raw - * the buffer containing the RGB image data - * @param width - * the width of the image in pixels - * @param height - * the height of the image in pixels - * @param bitsPerPixel - * the number of bits to use per pixel - */ - private static void convertToGrayScaleImage(ImageObject io, byte[] raw, int width, - int height, int bitsPerPixel) { - int pixelsPerByte = 8 / bitsPerPixel; - int bytewidth = (width / pixelsPerByte); - if ((width % pixelsPerByte) != 0) { - bytewidth++; - } - byte[] bw = new byte[height * bytewidth]; - byte ib; - for (int y = 0; y < height; y++) { - ib = 0; - int i = 3 * y * width; - for (int x = 0; x < width; x++, i += 3) { - - // see http://www.jguru.com/faq/view.jsp?EID=221919 - double greyVal = 0.212671d * ((int) raw[i] & 0xff) + 0.715160d - * ((int) raw[i + 1] & 0xff) + 0.072169d - * ((int) raw[i + 2] & 0xff); - switch (bitsPerPixel) { - case 1: - if (greyVal < 128) { - ib |= (byte) (1 << (7 - (x % 8))); - } - break; - case 4: - greyVal /= 16; - ib |= (byte) ((byte) greyVal << ((1 - (x % 2)) * 4)); - break; - case 8: - ib = (byte) greyVal; - break; - default: - throw new UnsupportedOperationException( - "Unsupported bits per pixel: " + bitsPerPixel); - } - - if ((x % pixelsPerByte) == (pixelsPerByte - 1) - || ((x + 1) == width)) { - bw[(y * bytewidth) + (x / pixelsPerByte)] = ib; - ib = 0; - } - } - } - io.setImageIDESize((byte) bitsPerPixel); - io.setImageData(bw); - } - - /** - * Helper method to create an image on the current container and to return + * Helper method to create a new resource object in the current container and to return * the object. - * @param params the set of image object parameters - * @return a newly created image object + * @return a newly created resource object */ - private ImageObject createImage(ImageObjectParameters params) { - String name = IMAGE_NAME_PREFIX - + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); - ImageObject imageObj = new ImageObject(name); - if (params.hasCompression()) { - int compression = params.getCompression(); - switch (compression) { - case TIFFImage.COMP_FAX_G3_1D: - imageObj.setImageEncoding(ImageContent.COMPID_G3_MH); - break; - case TIFFImage.COMP_FAX_G3_2D: - imageObj.setImageEncoding(ImageContent.COMPID_G3_MR); - break; - case TIFFImage.COMP_FAX_G4_2D: - imageObj.setImageEncoding(ImageContent.COMPID_G3_MMR); - break; - default: - throw new IllegalStateException( - "Invalid compression scheme: " + compression); - } - } - imageObj.setFullyQualifiedName( - FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, - FullyQualifiedNameTriplet.FORMAT_URL, params.getUri()); - imageObj.setImageParameters(params.getWidthRes(), params.getHeightRes(), - params.getImageDataWidth(), params.getImageDataHeight()); - if (params.isColor()) { - imageObj.setImageIDESize((byte)24); - imageObj.setImageData(params.getData()); - } else { - convertToGrayScaleImage(imageObj, params.getData(), - params.getImageDataWidth(), params.getImageDataHeight(), - params.getBitsPerPixel()); - } - return imageObj; + private ResourceObject createResourceObject() { + String name = RESOURCE_NAME_PREFIX + + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); + ResourceObject resource = new ResourceObject(name); + return resource; } - /** - * Helper method to create a graphic in the current container and to return - * the object. - * @param params the data object parameters - * @return a newly created graphics object - */ - private GraphicsObject createGraphic(DataObjectParameters params) { - String name = GRAPHIC_NAME_PREFIX - + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); - GraphicsObject graphicsObj = new GraphicsObject(name); - return graphicsObj; - } +// /** +// * Helper method to create a new resource object in the current container and to return +// * the object. +// * @return a newly created resource object +// */ +// private IncludeObject createIncludeObject() { +// String name = INCLUDE_NAME_PREFIX +// + StringUtils.lpad(String.valueOf(getResourceCount() + 1), '0', 5); +// IncludeObject includeObj = new IncludeObject(name); +// return includeObj; +// } /** - * Adds a data object to this resource group - * @param params the data object parameters + * Creates a data object in this resource group + * @param info the data object info * @return an include object reference */ - public IncludeObject addObject(DataObjectParameters params) { - ResourceObject resourceObj = (ResourceObject)getResourceMap().get(params.getUri()); - if (resourceObj == null) { + public IncludeObject createObject(DataObjectInfo info) { + IncludeObject includeObj = (IncludeObject)getResourceMap().get(info.getUri()); + if (includeObj == null) { AbstractDataObject dataObj; - if (params instanceof ImageObjectParameters) { - dataObj = createImage((ImageObjectParameters)params); + if (info instanceof ImageObjectInfo) { + dataObj = container.createImage((ImageObjectInfo)info); + } else { + dataObj = container.createGraphic(); + } + + ResourceInfo resourceInfo = info.getResourceInfo(); + String resourceName = resourceInfo.getName(); + if (resourceInfo.isExternal()) { + ObjectContainer objectContainer = new ObjectContainer(resourceName, dataObj, info); } else { - dataObj = createGraphic(params); + } + + // Wrap the data object in a page segment + PageSegment pageSegment = container.createPageSegment(); + pageSegment.addObject(dataObj); + + // TODO: AC - rotation? int rotation = 0; - dataObj.setViewport(params.getX(), params.getY(), - params.getWidth(), params.getHeight(), - params.getWidthRes(), params.getHeightRes(), rotation); + dataObj.setViewport(info.getX(), info.getY(), + info.getWidth(), info.getHeight(), + info.getWidthRes(), info.getHeightRes(), rotation); - // Wrap the data object in a resource object - resourceObj = new ResourceObject(dataObj.getName(), dataObj); - getResourceMap().put(params.getUri(), resourceObj); + includeObj.setResourceInfo(resourceInfo); + + // If resource is to be stored externally, + // wrap the page segment in a resource object + if (resourceInfo.isExternal()) { + ResourceObject resource = createResourceObject(); + resource.setReferencedObject(pageSegment); + includeObj = new IncludeObject(resource); + } else { + includeObj = new IncludeObject(pageSegment); + } + // Add include object to resource map + getResourceMap().put(info.getUri(), includeObj); } - IncludeObject includeObj = new IncludeObject(resourceObj); //includeObj.setObjectAreaSize(params.getX(), params.getY()); return includeObj; } @@ -218,7 +154,7 @@ public final class ResourceGroup extends AbstractNamedAFPObject { */ public int getResourceCount() { if (resourceMap != null) { - return resourceMap.size(); + return resourceMap.size(); } return 0; } @@ -250,7 +186,13 @@ public final class ResourceGroup extends AbstractNamedAFPObject { */ public void writeContent(OutputStream os) throws IOException { if (resourceMap != null) { - super.writeObjects(resourceMap.values(), os); + Collection includes = resourceMap.values(); + Iterator it = includes.iterator(); + while (it.hasNext()) { + IncludeObject includeObj = (IncludeObject)it.next(); + AbstractStructuredAFPObject obj = includeObj.getReferencedObject(); + obj.writeDataStream(os); + } } } diff --git a/src/java/org/apache/fop/render/afp/modca/ResourceObject.java b/src/java/org/apache/fop/render/afp/modca/ResourceObject.java index ff660a208..c0d4d88df 100644 --- a/src/java/org/apache/fop/render/afp/modca/ResourceObject.java +++ b/src/java/org/apache/fop/render/afp/modca/ResourceObject.java @@ -22,6 +22,7 @@ package org.apache.fop.render.afp.modca; import java.io.IOException; import java.io.OutputStream; +import org.apache.fop.render.afp.ResourceInfo; import org.apache.fop.render.afp.modca.triplets.Triplet; import org.apache.fop.render.afp.tools.BinaryUtils; @@ -48,19 +49,37 @@ public class ResourceObject extends AbstractPreparedAFPObject { private static final byte FORMDEF_OBJECT = (byte) 0xFE; /** - * the resource object + * the referenced data object */ - private AbstractNamedAFPObject resourceObj = null; - + private AbstractNamedAFPObject dataObj = null; + /** * Default constructor * * @param name the name of this resource (reference id) - * @param resourceObj the resource object to be added + * @param dataObj the resource object to be added */ - public ResourceObject(String name, AbstractNamedAFPObject resourceObj) { + public ResourceObject(String name) { super(name); - this.resourceObj = resourceObj; + } + + /** + * Sets the data object referenced by this resource object + * @param dataObj the data object + */ + public void setReferencedObject(AbstractNamedAFPObject dataObj) { + this.dataObj = dataObj; + setResourceObjectType(dataObj); + } + + /** + * @return the resource object contained in this envelope + */ + public AbstractNamedAFPObject getReferencedObject() { + return this.dataObj; + } + + private void setResourceObjectType(AbstractNamedAFPObject resourceObj) { byte type; if (resourceObj instanceof ImageObject) { type = IMAGE_OBJECT; @@ -76,19 +95,8 @@ public class ResourceObject extends AbstractPreparedAFPObject { throw new UnsupportedOperationException( "Unsupported resource object type " + resourceObj); } - setResourceObjectType(type); - } - - private void setResourceObjectType(byte type) { getTriplets().add(new ResourceObjectTypeTriplet(type)); } - - /** - * @return the resource object contained in this envelope - */ - public AbstractNamedAFPObject getResource() { - return this.resourceObj; - } /** * {@inheritDoc} @@ -127,8 +135,8 @@ public class ResourceObject extends AbstractPreparedAFPObject { */ protected void writeContent(OutputStream os) throws IOException { super.writeContent(os); // write triplets - if (resourceObj != null) { - resourceObj.writeDataStream(os); + if (dataObj != null) { + dataObj.writeDataStream(os); } } @@ -158,6 +166,13 @@ public class ResourceObject extends AbstractPreparedAFPObject { os.write(data); } + /** + * {@inheritDoc} + */ + public String toString() { + return this.getName(); + } + private class ResourceObjectTypeTriplet extends Triplet { private static final byte RESOURCE_OBJECT = 0x21; diff --git a/src/java/org/apache/fop/render/afp/modca/triplets/FullyQualifiedNameTriplet.java b/src/java/org/apache/fop/render/afp/modca/triplets/FullyQualifiedNameTriplet.java index 23c0d7e6b..e1a36b4da 100644 --- a/src/java/org/apache/fop/render/afp/modca/triplets/FullyQualifiedNameTriplet.java +++ b/src/java/org/apache/fop/render/afp/modca/triplets/FullyQualifiedNameTriplet.java @@ -37,6 +37,10 @@ /* $Id: $ */ package org.apache.fop.render.afp.modca.triplets; +import java.io.UnsupportedEncodingException; + +import org.apache.fop.render.afp.modca.AFPConstants; + /** * A Fully Qualified Name triplet enable the identification and referencing of @@ -75,28 +79,39 @@ public class FullyQualifiedNameTriplet extends Triplet { public static final byte FORMAT_OID = (byte)0x10; public static final byte FORMAT_URL = (byte)0x20; - private byte[] fqName; + private byte[] nameBytes; /** * @return the actual fully qualified name of this triplet */ public byte[] getFullyQualifiedName() { - return fqName; + return nameBytes; } /** * Main constructor - * @param fqnType the fully qualified name type - * @param fqnFmt the fully qualified name format - * @param fqName the fully qualified name + * @param type the fully qualified name type + * @param format the fully qualified name format + * @param name the fully qualified name */ - public FullyQualifiedNameTriplet(byte fqnType, byte fqnFmt, byte[] fqName) { + public FullyQualifiedNameTriplet(byte type, byte format, byte[] name) { super(FULLY_QUALIFIED_NAME); - this.fqName = fqName; - super.data = new byte[2 + fqName.length]; - data[0] = fqnType; - data[1] = fqnFmt; + this.nameBytes = name; + super.data = new byte[2 + name.length]; + data[0] = type; + data[1] = format; // FQName - System.arraycopy(fqName, 0, data, 2, fqName.length); + System.arraycopy(name, 0, data, 2, name.length); + } + + /** + * {@inheritDoc} + */ + public String toString() { + try { + return new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); + } catch (UnsupportedEncodingException e) { + } + return null; } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/modca/triplets/ObjectClassificationTriplet.java b/src/java/org/apache/fop/render/afp/modca/triplets/ObjectClassificationTriplet.java index 4d5f41359..28c3dca4e 100644 --- a/src/java/org/apache/fop/render/afp/modca/triplets/ObjectClassificationTriplet.java +++ b/src/java/org/apache/fop/render/afp/modca/triplets/ObjectClassificationTriplet.java @@ -19,52 +19,83 @@ package org.apache.fop.render.afp.modca.triplets; -import org.apache.fop.render.afp.modca.Registry; +import org.apache.fop.render.afp.modca.ObjectTypeRegistry; /** * The Object Classification is used to classify and identify object data. * The object data may or may not be defined by an IBM presentation architecture */ -public class ObjectClassificationTriplet extends Triplet { +public class ObjectClassificationTriplet extends Triplet { + + /** + * The scope of this object is the including page or overlay + */ public static final byte CLASS_TIME_INVARIANT_PAGINATED_PRESENTATION_OBJECT = 0x01; + + /** + * The scope of this object is not defined + */ public static final byte CLASS_TIME_VARIANT_PRESENTATION_OBJECT = 0x10; + + /** + * This is not a presentation object, the scope of this object is not defined + */ public static final byte CLASS_EXECUTABLE_PROGRAM = 0x20; + + /** + * Setup information file, document level. This is not a presentation object, + */ public static final byte CLASS_SETUP_FILE = 0x30; + + /** + * This is a resource used by a presentation object that may itself be a resource. + * The scope of the resource is the object that uses the resource. + */ public static final byte CLASS_SECONDARY_RESOURCE = 0x40; + + /** + * Data object font. This is a non-FOCA font resource used to present + * text in a data object. The scope of the resource is the object that + * uses the resource. + */ public static final byte CLASS_DATA_OBJECT_FONT = 0x41; - + /** * Main constructor * * @param objectClass * the object class type - * @param componentId - * the object componentId + * @param entry + * the object type registry entry + * @param strucFlgs + * the structured flags pertaining to this object classification triplet */ - public ObjectClassificationTriplet(byte objectClass, byte componentId) { + public ObjectClassificationTriplet(byte objectClass, ObjectTypeRegistry.ObjectType entry, + StrucFlgs strucFlgs) { super(OBJECT_CLASSIFICATION); byte[] data = new byte[93]; data[0] = 0x00; // reserved (must be zero) data[1] = objectClass; // ObjClass data[2] = 0x00; // reserved (must be zero) data[3] = 0x00; // reserved (must be zero) - data[4] = 0x00; // StrucFlgs - Information on the structure of the object container + // StrucFlgs - Information on the structure of the object container + data[4] = strucFlgs.getValue(); data[5] = 0x00; // StrucFlgs - Registry.Entry entry = Registry.getInstance().getEntry(componentId); - if (entry == null) { - throw new UnsupportedOperationException("unknown registry entry " + componentId); + throw new UnsupportedOperationException("Unknown registry entry"); } + + byte[] oid = entry.getOID(); // RegObjId - MOD:CA-registered ASN.1 OID for object type (8-23) - System.arraycopy(entry.getOID(), 0, data, 6, entry.getOID().length); + System.arraycopy(oid, 0, data, 6, oid.length); + byte[] objectTypeName = entry.getName(); // ObjTpName - name of object type (24-55) - System.arraycopy(entry.getObjectTypeName(), 0, data, 22, - entry.getObjectTypeName().length); + System.arraycopy(objectTypeName, 0, data, 22, objectTypeName.length); // ObjLev (not specified) - Release level or version number of object type (56-63) // CompName (not specified) - Name of company or org that owns object definition (64-95) - } + } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/render/afp/modca/triplets/StrucFlgs.java b/src/java/org/apache/fop/render/afp/modca/triplets/StrucFlgs.java new file mode 100644 index 000000000..87601c435 --- /dev/null +++ b/src/java/org/apache/fop/render/afp/modca/triplets/StrucFlgs.java @@ -0,0 +1,80 @@ +/* + * 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.render.afp.modca.triplets; + +/** + * Used by ObjectClassificationTriplet to provide + * information on the structure of the object and its container + */ +public class StrucFlgs { + private static final int OBJECT_DATA_NOT_CARRIED_IN_OBJECT_CONTAINER = 1; + private static final int OBJECT_DATA_OBJECT_CONTAINER_STRUCTURE_UNKNOWN = 2; + private static final int OBJECT_DATA_CARRIED_IN_OBJECT_CONTAINER = 3; + + private static final int OBJECT_CONTAINER_NOT_INCLUDE_OBJECT_ENVIRONMENT_GROUP = 4; + private static final int OBJECT_CONTAINER_OBJECT_ENVIRONMENT_GROUP_CONTAINMENT_UNKNOWN = 8; + private static final int OBJECT_CONTAINER_INCLUDES_OBJECT_ENVIRONMENT_GROUP = 12; + + private static final int OBJECT_CONTAINER_DATA_NOT_CARRIED_IN_OBJECT_DATA = 16; + private static final int OBJECT_CONTAINER_DATA_OBJECT_DATA_CONTAINMENT_UNKNOWN = 32; + private static final int OBJECT_CONTAINER_DATA_CARRIES_OBJECT_DATA = 48; + + private byte value = 0; + + /** + * Main constructor + * @param dataInContainer true if the object data in carried in the object container + * @param containerHasOEG true if the object container has an object environment group + * @param dataInOCD true if the object container data carries the object data + */ + public StrucFlgs(boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { + if (dataInContainer) { + this.value += OBJECT_DATA_CARRIED_IN_OBJECT_CONTAINER; + } else { + this.value += OBJECT_DATA_NOT_CARRIED_IN_OBJECT_CONTAINER; + } + if (containerHasOEG) { + this.value += OBJECT_CONTAINER_INCLUDES_OBJECT_ENVIRONMENT_GROUP; + } else { + this.value += OBJECT_CONTAINER_NOT_INCLUDE_OBJECT_ENVIRONMENT_GROUP; + } + if (dataInOCD) { + this.value += OBJECT_CONTAINER_DATA_CARRIES_OBJECT_DATA; + } else { + this.value += OBJECT_CONTAINER_DATA_NOT_CARRIED_IN_OBJECT_DATA; + } + } + + /** + * Default constructor + */ + public StrucFlgs() { + this.value = OBJECT_DATA_OBJECT_CONTAINER_STRUCTURE_UNKNOWN + + OBJECT_CONTAINER_OBJECT_ENVIRONMENT_GROUP_CONTAINMENT_UNKNOWN + + OBJECT_CONTAINER_DATA_OBJECT_DATA_CONTAINMENT_UNKNOWN; + } + + /** + * @return the value of structure flags value + */ + public byte getValue() { + return this.value; + } +}
\ No newline at end of file |