diff options
author | Adrian Cumiskey <acumiskey@apache.org> | 2008-11-17 15:26:06 +0000 |
---|---|---|
committer | Adrian Cumiskey <acumiskey@apache.org> | 2008-11-17 15:26:06 +0000 |
commit | 016ddc8aa591fde0adcaef76158f3f5ebcf6590a (patch) | |
tree | bbba62706cdaf1c15113b0c1bd309a84e476e36f /src/java/org/apache/fop/afp/modca | |
parent | 3bc890586dede254dad93f506d12e68b1533047c (diff) | |
download | xmlgraphics-fop-016ddc8aa591fde0adcaef76158f3f5ebcf6590a.tar.gz xmlgraphics-fop-016ddc8aa591fde0adcaef76158f3f5ebcf6590a.zip |
Memory usage optimizations.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@718262 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/afp/modca')
32 files changed, 1023 insertions, 933 deletions
diff --git a/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java index 9e1e107f3..f34ac7d00 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java @@ -20,6 +20,7 @@ package org.apache.fop.afp.modca; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.Collection; import java.util.Iterator; @@ -27,6 +28,7 @@ import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.afp.Streamable; +import org.apache.fop.afp.util.BinaryUtils; /** * This is the base class for all data stream objects. Page objects are @@ -100,6 +102,81 @@ public abstract class AbstractAFPObject implements Streamable { } } + /** + * Reads data chunks from an inputstream + * and then formats them with a structured header to a given outputstream + * + * @param dataHeader the header data + * @param lengthOffset offset of length field in data chunk + * @param maxChunkLength the maximum chunk length + * @param inputStream the inputstream to read from + * @param outputStream the outputstream to write to + * @throws IOException thrown if an I/O exception of some sort has occurred. + */ + protected static void copyChunks(byte[] dataHeader, int lengthOffset, + int maxChunkLength, InputStream inputStream, OutputStream outputStream) + throws IOException { + int headerLen = dataHeader.length - lengthOffset; + // length field is just before data so do not include in data length + if (headerLen == 2) { + headerLen = 0; + } + byte[] data = new byte[maxChunkLength]; + int numBytesRead = 0; + while ((numBytesRead = inputStream.read(data, 0, maxChunkLength)) > 0) { + byte[] len = BinaryUtils.convert(headerLen + numBytesRead, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + outputStream.write(dataHeader); + outputStream.write(data, 0, numBytesRead); + } + } + + /** + * Writes data chunks to a given outputstream + * + * @param data the data byte array + * @param dataHeader the header data + * @param lengthOffset offset of length field in data chunk + * @param maxChunkLength the maximum chunk length + * @param os the outputstream to write to + * @throws IOException thrown if an I/O exception of some sort has occurred. + */ + protected static void writeChunksToStream(byte[] data, byte[] dataHeader, + int lengthOffset, int maxChunkLength, OutputStream os) throws IOException { + int dataLength = data.length; + int numFullChunks = dataLength / maxChunkLength; + int lastChunkLength = dataLength % maxChunkLength; + + int headerLen = dataHeader.length - lengthOffset; + // length field is just before data so do not include in data length + if (headerLen == 2) { + headerLen = 0; + } + + byte[] len; + int off = 0; + if (numFullChunks > 0) { + // write out full data chunks + len = BinaryUtils.convert(headerLen + maxChunkLength, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + for (int i = 0; i < numFullChunks; i++, off += maxChunkLength) { + os.write(dataHeader); + os.write(data, off, maxChunkLength); + } + } + + if (lastChunkLength > 0) { + // write last data chunk + len = BinaryUtils.convert(headerLen + lastChunkLength, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + os.write(dataHeader); + os.write(data, off, lastChunkLength); + } + } + /** structured field type codes */ public interface Type { diff --git a/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java b/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java index f3734cfb7..c471794e9 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java +++ b/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java @@ -22,7 +22,7 @@ package org.apache.fop.afp.modca; /** * Base class for AFP descriptor objects */ -public abstract class AbstractDescriptor extends AbstractStructuredAFPObject { +public abstract class AbstractDescriptor extends AbstractTripletStructuredObject { /** width of this descriptor */ protected int width = 0; /** height of this descriptor */ diff --git a/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java index de02f13d3..c3c158825 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java @@ -27,7 +27,7 @@ import org.apache.fop.afp.AFPConstants; * This is the base class for all named data stream objects. * A named data stream object has an 8 byte EBCIDIC name. */ -public abstract class AbstractNamedAFPObject extends AbstractStructuredAFPObject { +public abstract class AbstractNamedAFPObject extends AbstractTripletStructuredObject { private static final int DEFAULT_NAME_LENGTH = 8; diff --git a/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java deleted file mode 100644 index 10a516318..000000000 --- a/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java +++ /dev/null @@ -1,96 +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.afp.modca; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * A base class that carries out early preparation of structured field data - * for the AFP object (so the data length can be pre-calculated) - */ -public abstract class AbstractPreparedAFPObject extends AbstractNamedAFPObject -implements PreparedAFPObject { - - /** structured field data to be written */ - protected byte[] data = null; - - /** - * Default constructor - */ - public AbstractPreparedAFPObject() { - } - - /** - * Named constructor - * - * @param name the name of this AFP object - */ - public AbstractPreparedAFPObject(String name) { - super(name); - } - - /** {@inheritDoc} */ - protected void writeContent(OutputStream os) throws IOException { - super.writeContent(os); // write triplets - if (this.data != null) { - os.write(this.data); - } - } - - /** - * Return the start data length of this structured field - * - * @return the start data length of this structured field - */ - protected int getStartDataLength() { - return 0; - } - - /** - * Return the data length of the structured field data of this AFP object - * - * @return the data length of the structured field data of this AFP object - */ - public int getDataLength() { - if (this.data != null) { - return this.data.length; - } - return 0; - } - - /** - * Return the structured field length - * - * @return the structured field length - */ - protected int getLength() { - return getStartDataLength() + getTripletDataLength() + getDataLength(); - } - - /** - * Sets the data - * - * @param data the data - */ - protected void setData(byte[] data) { - this.data = data; - } -}
\ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java b/src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java deleted file mode 100644 index 9019a6acc..000000000 --- a/src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java +++ /dev/null @@ -1,84 +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.afp.modca; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.Iterator; -import java.util.List; - -/** - * A base container of prepared structured AFP objects - */ -public abstract class AbstractPreparedObjectContainer extends AbstractNamedAFPObject -implements PreparedAFPObject { - - /** list of objects contained within this container */ - protected List/*<PreparedAFPObject>*/ objects - = new java.util.ArrayList/*<PreparedAFPObject>*/(); - - /** - * Default constructor - */ - protected AbstractPreparedObjectContainer() { - } - - /** - * Named constructor - * - * @param name the name of the container - */ - protected AbstractPreparedObjectContainer(String name) { - super(name); - } - - /** {@inheritDoc} */ - protected void writeContent(OutputStream os) throws IOException { - writeObjects(objects, os); - } - - /** - * Adds a given prepared object to this container - * - * @param preparedObject the prepared object - */ - public void addObject(PreparedAFPObject preparedObject) { - objects.add(preparedObject); - } - - /** - * Returns the current data length - * - * @return the current data length of this container including - * all enclosed objects (and their containers) - */ - public int getDataLength() { - int dataLen = 0; - Iterator it = objects.iterator(); - while (it.hasNext()) { - Object obj = it.next(); - if (obj instanceof PreparedAFPObject) { - PreparedAFPObject prepObj = (PreparedAFPObject)obj; - dataLen += prepObj.getDataLength(); - } - } - return dataLen; - } -} diff --git a/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java deleted file mode 100644 index 9bfc11095..000000000 --- a/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java +++ /dev/null @@ -1,324 +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.afp.modca; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.apache.commons.io.output.ByteArrayOutputStream; -import org.apache.fop.afp.modca.Registry.ObjectType; -import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet; -import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet; -import org.apache.fop.afp.modca.triplets.Triplet; -import org.apache.fop.afp.util.BinaryUtils; - -/** - * An abstract class encapsulating an MODCA structured object - */ -public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { - /** - * list of object triplets - */ - protected List/*<Triplet>*/ triplets = null; - - /** - * triplet data created from triplet list - */ - protected byte[] tripletData = null; - - /** - * Default constructor - */ - protected AbstractStructuredAFPObject() { - } - - /** - * Returns the triplet data length - * - * @return the triplet data length - */ - protected int getTripletDataLength() { - if (tripletData == null) { - try { - getTripletData(); - } catch (IOException e) { - log.error("failed to get triplet data"); - } - } - if (tripletData != null) { - return tripletData.length; - } - return 0; - } - - /** - * Returns the triplet data - * - * @return the triplet data - * @throws IOException throws an I/O exception if one occurred - */ - protected byte[] getTripletData() throws IOException { - if (tripletData == null && triplets != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - writeObjects(triplets, baos); - this.tripletData = baos.toByteArray(); - triplets = null; // gc - } - return this.tripletData; - } - - /** - * Writes any triplet data - * - * @param os The stream to write to - * @throws IOException The stream to write to - */ - protected void writeTriplets(OutputStream os) throws IOException { - if (tripletData != null) { - os.write(tripletData); - } else if (triplets != null) { - writeObjects(triplets, os); - triplets = null; // gc - } - } - - /** - * 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 end of the Object. - * - * @param os The stream to write to - * @throws IOException an I/O exception if one occurred - */ - protected void writeEnd(OutputStream os) throws IOException { - } - - /** - * 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); - } - - /** {@inheritDoc} */ - public void writeToStream(OutputStream os) throws IOException { - writeStart(os); - writeContent(os); - writeEnd(os); - } - - /** - * Returns the first matching triplet found in the structured field triplet list - * - * @param tripletId the triplet identifier - */ - private Triplet getTriplet(byte tripletId) { - Iterator it = getTriplets().iterator(); - while (it.hasNext()) { - Triplet triplet = (Triplet)it.next(); - if (triplet.getId() == tripletId) { - return triplet; - } - } - return null; - } - - /** - * Returns true of this structured field has the given triplet - * - * @param tripletId the triplet identifier - * @return true if the structured field has the given triplet - */ - public boolean hasTriplet(byte tripletId) { - return getTriplet(tripletId) != null; - } - - /** - * Adds a triplet to this structured object - * - * @param triplet the triplet to add - */ - protected void addTriplet(Triplet triplet) { - getTriplets().add(triplet); - } - - /** - * Adds a list of triplets to the triplets contained within this structured field - * - * @param tripletCollection a collection of triplets - */ - public void addTriplets(Collection/*<Triplet>*/ tripletCollection) { - if (tripletCollection != null) { - getTriplets().addAll(tripletCollection); - } - } - - /** @return the triplet list pertaining to this resource */ - protected List/*<Triplet>*/ getTriplets() { - if (triplets == null) { - triplets = new java.util.ArrayList(); - } - return triplets; - } - - /** - * Sets the fully qualified name of this resource - * - * @param fqnType the fully qualified name type of this resource - * @param fqnFormat the fully qualified name format of this resource - * @param fqName the fully qualified name of this resource - */ - public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) { - addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName)); - } - - /** @return the fully qualified name of this triplet or null if it does not exist */ - public String getFullyQualifiedName() { - FullyQualifiedNameTriplet fqNameTriplet - = (FullyQualifiedNameTriplet)getTriplet(Triplet.FULLY_QUALIFIED_NAME); - if (fqNameTriplet != null) { - return fqNameTriplet.getFullyQualifiedName(); - } - log.warn(this + " has no fully qualified name"); - return null; - } - - /** - * Sets the objects classification - * - * @param objectClass the classification of the object - * @param objectType the MOD:CA registry object type entry for the given - * object/component type of the object - * @param dataInContainer whether the data resides in the container - * @param containerHasOEG whether the container has an object environment group - * @param dataInOCD whether the data resides in a object container data structured field - */ - public void setObjectClassification( - byte objectClass, ObjectType objectType, - boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { - addTriplet( - new ObjectClassificationTriplet( - objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD)); - } - - /** - * Sets a comment on this resource - * - * @param comment a comment string - */ - public void setComment(String comment) { - try { - addTriplet(new Triplet(Triplet.COMMENT, comment)); - } catch (UnsupportedEncodingException e) { - log.error(e.getMessage()); - } - } - - /** - * Reads data chunks from an inputstream - * and then formats them with a structured header to a given outputstream - * - * @param dataHeader the header data - * @param lengthOffset offset of length field in data chunk - * @param maxChunkLength the maximum chunk length - * @param inputStream the inputstream to read from - * @param outputStream the outputstream to write to - * @throws IOException thrown if an I/O exception of some sort has occurred. - */ - protected static void copyChunks(byte[] dataHeader, int lengthOffset, - int maxChunkLength, InputStream inputStream, OutputStream outputStream) - throws IOException { - int headerLen = dataHeader.length - lengthOffset; - // length field is just before data so do not include in data length - if (headerLen == 2) { - headerLen = 0; - } - byte[] data = new byte[maxChunkLength]; - int numBytesRead = 0; - while ((numBytesRead = inputStream.read(data, 0, maxChunkLength)) > 0) { - byte[] len = BinaryUtils.convert(headerLen + numBytesRead, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - outputStream.write(dataHeader); - outputStream.write(data, 0, numBytesRead); - } - } - - /** - * Writes data chunks to a given outputstream - * - * @param data the data byte array - * @param dataHeader the header data - * @param lengthOffset offset of length field in data chunk - * @param maxChunkLength the maximum chunk length - * @param os the outputstream to write to - * @throws IOException thrown if an I/O exception of some sort has occurred. - */ - protected static void writeChunksToStream(byte[] data, byte[] dataHeader, - int lengthOffset, int maxChunkLength, OutputStream os) throws IOException { - int dataLength = data.length; - int numFullChunks = dataLength / maxChunkLength; - int lastChunkLength = dataLength % maxChunkLength; - - int headerLen = dataHeader.length - lengthOffset; - // length field is just before data so do not include in data length - if (headerLen == 2) { - headerLen = 0; - } - - byte[] len; - int off = 0; - if (numFullChunks > 0) { - // write out full data chunks - len = BinaryUtils.convert(headerLen + maxChunkLength, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - for (int i = 0; i < numFullChunks; i++, off += maxChunkLength) { - os.write(dataHeader); - os.write(data, off, maxChunkLength); - } - } - - if (lastChunkLength > 0) { - // write last data chunk - len = BinaryUtils.convert(headerLen + lastChunkLength, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - os.write(dataHeader); - os.write(data, off, lastChunkLength); - } - } -} diff --git a/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java b/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java new file mode 100644 index 000000000..e848583b5 --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java @@ -0,0 +1,69 @@ +/* + * 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.afp.modca; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * An abstract class encapsulating an MODCA structured object + */ +public abstract class AbstractStructuredObject extends AbstractAFPObject { + + /** + * Default constructor + */ + protected AbstractStructuredObject() { + } + + /** + * 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 { + } + + /** + * Helper method to write the end of the Object. + * + * @param os The stream to write to + * @throws IOException an I/O exception if one occurred + */ + protected void writeEnd(OutputStream os) throws IOException { + } + + /** + * 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 { + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + writeStart(os); + writeContent(os); + writeEnd(os); + } +} diff --git a/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java b/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java new file mode 100644 index 000000000..c1686d07c --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java @@ -0,0 +1,178 @@ +/* + * 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.afp.modca; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.apache.fop.afp.modca.Registry.ObjectType; +import org.apache.fop.afp.modca.triplets.AbstractTriplet; +import org.apache.fop.afp.modca.triplets.CommentTriplet; +import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet; +import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet; + +public class AbstractTripletStructuredObject extends AbstractStructuredObject { + + /** list of object triplets */ + protected List/*<AbstractTriplet>*/ triplets = new java.util.ArrayList/*<AbstractTriplet>*/(); + + /** + * Returns the triplet data length + * + * @return the triplet data length + */ + protected int getTripletDataLength() { + int dataLength = 0; + if (hasTriplets()) { + Iterator it = triplets.iterator(); + while (it.hasNext()) { + AbstractTriplet triplet = (AbstractTriplet)it.next(); + dataLength += triplet.getDataLength(); + } + } + return dataLength; + } + + /** + * Returns true when this structured field contains triplets + * + * @return true when this structured field contains triplets + */ + public boolean hasTriplets() { + return triplets.size() > 0; + } + + /** + * Writes any triplet data + * + * @param os The stream to write to + * @throws IOException The stream to write to + */ + protected void writeTriplets(OutputStream os) throws IOException { + if (hasTriplets()) { + writeObjects(triplets, os); + triplets = null; // gc + } + } + + /** + * Returns the first matching triplet found in the structured field triplet list + * + * @param tripletId the triplet identifier + */ + private AbstractTriplet getTriplet(byte tripletId) { + Iterator it = getTriplets().iterator(); + while (it.hasNext()) { + AbstractTriplet triplet = (AbstractTriplet)it.next(); + if (triplet.getId() == tripletId) { + return triplet; + } + } + return null; + } + + /** + * Returns true of this structured field has the given triplet + * + * @param tripletId the triplet identifier + * @return true if the structured field has the given triplet + */ + public boolean hasTriplet(byte tripletId) { + return getTriplet(tripletId) != null; + } + + /** + * Adds a triplet to this structured object + * + * @param triplet the triplet to add + */ + protected void addTriplet(AbstractTriplet triplet) { + triplets.add(triplet); + } + + /** + * Adds a list of triplets to the triplets contained within this structured field + * + * @param tripletCollection a collection of triplets + */ + public void addTriplets(Collection/*<Triplet>*/ tripletCollection) { + if (tripletCollection != null) { + triplets.addAll(tripletCollection); + } + } + + /** @return the triplet list pertaining to this resource */ + protected List/*<Triplet>*/ getTriplets() { + return triplets; + } + + /** + * Sets the fully qualified name of this resource + * + * @param fqnType the fully qualified name type of this resource + * @param fqnFormat the fully qualified name format of this resource + * @param fqName the fully qualified name of this resource + */ + public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) { + addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName)); + } + + /** @return the fully qualified name of this triplet or null if it does not exist */ + public String getFullyQualifiedName() { + FullyQualifiedNameTriplet fqNameTriplet + = (FullyQualifiedNameTriplet)getTriplet(AbstractTriplet.FULLY_QUALIFIED_NAME); + if (fqNameTriplet != null) { + return fqNameTriplet.getFullyQualifiedName(); + } + log.warn(this + " has no fully qualified name"); + return null; + } + + /** + * Sets the objects classification + * + * @param objectClass the classification of the object + * @param objectType the MOD:CA registry object type entry for the given + * object/component type of the object + * @param dataInContainer whether the data resides in the container + * @param containerHasOEG whether the container has an object environment group + * @param dataInOCD whether the data resides in a object container data structured field + */ + public void setObjectClassification( + byte objectClass, ObjectType objectType, + boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { + addTriplet( + new ObjectClassificationTriplet( + objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD)); + } + + /** + * Sets a comment on this resource + * + * @param commentString a comment string + */ + public void setComment(String commentString) { + addTriplet(new CommentTriplet(AbstractTriplet.COMMENT, commentString)); + } + +} diff --git a/src/java/org/apache/fop/afp/modca/GraphicsObject.java b/src/java/org/apache/fop/afp/modca/GraphicsObject.java index 3848ca4c3..da6079d33 100644 --- a/src/java/org/apache/fop/afp/modca/GraphicsObject.java +++ b/src/java/org/apache/fop/afp/modca/GraphicsObject.java @@ -51,11 +51,11 @@ import org.apache.fop.afp.goca.GraphicsString; public class GraphicsObject extends AbstractDataObject { /** The graphics data */ - private GraphicsData currentGraphicsData = null; + private GraphicsData data = null; /** list of objects contained within this container */ - protected List/*<PreparedAFPObject>*/ objects - = new java.util.ArrayList/*<PreparedAFPObject>*/(); + protected List/*<GraphicsDrawingOrder>*/ objects + = new java.util.ArrayList/*<GraphicsDrawingOrder>*/(); /** * Default constructor @@ -85,13 +85,13 @@ public class GraphicsObject extends AbstractDataObject { } /** {@inheritDoc} */ - public void addObject(PreparedAFPObject drawingOrder) { - if (currentGraphicsData == null - || (currentGraphicsData.getDataLength() + drawingOrder.getDataLength()) + public void addObject(StructuredDataObject drawingOrder) { + if (data == null + || (data.getDataLength() + drawingOrder.getDataLength()) >= GraphicsData.MAX_DATA_LEN) { newData(); } - currentGraphicsData.addObject(drawingOrder); + data.addObject(drawingOrder); } /** @@ -100,10 +100,10 @@ public class GraphicsObject extends AbstractDataObject { * @return the current graphics data */ private GraphicsData getData() { - if (this.currentGraphicsData == null) { + if (this.data == null) { return newData(); } - return this.currentGraphicsData; + return this.data; } /** @@ -112,9 +112,9 @@ public class GraphicsObject extends AbstractDataObject { * @return a newly created graphics data */ private GraphicsData newData() { - this.currentGraphicsData = factory.createGraphicsData(); - objects.add(currentGraphicsData); - return currentGraphicsData; + this.data = factory.createGraphicsData(); + objects.add(data); + return data; } /** @@ -284,18 +284,18 @@ public class GraphicsObject extends AbstractDataObject { * Begins a graphics area (start of fill) */ public void beginArea() { - if (currentGraphicsData == null) { + if (data == null) { newData(); } - currentGraphicsData.beginArea(); + data.beginArea(); } /** * Ends a graphics area (end of fill) */ public void endArea() { - if (currentGraphicsData != null) { - currentGraphicsData.endArea(); + if (data != null) { + data.endArea(); } } diff --git a/src/java/org/apache/fop/afp/modca/IncludeObject.java b/src/java/org/apache/fop/afp/modca/IncludeObject.java index c0ab5c640..2dff6bd87 100644 --- a/src/java/org/apache/fop/afp/modca/IncludeObject.java +++ b/src/java/org/apache/fop/afp/modca/IncludeObject.java @@ -238,9 +238,7 @@ public class IncludeObject extends AbstractNamedAFPObject { os.write(data); // Write triplet for FQN internal/external object reference - if (tripletData != null) { - os.write(tripletData); - } + writeTriplets(os); } private String getObjectTypeName() { diff --git a/src/java/org/apache/fop/afp/modca/MapCodedFont.java b/src/java/org/apache/fop/afp/modca/MapCodedFont.java index 159aa200a..01e9abc6f 100644 --- a/src/java/org/apache/fop/afp/modca/MapCodedFont.java +++ b/src/java/org/apache/fop/afp/modca/MapCodedFont.java @@ -41,7 +41,7 @@ import org.apache.fop.afp.util.BinaryUtils; * the Map Coded Font structured field specifies a set of resource attributes * for the coded font. */ -public class MapCodedFont extends AbstractStructuredAFPObject { +public class MapCodedFont extends AbstractStructuredObject { /** * The collection of map coded fonts (maximum of 254) diff --git a/src/java/org/apache/fop/afp/modca/MapContainerData.java b/src/java/org/apache/fop/afp/modca/MapContainerData.java index dba7616a8..8411592aa 100644 --- a/src/java/org/apache/fop/afp/modca/MapContainerData.java +++ b/src/java/org/apache/fop/afp/modca/MapContainerData.java @@ -29,7 +29,7 @@ import org.apache.fop.afp.util.BinaryUtils; * The Map Container Data structured field specifies how a presentation data object * that is carried in an Object Container is mapped into its object area. */ -public class MapContainerData extends AbstractStructuredAFPObject { +public class MapContainerData extends AbstractTripletStructuredObject { /** * Main constructor @@ -45,13 +45,15 @@ public class MapContainerData extends AbstractStructuredAFPObject { byte[] data = new byte[11]; copySF(data, Type.MAP, Category.OBJECT_CONTAINER); int tripletLen = getTripletDataLength(); + byte[] len = BinaryUtils.convert(10 + tripletLen, 2); data[1] = len[0]; data[2] = len[1]; + len = BinaryUtils.convert(2 + tripletLen, 2); data[9] = len[0]; data[10] = len[1]; os.write(data); - os.write(tripletData); + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/MapDataResource.java b/src/java/org/apache/fop/afp/modca/MapDataResource.java index 7bebc4c07..566f60ce5 100644 --- a/src/java/org/apache/fop/afp/modca/MapDataResource.java +++ b/src/java/org/apache/fop/afp/modca/MapDataResource.java @@ -28,7 +28,7 @@ import org.apache.fop.afp.util.BinaryUtils; * The Map Data Resource structured field specifies resources that are * required for presentation. */ -public class MapDataResource extends AbstractStructuredAFPObject { +public class MapDataResource extends AbstractTripletStructuredObject { /** * Main constructor @@ -53,6 +53,6 @@ public class MapDataResource extends AbstractStructuredAFPObject { data[10] = len[1]; os.write(data); - os.write(tripletData); + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java b/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java index 21c21e39f..80d7f746f 100644 --- a/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java +++ b/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java @@ -22,11 +22,10 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.fop.afp.modca.triplets.DescriptorPositionTriplet; import org.apache.fop.afp.modca.triplets.MeasurementUnitsTriplet; import org.apache.fop.afp.modca.triplets.ObjectAreaSizeTriplet; import org.apache.fop.afp.modca.triplets.PresentationSpaceResetMixingTriplet; -import org.apache.fop.afp.modca.triplets.Triplet; import org.apache.fop.afp.util.BinaryUtils; /** @@ -48,40 +47,27 @@ public class ObjectAreaDescriptor extends AbstractDescriptor { super(width, height, widthRes, heightRes); } - /** {@inheritDoc} */ - protected byte[] getTripletData() throws IOException { - if (tripletData == null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - // Specifies the associated ObjectAreaPosition structured field - final byte oapId = 0x01; - Triplet triplet = new Triplet(Triplet.DESCRIPTOR_POSITION, oapId); - triplet.writeToStream(baos); - - triplet = new MeasurementUnitsTriplet(widthRes, heightRes); - triplet.writeToStream(baos); - - triplet = new ObjectAreaSizeTriplet(width, height); - triplet.writeToStream(baos); - - triplet = new PresentationSpaceResetMixingTriplet( - PresentationSpaceResetMixingTriplet.NOT_RESET); - triplet.writeToStream(baos); - - this.tripletData = baos.toByteArray(); - } - return this.tripletData; - } + private static final byte oapId = 0x01; /** {@inheritDoc} */ - public void writeStart(OutputStream os) throws IOException { - super.writeStart(os); + public void writeToStream(OutputStream os) throws IOException { + // add triplets + addTriplet(new DescriptorPositionTriplet(oapId)); + addTriplet(new MeasurementUnitsTriplet(widthRes, heightRes)); + addTriplet(new ObjectAreaSizeTriplet(width, height)); + addTriplet(new PresentationSpaceResetMixingTriplet( + PresentationSpaceResetMixingTriplet.NOT_RESET)); + byte[] data = new byte[9]; copySF(data, Type.DESCRIPTOR, Category.OBJECT_AREA); - byte[] len = BinaryUtils.convert(data.length + tripletData.length - 1, 2); + + int tripletDataLength = getTripletDataLength(); + byte[] len = BinaryUtils.convert(data.length + tripletDataLength, 2); data[1] = len[0]; // Length data[2] = len[1]; os.write(data); + + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java b/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java index b1f95e236..279b31201 100644 --- a/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java +++ b/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java @@ -5,9 +5,9 @@ * 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. @@ -27,24 +27,24 @@ import org.apache.fop.afp.util.BinaryUtils; /** * The Preprocess Presentation Object structured field specifies presentation - * parameters for a data object that has been mapped as a resource. + * parameters for a data object that has been mapped as a resource. */ -public class PreprocessPresentationObject extends AbstractStructuredAFPObject { +public class PreprocessPresentationObject extends AbstractTripletStructuredObject { private static final byte TYPE_OTHER = (byte)0x92; private static final byte TYPE_OVERLAY = (byte)0xDF; private static final byte TYPE_IMAGE = (byte)0xFB; - + private byte objType = TYPE_OTHER; private byte objOrent = 0; // object always processed at 0 degree orientation private int objXOffset = -1; private int objYOffset = -1; - + /** * Main constructor - * - * @param prePresObj the presentation object to be preprocessed + * + * @param prePresObj the presentation object to be preprocessed */ - public PreprocessPresentationObject(AbstractStructuredAFPObject prePresObj) { + public PreprocessPresentationObject(AbstractTripletStructuredObject prePresObj) { if (prePresObj instanceof ImageObject || prePresObj instanceof Overlay) { if (prePresObj instanceof ImageObject) { this.objType = TYPE_IMAGE; @@ -52,40 +52,40 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { this.objType = TYPE_OVERLAY; } setFullyQualifiedName( - FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, + FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, FullyQualifiedNameTriplet.FORMAT_CHARSTR, prePresObj.getFullyQualifiedName()); } else { this.objType = TYPE_OTHER; } } - + public static final byte ORIENTATION_ZERO_DEGREES = 1; public static final byte ORIENTATION_90_DEGREES = 2; public static final byte ORIENTATION_180_DEGREES = 4; public static final byte ORIENTATION_270_DEGREES = 8; - + /** * Sets the object orientations relative to media leading edge - * + * * @param orientation the object orientations relative to media leading edge */ public void setOrientation(byte orientation) { - objOrent = (byte)orientation; + objOrent = orientation; } - + /** * Sets the X axis origin for object content - * + * * @param xOffset the X axis origin for object content */ public void setXOffset(int xOffset) { - this.objXOffset = xOffset; + this.objXOffset = xOffset; } - + /** * Sets the Y axis origin for object content - * + * * @param yOffset the Y axis origin for object content */ public void setYOffset(int yOffset) { @@ -105,7 +105,7 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { os.write(data); } - + /** {@inheritDoc} */ public void writeContent(OutputStream os) throws IOException { byte[] data = new byte[12]; @@ -137,7 +137,7 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { data[11] = (byte)0xFF; // YocaOset } os.write(data); - + // Triplets super.writeContent(os); } diff --git a/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java b/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java index 49b33c0ef..a2ebe22f6 100644 --- a/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java +++ b/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java @@ -22,7 +22,6 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.triplets.Triplet; import org.apache.fop.afp.util.BinaryUtils; /** @@ -30,7 +29,7 @@ import org.apache.fop.afp.util.BinaryUtils; * affect the rendering of presentation data and the appearance that is to be assumed * by the presentation device. */ -public class PresentationEnvironmentControl extends AbstractStructuredAFPObject { +public class PresentationEnvironmentControl extends AbstractTripletStructuredObject { /** * Main constructor @@ -38,27 +37,6 @@ public class PresentationEnvironmentControl extends AbstractStructuredAFPObject public PresentationEnvironmentControl() { } - /** - * Sets the object offset - */ - public void setObjectOffset() { - addTriplet(new ObjectOffsetTriplet()); - } - - /** - * Sets the rendering intent - */ - public void setRenderingIntent() { - addTriplet(new RenderingIntentTriplet()); - } - - /** - * Sets the device appearance - */ - public void setDeviceAppearance() { - addTriplet(new DeviceAppearanceTriplet()); - } - /** {@inheritDoc} */ public void writeToStream(OutputStream os) throws IOException { byte[] data = new byte[11]; @@ -71,27 +49,50 @@ public class PresentationEnvironmentControl extends AbstractStructuredAFPObject data[10] = 0x00; // Reserved; must be zero os.write(data); - os.write(tripletData); + + writeTriplets(os); } +// /** +// * Sets the object offset +// */ +// public void setObjectOffset() { +// addTriplet(new ObjectOffsetTriplet()); +// } +// +// /** +// * Sets the rendering intent +// */ +// public void setRenderingIntent() { +// addTriplet(new RenderingIntentTriplet()); +// } +// +// /** +// * Sets the device appearance +// */ +// public void setDeviceAppearance() { +// addTriplet(new DeviceAppearanceTriplet()); +// } + + // TODO - private class DeviceAppearanceTriplet extends Triplet { - public DeviceAppearanceTriplet() { - super(Triplet.DEVICE_APPEARANCE); - } - } +// private class DeviceAppearanceTriplet extends AbstractTriplet { +// public DeviceAppearanceTriplet() { +// super(AbstractTriplet.DEVICE_APPEARANCE); +// } +// } // TODO - private class RenderingIntentTriplet extends Triplet { - public RenderingIntentTriplet() { - super(Triplet.RENDERING_INTENT); - } - } +// private class RenderingIntentTriplet extends AbstractTriplet { +// public RenderingIntentTriplet() { +// super(AbstractTriplet.RENDERING_INTENT); +// } +// } // TODO - private class ObjectOffsetTriplet extends Triplet { - public ObjectOffsetTriplet() { - super(Triplet.OBJECT_OFFSET); - } - } +// private class ObjectOffsetTriplet extends AbstractTriplet { +// public ObjectOffsetTriplet() { +// super(AbstractTriplet.OBJECT_OFFSET); +// } +// } } diff --git a/src/java/org/apache/fop/afp/modca/Registry.java b/src/java/org/apache/fop/afp/modca/Registry.java index 23c72cd39..481a72afd 100644 --- a/src/java/org/apache/fop/afp/modca/Registry.java +++ b/src/java/org/apache/fop/afp/modca/Registry.java @@ -30,6 +30,7 @@ public final class Registry { /** IOB supported object types */ private static final byte COMPID_IOCA_FS10 = 5; private static final byte COMPID_IOCA_FS11 = 11; + private static final byte COMPID_IOCA_FS40 = 55; private static final byte COMPID_IOCA_FS45 = 12; private static final byte COMPID_EPS = 13; private static final byte COMPID_TIFF = 14; @@ -38,6 +39,9 @@ public final class Registry { private static final byte COMPID_PDF_SINGLE_PAGE = 25; private static final byte COMPID_PCL_PAGE_OBJECT = 34; + private static final byte COMPID_TRUETYPE_OPENTYPE_FONT_RESOURCE_OBJECT = 51; + private static final byte COMPID_TRUETYPE_OPENTYPE_FONT_COLLECTION_RESOURCE_OBJECT = 53; + /** mime type entry mapping */ private final java.util.Map/*<String, ObjectType>*/ mimeObjectTypeMap = Collections.synchronizedMap( @@ -91,6 +95,16 @@ public final class Registry { MimeConstants.MIME_AFP_IOCA_FS11 ) ); +// mimeObjectTypeMap.put( +// MimeConstants.MIME_AFP_IOCA_FS40, +// new ObjectType( +// COMPID_IOCA_FS40, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x37}, +// "IOCA FS40", +// true, +// MimeConstants.MIME_AFP_IOCA_FS40 +// ) +//); mimeObjectTypeMap.put( MimeConstants.MIME_AFP_IOCA_FS45, new ObjectType( @@ -160,6 +174,26 @@ public final class Registry { MimeConstants.MIME_PCL ) ); +// mimeObjectTypeMap.put( +// null, +// new ObjectType( +// COMPID_TRUETYPE_OPENTYPE_FONT_RESOURCE_OBJECT, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x33}, +// "TrueType/OpenType Font Resource Object", +// true, +// null +// ) +// ); +// mimeObjectTypeMap.put( +// null, +// new ObjectType( +// COMPID_TRUETYPE_OPENTYPE_FONT_COLLECTION_RESOURCE_OBJECT, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x35}, +// "TrueType/OpenType Font Collection Resource Object", +// true, +// null +// ) +// ); } /** @@ -193,11 +227,11 @@ public final class Registry { */ public ObjectType(byte componentId, byte[] oid, String name, boolean includable, String mimeType) { + this.componentId = componentId; + this.oid = oid; this.name = name; this.includable = includable; this.mimeType = mimeType; - this.componentId = componentId; - this.oid = oid; } /** diff --git a/src/java/org/apache/fop/afp/modca/ResourceObject.java b/src/java/org/apache/fop/afp/modca/ResourceObject.java index a5b551e07..0f555a42e 100644 --- a/src/java/org/apache/fop/afp/modca/ResourceObject.java +++ b/src/java/org/apache/fop/afp/modca/ResourceObject.java @@ -22,14 +22,50 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.triplets.Triplet; +import org.apache.fop.afp.modca.triplets.ResourceObjectTypeTriplet; import org.apache.fop.afp.util.BinaryUtils; /** * This resource structured field begins an envelope that is used to carry * resource objects in print-file-level (external) resource groups. */ -public class ResourceObject extends AbstractPreparedAFPObject { +public class ResourceObject extends AbstractNamedAFPObject { + + /** graphics object type */ + public static final byte TYPE_GRAPHIC = 0x03; + + /** barcode object type */ + public static final byte TYPE_BARCODE = 0x05; + + /** image object type */ + public static final byte TYPE_IMAGE = 0x06; + + /** font character set type */ + public static final byte TYPE_FONT_CHARACTER_SET = 0x40; + + /** code page type */ + public static final byte TYPE_CODE_PAGE = 0x41; + + /** coded font type */ + public static final byte TYPE_CODED_FONT = 0x42; + + /** object container type */ + public static final byte TYPE_OBJECT_CONTAINER = (byte) 0x92; + + /** document object type */ + public static final byte TYPE_DOCUMENT = (byte) 0xA8; + + /** page segment object type */ + public static final byte TYPE_PAGE_SEGMENT = (byte) 0xFB; + + /** overlay object type */ + public static final byte TYPE_OVERLAY_OBJECT = (byte) 0xFC; + + /** page def type */ + public static final byte TYPE_PAGEDEF = (byte) 0xFD; + + /** form def type */ + public static final byte TYPE_FORMDEF = (byte) 0xFE; private AbstractNamedAFPObject namedObject; @@ -45,10 +81,10 @@ public class ResourceObject extends AbstractPreparedAFPObject { /** * Sets the data object referenced by this resource object * - * @param obj the named data object + * @param namedObject the named data object */ - public void setDataObject(AbstractNamedAFPObject obj) { - this.namedObject = obj; + public void setDataObject(AbstractNamedAFPObject namedObject) { + this.namedObject = namedObject; } /** @@ -78,11 +114,13 @@ public class ResourceObject extends AbstractPreparedAFPObject { data[18] = 0x00; // Reserved os.write(data); + + // Write triplets + writeTriplets(os); } /** {@inheritDoc} */ protected void writeContent(OutputStream os) throws IOException { - super.writeContent(os); // write triplets if (namedObject != null) { namedObject.writeToStream(os); } @@ -109,60 +147,4 @@ public class ResourceObject extends AbstractPreparedAFPObject { getTriplets().add(new ResourceObjectTypeTriplet(type)); } - /** graphics object type */ - public static final byte TYPE_GRAPHIC = 0x03; - - /** barcode object type */ - public static final byte TYPE_BARCODE = 0x05; - - /** image object type */ - public static final byte TYPE_IMAGE = 0x06; - - /** font character set type */ - public static final byte TYPE_FONT_CHARACTER_SET = 0x40; - - /** code page type */ - public static final byte TYPE_CODE_PAGE = 0x41; - - /** coded font type */ - public static final byte TYPE_CODED_FONT = 0x42; - - /** object container type */ - public static final byte TYPE_OBJECT_CONTAINER = (byte) 0x92; - - /** document object type */ - public static final byte TYPE_DOCUMENT = (byte) 0xA8; - - /** page segment object type */ - public static final byte TYPE_PAGE_SEGMENT = (byte) 0xFB; - - /** overlay object type */ - public static final byte TYPE_OVERLAY_OBJECT = (byte) 0xFC; - - /** page def type */ - public static final byte TYPE_PAGEDEF = (byte) 0xFD; - - /** form def type */ - public static final byte TYPE_FORMDEF = (byte) 0xFE; - - - /** resource object type triplet */ - private class ResourceObjectTypeTriplet extends Triplet { - - private static final byte RESOURCE_OBJECT = 0x21; - - /** - * Main constructor - * - * @param objectType the resource object type - */ - public ResourceObjectTypeTriplet(byte objectType) { - super(RESOURCE_OBJECT, - new byte[] { - objectType, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Constant Data - } - ); - } - } } diff --git a/src/java/org/apache/fop/afp/modca/PreparedAFPObject.java b/src/java/org/apache/fop/afp/modca/StructuredDataObject.java index 2771515bf..f95810ff2 100644 --- a/src/java/org/apache/fop/afp/modca/PreparedAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/StructuredDataObject.java @@ -20,15 +20,14 @@ package org.apache.fop.afp.modca; /** - * An AFP object which is able to know its own data length before write() + * An AFP object which is able to know its own data length before writeToStream() */ -public interface PreparedAFPObject { +public interface StructuredDataObject { /** - * Returns the current data length of this container + * Returns the data length of this structured field * - * @return the current data length of this container including - * all enclosed GOCA drawing objects + * @return the data length of this structured field */ int getDataLength(); }
\ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/Triplet.java b/src/java/org/apache/fop/afp/modca/triplets/AbstractTriplet.java index b7f51466b..63914eb73 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/Triplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/AbstractTriplet.java @@ -19,17 +19,13 @@ package org.apache.fop.afp.modca.triplets; -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; - -import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.Streamable; +import org.apache.fop.afp.modca.StructuredDataObject; /** * A simple implementation of a MOD:CA triplet */ -public class Triplet implements Streamable { +public abstract class AbstractTriplet implements Streamable, StructuredDataObject { public static final byte CODED_GRAPHIC_CHARACTER_SET_GLOBAL_IDENTIFIER = 0x01; /** Triplet identifiers */ @@ -89,60 +85,18 @@ public class Triplet implements Streamable { public static final byte DEVICE_APPEARANCE = (byte)0x97; /** the triplet identifier */ - private final byte id; - - /** the triplet's data contents */ - private byte[] data; - - /** - * Main constructor - * - * @param id the triplet identifier (see static definitions above) - * @param data the data item contained in this triplet - */ - public Triplet(byte id, byte[] data) { - this(id); - setData(data); - } + protected final byte id; /** * Constructor * * @param id the triplet identifier (see static definitions above) */ - public Triplet(byte id) { + public AbstractTriplet(byte id) { this.id = id; } /** - * Constructor - * - * @param id the triplet identifier (see static definitions above) - * @param content the content byte data - */ - public Triplet(byte id, byte content) { - this(id, new byte[] {content}); - } - - /** - * Constructor - * - * @param id the triplet identifier (see static definitions above) - * @param data the data item (in String form) contained in this triplet - * @throws UnsupportedEncodingException EBCIDIC encoding is not supported - */ - public Triplet(byte id, String data) throws UnsupportedEncodingException { - this(id, data.getBytes(AFPConstants.EBCIDIC_ENCODING)); - } - - /** {@inheritDoc} */ - public void writeToStream(OutputStream os) throws IOException { - os.write((byte)data.length + 2); - os.write(id); - os.write(data); - } - - /** * Returns the triplet identifier * * @return the triplet identifier @@ -152,11 +106,15 @@ public class Triplet implements Streamable { } /** - * Sets the data contents of this triplet + * Returns the structured triplet data array * - * @param data the data contents + * @return the structured triplet data array */ - protected void setData(byte[] data) { - this.data = data; + public byte[] getData() { + int dataLen = getDataLength(); + byte[] data = new byte[dataLen]; + data[0] = (byte)dataLen; + data[1] = id; + return data; } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java new file mode 100644 index 000000000..9b15de8c1 --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java @@ -0,0 +1,52 @@ +/* + * 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.afp.modca.triplets; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.AFPConstants; + +/** + * An inline comment triplet. + */ +public class CommentTriplet extends AbstractTriplet { + + private final String commentString; + + public CommentTriplet(byte id, String commentString) { + super(id); + this.commentString = commentString; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + commentString.length(); + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] strData = commentString.getBytes(AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(strData, 0, data, 2, strData.length); + os.write(data); + } + +} diff --git a/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java new file mode 100644 index 000000000..61b279b5c --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java @@ -0,0 +1,50 @@ +/* + * 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.afp.modca.triplets; + +import java.io.IOException; +import java.io.OutputStream; + +public class DescriptorPositionTriplet extends AbstractTriplet { + + private final byte oapId; + + /** + * Main constructor + * + * @param oapId the object area position id + */ + public DescriptorPositionTriplet(byte oapId) { + super(DESCRIPTOR_POSITION); + this.oapId = oapId; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = oapId; + os.write(data); + } +} diff --git a/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java index a6059d3ec..af9452fcb 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java @@ -19,6 +19,9 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** @@ -27,7 +30,7 @@ import org.apache.fop.afp.util.BinaryUtils; * resource name by a map structured field, such as a Map Data Resource structured * field, or a Map Media Type structured field. */ -public class ExtendedResourceLocalIdentifierTriplet extends Triplet { +public class ExtendedResourceLocalIdentifierTriplet extends AbstractTriplet { /** the image resource type */ public static final byte TYPE_IMAGE_RESOURCE = 0x10; @@ -38,6 +41,12 @@ public class ExtendedResourceLocalIdentifierTriplet extends Triplet { /** the retired value type */ public static final byte TYPE_MEDIA_RESOURCE = 0x40; + /** the resource type */ + private final byte type; + + /** the resource local id */ + private final int localId; + /** * Main constructor * @@ -45,11 +54,22 @@ public class ExtendedResourceLocalIdentifierTriplet extends Triplet { * @param localId the resource local id */ public ExtendedResourceLocalIdentifierTriplet(byte type, int localId) { - super(Triplet.EXTENDED_RESOURCE_LOCAL_IDENTIFIER); - byte[] data = new byte[5]; - data[0] = type; - byte[] resLID = BinaryUtils.convert(localId, 4); - System.arraycopy(resLID, 0, data, 1, resLID.length); - super.setData(data); + super(AbstractTriplet.EXTENDED_RESOURCE_LOCAL_IDENTIFIER); + this.type = type; + this.localId = localId; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = type; + byte[] resLID = BinaryUtils.convert(localId, 4); // 4 bytes + System.arraycopy(resLID, 0, data, 3, resLID.length); + os.write(data); + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 7; } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java index a949124c8..55653457c 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,105 +19,107 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import org.apache.fop.afp.AFPConstants; /** * A Fully Qualified Name triplet enable the identification and referencing of - * objects using Gloabl Identifiers (GIDs). + * objects using Gloabl Identifiers (GIDs). */ -public class FullyQualifiedNameTriplet extends Triplet { +public class FullyQualifiedNameTriplet extends AbstractTriplet { // Specifies how the GID will be used /** This GID replaces the first parameter in the structured field that contains a GID name. */ public static final byte TYPE_REPLACE_FIRST_GID_NAME = 0x01; - + /** This triplet contains the name of a font family. */ public static final byte TYPE_FONT_FAMILY_NAME = 0x07; - + /** This triplet contains the name of a font typeface. */ public static final byte TYPE_FONT_TYPEFACE_NAME = 0x08; - + /** This triplet specifies a reference to the MO:DCA resource hierarchy. */ public static final byte TYPE_MODCA_RESOURCE_HIERARCHY_REF = 0x09; - + /** The triplet contains a GID reference to a begin resource group structured field. */ public static final byte TYPE_BEGIN_RESOURCE_GROUP_REF = 0x0A; - + /** The triplet contains a GID reference to a document attribute. */ public static final byte TYPE_ATTRIBUTE_GID = 0x0B; - + /** The triplet contains the GID of a process element. */ public static final byte TYPE_PROCESS_ELEMENT_GID = 0x0C; - + /** The triplet contains a reference to a begin page group structured field. */ public static final byte TYPE_BEGIN_PAGE_GROUP_REF = 0x0D; - + /** The triplet contains a reference to a media type. */ public static final byte TYPE_MEDIA_TYPE_REF = 0x11; - + /** The triplet contains a reference to a color management resource. */ public static final byte TYPE_COLOR_MANAGEMENT_RESOURCE_REF = 0x41; /** The triplet contains a reference to a data-object font file that defines a base font. */ public static final byte TYPE_DATA_OBJECT_FONT_BASE_FONT_ID = 0x6E; - + /** The triplet contains a reference to a data-object font file that defines a linked font. */ public static final byte TYPE_DATA_OBJECT_FONT_LINKED_FONT_ID = 0x7E; - - /** The triplet contains a reference to a begin document structured field. */ + + /** The triplet contains a reference to a begin document structured field. */ public static final byte TYPE_BEGIN_DOCUMENT_REF = (byte)0x83; - + /** * The triplet contains a reference to a begin structured field associated with a resource; * or contains a GID reference to a coded font. */ public static final byte TYPE_BEGIN_RESOURCE_OBJECT_REF = (byte)0x84; - + /** * The triplet contains a GID reference to a code page that specifies the code points and * graphic character names for a coded font. */ public static final byte TYPE_CODE_PAGE_NAME_REF = (byte)0x85; - - /** + + /** * The triplet contains a GID name reference to a font character set that specifies * a set of graphics characters. */ public static final byte TYPE_FONT_CHARSET_NAME_REF = (byte)0x86; - + /** The triplet contains a GID reference to a begin page structured field. */ public static final byte TYPE_BEGIN_PAGE_REF = (byte)0x87; - + /** The triplet contains a GID reference to a begin medium map structured field. */ public static final byte TYPE_BEGIN_MEDIUM_MAP_REF = (byte)0x8D; - + /** * The triplet contains a GID reference to a coded font, which identifies a specific * code page and a specific font character set. */ public static final byte TYPE_CODED_FONT_NAME_REF = (byte)0x8E; - + /** The triplet contains a GID reference to a begin document index structured field. */ - public static final byte TYPE_BEGIN_DOCUMENT_INDEX_REF = (byte)0x98; - + public static final byte TYPE_BEGIN_DOCUMENT_INDEX_REF = (byte)0x98; + /** The triplet contains a GID reference to a begin overlay structured field. */ public static final byte TYPE_BEGIN_OVERLAY_REF = (byte)0xB0; - + /** The triplet contains a GID reference to a resource used by a data object. */ public static final byte TYPE_DATA_OBJECT_INTERNAL_RESOURCE_REF = (byte)0xBE; - + /** The triplet contains a GID reference to an index element structured field. */ public static final byte TYPE_INDEX_ELEMENT_GID = (byte)0xCA; - + /** * The triplet contains a reference to other object data which may or may * not be defined by an IBM presentation architecture. */ public static final byte TYPE_OTHER_OBJECT_DATA_REF = (byte)0xCE; - + /** * The triplet contains a reference to a resource used by a data object. * The GID may be a filename or any other identifier associated with the @@ -127,44 +129,71 @@ public class FullyQualifiedNameTriplet extends Triplet { */ public static final byte TYPE_DATA_OBJECT_EXTERNAL_RESOURCE_REF = (byte)0xDE; - + // GID Format - + /** The GID is a character encoded name. */ public static final byte FORMAT_CHARSTR = (byte)0x00; - + /** the GID is a ASN.1 object identifier (OID). */ public static final byte FORMAT_OID = (byte)0x10; /** the GID is a uniform resource locator (URL). */ public static final byte FORMAT_URL = (byte)0x20; - - private String fqName; - - /** - * @return the actual fully qualified name of this triplet - */ - public String getFullyQualifiedName() { - return fqName; - } + /** the fully qualified name type */ + private final byte type; + + /** the fully qualified name format */ + private final byte format; + + /** the actual fully qualified name */ + private final String fqName; /** * Main constructor - * + * * @param type the fully qualified name type * @param format the fully qualified name format * @param fqName the fully qualified name */ public FullyQualifiedNameTriplet(byte type, byte format, String fqName) { super(FULLY_QUALIFIED_NAME); - + this.type = type; + this.format = format; this.fqName = fqName; + } + /** + * Returns the actual fully qualified name + * + * @return the actual fully qualified name + */ + public String getFullyQualifiedName() { + return fqName; + } + + /** {@inheritDoc} */ + public String toString() { + return this.fqName; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 4 + fqName.length(); + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = type; + data[3] = format; + + // FQName byte[] fqNameBytes; - String encoding = AFPConstants.EBCIDIC_ENCODING; + String encoding = AFPConstants.EBCIDIC_ENCODING; if (format == FORMAT_URL) { - encoding = AFPConstants.US_ASCII_ENCODING; + encoding = AFPConstants.US_ASCII_ENCODING; } try { fqNameBytes = fqName.getBytes(encoding); @@ -172,20 +201,8 @@ public class FullyQualifiedNameTriplet extends Triplet { throw new IllegalArgumentException( encoding + " encoding failed"); } + System.arraycopy(fqNameBytes, 0, data, 4, fqNameBytes.length); - byte[] data = new byte[2 + fqNameBytes.length]; - data[0] = type; - data[1] = format; - // FQName - System.arraycopy(fqNameBytes, 0, data, 2, fqNameBytes.length); - - super.setData(data); - } - - /** - * {@inheritDoc} - */ - public String toString() { - return this.fqName; + os.write(data); } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java index 9fe6dad79..0d20d0227 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,10 +19,13 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * Specifies the mapping of data object presentation space to object area */ -public class MappingOptionTriplet extends Triplet { +public class MappingOptionTriplet extends AbstractTriplet { /** * the data object is placed in the upper left corner, all data must be presented * within the object area extents @@ -34,35 +37,51 @@ public class MappingOptionTriplet extends Triplet { * the object area extents will be presented but data that falls outside will not be presented */ public static final byte POSITION_AND_TRIM = 0x10; - + /** * the data object is centred and symmetrically scaled up or down * while preserving aspect ratio */ public static final byte SCALE_TO_FIT = 0x20; - + /** * the data object is centred, all data that falls within the object area extents * will be presented but data that falls outside will not be presented */ public static final byte CENTER_AND_TRIM = 0x30; - + // public static final byte MIGRATION_MAPPING_1 = 0x41; // public static final byte MIGRATION_MAPPING_2 = 0x42; // public static final byte MIGRATION_MAPPING_3 = 0x50; - + /** the data object is centred, aspect ratio is not always preserved */ public static final byte SCALE_TO_FILL = 0x60; - + /** used to map ip3i print data objects */ public static final byte UP3I_PRINT_DATA = 0x70; + private final byte mapValue; + /** * Main constructor - * + * * @param mapValue the mapping option to use */ public MappingOptionTriplet(byte mapValue) { - super(Triplet.MAPPING_OPTION, mapValue); + super(AbstractTriplet.MAPPING_OPTION); + this.mapValue = mapValue; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = mapValue; + + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java index d39dc5f96..68d3fc40c 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,36 +19,54 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Measurement Units triplet is used to specify the units of measure * for a presentation space */ -public class MeasurementUnitsTriplet extends Triplet { +public class MeasurementUnitsTriplet extends AbstractTriplet { - private static final byte TEN_INCHES = 0x00; + private static final byte TEN_INCHES = 0x00; private static final byte TEN_CM = 0x01; - + private final int xRes; + private final int yRes; + /** * Main constructor - * + * * @param xRes units per base on the x-axis * @param yRes units per base on the y-axis */ public MeasurementUnitsTriplet(int xRes, int yRes) { super(MEASUREMENT_UNITS); - //TODO: units correct? + this.xRes = xRes; + this.yRes = yRes; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 8; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + + data[2] = TEN_INCHES; // XoaBase + data[3] = TEN_INCHES; // YoaBase + byte[] xUnits = BinaryUtils.convert(xRes * 10, 2); + data[4] = xUnits[0]; // XoaUnits (x units per unit base) + data[5] = xUnits[1]; + byte[] yUnits = BinaryUtils.convert(yRes * 10, 2); - byte[] data = new byte[] { - TEN_INCHES, // XoaBase - TEN_INCHES, // YoaBase - xUnits[0], // XoaUnits (x units per unit base) - xUnits[1], - yUnits[0], // YoaUnits (y units per unit base) - yUnits[1] - }; - super.setData(data); + data[6] = yUnits[0]; // YoaUnits (y units per unit base) + data[7] = yUnits[1]; + + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java index 64fba23c9..3d408639e 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,44 +19,66 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Object Area Size triplet is used to specify the extent of an object area * in the X and Y directions */ -public class ObjectAreaSizeTriplet extends Triplet { +public class ObjectAreaSizeTriplet extends AbstractTriplet { + + private final int x; + private final int y; + private final byte type; /** * Main constructor - * + * * @param x the object area extent for the X axis * @param y the object area extent for the Y axis * @param type the object area size type */ public ObjectAreaSizeTriplet(int x, int y, byte type) { - super(Triplet.OBJECT_AREA_SIZE); - byte[] xOASize = BinaryUtils.convert(x, 3); - byte[] yOASize = BinaryUtils.convert(y, 3); - byte[] data = new byte[] { - type, // SizeType - xOASize[0], // XoaSize - Object area extent for X axis - xOASize[1], - xOASize[2], - yOASize[0], // YoaSize - Object area extent for Y axis - yOASize[1], - yOASize[2] - }; - super.setData(data); + super(AbstractTriplet.OBJECT_AREA_SIZE); + this.x = x; + this.y = y; + this.type = type; } /** * Main constructor - * + * * @param x the object area extent for the X axis * @param y the object area extent for the Y axis */ public ObjectAreaSizeTriplet(int x, int y) { this(x, y, (byte)0x02); } + + /** {@inheritDoc} */ + public int getDataLength() { + return 9; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + + data[2] = type; // SizeType + + byte[] xOASize = BinaryUtils.convert(x, 3); + data[3] = xOASize[0]; // XoaSize - Object area extent for X axis + data[4] = xOASize[1]; + data[5] = xOASize[2]; + + byte[] yOASize = BinaryUtils.convert(y, 3); + data[6] = yOASize[0]; // YoaSize - Object area extent for Y axis + data[7] = yOASize[1]; + data[8] = yOASize[2]; + + os.write(data); + } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java index 355fe8288..3aaa6969a 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java @@ -19,12 +19,17 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Object Byte Extent triplet is used to specify the number of bytes contained in an object */ -public class ObjectByteExtentTriplet extends Triplet { +public class ObjectByteExtentTriplet extends AbstractTriplet { + + private final int byteExt; /** * Main constructor @@ -33,7 +38,19 @@ public class ObjectByteExtentTriplet extends Triplet { */ public ObjectByteExtentTriplet(int byteExt) { super(OBJECT_BYTE_EXTENT); - byte[] data = BinaryUtils.convert(byteExt, 4); - super.setData(data); + this.byteExt = byteExt; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 6; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] extData = BinaryUtils.convert(byteExt, 4); + System.arraycopy(extData, 0, data, 2, extData.length); + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java index 1692339ac..346dddaab 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java @@ -19,7 +19,8 @@ package org.apache.fop.afp.modca.triplets; -import java.io.UnsupportedEncodingException; +import java.io.IOException; +import java.io.OutputStream; import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.modca.Registry.ObjectType; @@ -29,7 +30,7 @@ import org.apache.fop.afp.util.StringUtils; * 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 AbstractTriplet { /** * The scope of this object is the including page or overlay @@ -64,6 +65,28 @@ public class ObjectClassificationTriplet extends Triplet { */ public static final byte CLASS_DATA_OBJECT_FONT = 0x41; + /** the object class */ + private final byte objectClass; + + /** the object type */ + private final ObjectType objectType; + + /** whether the container has an object environment group */ + private final boolean containerHasOEG; + + /** whether the data resides within the container */ + private final boolean dataInContainer; + + /** whether the data resides within the object container data */ + private final boolean dataInOCD; + + /** the object level (version) */ + private final String objectLevel; + + /** the company/organization name */ + private final String companyName; + + /** * Main constructor * @@ -79,11 +102,6 @@ public class ObjectClassificationTriplet extends Triplet { this(objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD, null, null); } - - private static final int OBJECT_LEVEL_LEN = 8; - private static final int OBJECT_TYPE_NAME_LEN = 32; - private static final int COMPANY_NAME_LEN = 32; - /** * Fully parameterized constructor * @@ -100,56 +118,16 @@ public class ObjectClassificationTriplet extends Triplet { String objLev, String compName) { super(OBJECT_CLASSIFICATION); + this.objectClass = objectClass; if (objectType == null) { throw new IllegalArgumentException("MO:DCA Registry object type is null"); } - - byte[] data = new byte[94]; - 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) - - // StrucFlgs - Information on the structure of the object container - byte[] strucFlgs = getStrucFlgs(dataInContainer, containerHasOEG, dataInOCD); - data[4] = strucFlgs[0]; - data[5] = strucFlgs[1]; - - byte[] oid = objectType.getOID(); - // RegObjId - MOD:CA-registered ASN.1 OID for object type (8-23) - System.arraycopy(oid, 0, data, 6, oid.length); - - // ObjTpName - name of object type (24-55) - byte[] objTpName; - try { - objTpName = StringUtils.rpad(objectType.getName(), ' ', OBJECT_TYPE_NAME_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - System.arraycopy(objTpName, 0, data, 22, objTpName.length); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - - // ObjLev - release level or version number of object type (56-63) - byte[] objectLevel; - try { - objectLevel = StringUtils.rpad(objLev, ' ', OBJECT_LEVEL_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - System.arraycopy(objectLevel, 0, data, 54, objectLevel.length); - - // CompName - name of company or organization that owns object definition (64-95) - byte[] companyName; - try { - companyName = StringUtils.rpad(compName, ' ', COMPANY_NAME_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - System.arraycopy(companyName, 0, data, 62, companyName.length); - - super.setData(data); + this.objectType = objectType; + this.dataInContainer = dataInContainer; + this.containerHasOEG = containerHasOEG; + this.dataInOCD = dataInOCD; + this.objectLevel = objLev; + this.companyName = compName; } /** @@ -161,7 +139,7 @@ public class ObjectClassificationTriplet extends Triplet { * * @return the byte value of this structure */ - public byte[] getStrucFlgs(boolean dataInContainer, boolean containerHasOEG, + public byte[] getStructureFlagsAsBytes(boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { byte[] strucFlgs = new byte[2]; // Object Container (BOC/EOC) @@ -185,4 +163,52 @@ public class ObjectClassificationTriplet extends Triplet { strucFlgs[1] = 0x00; return strucFlgs; } + + /** {@inheritDoc} */ + public int getDataLength() { + return 95; + } + + private static final int OBJECT_LEVEL_LEN = 8; + private static final int OBJECT_TYPE_NAME_LEN = 32; + private static final int COMPANY_NAME_LEN = 32; + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = 0x00; // reserved (must be zero) + data[3] = objectClass; // ObjClass + data[4] = 0x00; // reserved (must be zero) + data[5] = 0x00; // reserved (must be zero) + + // StrucFlgs - Information on the structure of the object container + byte[] structureFlagsBytes = getStructureFlagsAsBytes(dataInContainer, containerHasOEG, dataInOCD); + data[6] = structureFlagsBytes[0]; + data[7] = structureFlagsBytes[1]; + + byte[] objectIdBytes = objectType.getOID(); + // RegObjId - MOD:CA-registered ASN.1 OID for object type (8-23) + System.arraycopy(objectIdBytes, 0, data, 8, objectIdBytes.length); + + // ObjTpName - name of object type (24-55) + byte[] objectTypeNameBytes; + objectTypeNameBytes + = StringUtils.rpad(objectType.getName(), ' ', OBJECT_TYPE_NAME_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(objectTypeNameBytes, 0, data, 24, objectTypeNameBytes.length); + + // ObjLev - release level or version number of object type (56-63) + byte[] objectLevelBytes; + objectLevelBytes = StringUtils.rpad(objectLevel, ' ', OBJECT_LEVEL_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(objectLevelBytes, 0, data, 56, objectLevelBytes.length); + + // CompName - name of company or organization that owns object definition (64-95) + byte[] companyNameBytes; + companyNameBytes = StringUtils.rpad(companyName, ' ', COMPANY_NAME_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(companyNameBytes, 0, data, 64, companyNameBytes.length); + + os.write(data); + } }
\ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java index 0f087e4fd..b93ec5477 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,28 +19,31 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * This triplet is used to specify the resulting appearance when data in a new * presentation space is merged with data in an existing presentation space. */ -public class PresentationSpaceMixingRulesTriplet extends Triplet { +public class PresentationSpaceMixingRulesTriplet extends AbstractTriplet { - /** background on background mixing rule */ + /** background on background mixing rule */ public static final byte RULE_BACK_ON_BACK = 0x70; - /** background on foreground mixing rule */ + /** background on foreground mixing rule */ public static final byte RULE_BACK_ON_FORE = 0x71; - /** foreground on background mixing rule */ + /** foreground on background mixing rule */ public static final byte RULE_FORE_ON_BACK = 0x72; - /** foreground on foreground mixing rule */ + /** foreground on foreground mixing rule */ public static final byte RULE_FORE_ON_FORE = 0x73; - + /** overpaint */ public static final byte OVERPAINT = (byte)0x01; - + /** underpaint */ public static final byte UNDERPAINT = (byte)0x02; @@ -49,14 +52,29 @@ public class PresentationSpaceMixingRulesTriplet extends Triplet { /** MO:DCA default mixing */ public static final byte DEFAULT = (byte)0xFF; - - + + /** the mixing rules */ + private final byte[] rules; + /** * Main constructor - * + * * @param rules the mixing rules */ public PresentationSpaceMixingRulesTriplet(byte[] rules) { - super(PRESENTATION_SPACE_MIXING_RULE, rules); + super(PRESENTATION_SPACE_MIXING_RULE); + this.rules = rules; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + rules.length; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + System.arraycopy(rules, 0, data, 2, rules.length); + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java index 38041f140..cecd40910 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,30 +19,48 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * This triplet is used to specify the resulting appearance when data in a new * presentation space is merged with data in an existing presentation space. */ -public class PresentationSpaceResetMixingTriplet extends Triplet { +public class PresentationSpaceResetMixingTriplet extends AbstractTriplet { /** * Do not reset to the color of the medium prior to * placing data into this MO:DCA presentation space. */ public static final byte NOT_RESET = 0x00; - + /** * Reset to the color of the medium prior to placing * data into this MO:DCA presentation space. */ public static final byte RESET = 0x01; - + + private final byte backgroundMixFlag; + /** * Main constructor - * + * * @param backgroundMixFlag the background mixing flag */ public PresentationSpaceResetMixingTriplet(byte backgroundMixFlag) { - super(PRESENTATION_SPACE_RESET_MIXING, backgroundMixFlag); + super(PRESENTATION_SPACE_RESET_MIXING); + this.backgroundMixFlag = backgroundMixFlag; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = backgroundMixFlag; + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java index 784975c43..ecc12122d 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,3 +19,36 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + +/** resource object type triplet */ +public class ResourceObjectTypeTriplet extends AbstractTriplet { + + private static final byte RESOURCE_OBJECT = 0x21; + + /** the resource object type */ + private final byte objectType; + + /** + * Main constructor + * + * @param objectType the resource object type + */ + public ResourceObjectTypeTriplet(byte objectType) { + super(RESOURCE_OBJECT); + this.objectType = objectType; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 10; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = objectType; + os.write(data); + } +} |