import org.apache.commons.logging.LogFactory;
import org.apache.fop.render.afp.modca.AbstractNamedAFPObject;
import org.apache.fop.render.afp.modca.DataObjectFactory;
+import org.apache.fop.render.afp.modca.Registry;
/**
* Caches and creates (as necessary using an instance of DataObjectFactory)
private static Map/*<Integer,DataObjectCache>*/ cacheMap
= new java.util.HashMap/*<Integer,DataObjectCache>*/();
-
/** Mapping of data object uri --> cache record */
- private Map/*<ResourceInfo,DataObjectCache.Record>*/ recordMap
- = new java.util.HashMap/*<ResourceInfo,DataObjectCache.Record>*/();
+ private Map/*<ResourceInfo,Record>*/ includableMap
+ = new java.util.HashMap/*<ResourceInfo,Record>*/();
/** Used for create data objects */
private DataObjectFactory factory = new DataObjectFactory();
log.error("Failed to close temporary file");
}
}
+
+ /**
+ * Stores a named data object in the cache
+ *
+ * @param dataObj a named data object
+ * @return a new cache record
+ */
+ public Record store(AbstractNamedAFPObject dataObj) {
+ Record record = new Record();
+ record.objectName = dataObj.getName();
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ try {
+ channel.position(nextPos);
+ record.position = channel.position();
+ dataObj.write(os);
+ record.size = os.size();
+ MappedByteBuffer byteBuffer
+ = channel.map(FileChannel.MapMode.READ_WRITE, record.position, record.size);
+ byte[] data = os.toByteArray();
+ byteBuffer.put(data);
+ channel.write(byteBuffer);
+ nextPos += record.size + 1;
+ } catch (IOException e) {
+ log.error("Failed to write cache record for '"
+ + dataObj + "', " + e.getMessage());
+ }
+ return record;
+ }
/**
* Creates and adds a new data object and record to the cache as necessary.
*
* @param dataObjectInfo a data object info
*
- * @return the name of the related data object
+ * @return a cache record
*/
- public String put(DataObjectInfo dataObjectInfo) {
- ResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
- Record record = (Record)recordMap.get(resourceInfo);
- if (record == null) {
- record = new Record();
+ public Record store(DataObjectInfo dataObjectInfo) {
+ Registry.ObjectType objectType = dataObjectInfo.getObjectType();
+ Record record = null;
+ if (!objectType.canBeIncluded()) {
AbstractNamedAFPObject dataObj = factory.createObject(dataObjectInfo);
- record.objectName = dataObj.getName();
-
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- channel.position(nextPos);
- record.position = channel.position();
- dataObj.write(os);
- record.size = os.size();
- MappedByteBuffer byteBuffer
- = channel.map(FileChannel.MapMode.READ_WRITE, record.position, record.size);
- byte[] data = os.toByteArray();
- byteBuffer.put(data);
- channel.write(byteBuffer);
- nextPos += record.size + 1;
- } catch (IOException e) {
- log.error("Failed to write cache record for '"
- + resourceInfo + "', " + e.getMessage());
+ record = store(dataObj);
+ } else {
+ ResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
+ record = (Record)includableMap.get(resourceInfo);
+ if (record == null) {
+ AbstractNamedAFPObject dataObj = factory.createObject(dataObjectInfo);
+ record = store(dataObj);
+ includableMap.put(resourceInfo, record);
}
- recordMap.put(resourceInfo, record);
}
- return record.objectName;
+ return record;
}
-
+
/**
* Returns the written binary data of the AbstractDataObject from the cache file
*
- * @param resourceInfo the data resource info
+ * @param record the cache record
* @return the binary data of the AbstractDataObject or null if failed.
*/
- public byte[] get(ResourceInfo resourceInfo) {
- Record record = (Record)recordMap.get(resourceInfo);
+ public byte[] retrieve(Record record) {
if (record == null) {
- throw new IllegalArgumentException("Unknown data object " + resourceInfo);
+ throw new IllegalArgumentException("Cache record is null");
}
MappedByteBuffer byteBuffer = null;
try {
byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, record.position, record.size);
} catch (IOException e) {
- log.error("Failed to read cache record for '" + resourceInfo + "', " + e.getMessage());
+ log.error("Failed to read cache record for '" + record + "', " + e.getMessage());
return null;
}
if (byteBuffer.hasArray()) {
return data;
}
}
-
+
/**
* Returns the data object factory
*
return this.factory;
}
+
/**
* A cache record
*/
- private class Record {
- protected long position;
- protected int size;
- protected String objectName;
+ public class Record {
+ private long position;
+ private int size;
+ private String objectName;
+ /** {@inheritDoc} */
public String toString() {
return "Record{name=" + objectName
+ ", pos=" + position
+ ", size=" + size
+ "}";
}
+
+ /**
+ * Returns the object name
+ *
+ * @return the object name
+ */
+ public String getObjectName() {
+ return this.objectName;
+ }
}
}
/** Static logging instance */
protected static Log log = LogFactory.getLog("org.apache.fop.render.afp.modca");
- /**
- * Boolean completion indicator
- */
+ /** Boolean completion indicator */
private boolean complete = false;
- /**
- * The application producing the AFP document
- */
+ /** The application producing the AFP document */
// not used
// private String producer = null;
- /**
- * The AFP document object
- */
+
+ /** The AFP document object */
private Document document = null;
- /**
- * The current page group object
- */
+ /** The current page group object */
private PageGroup currentPageGroup = null;
- /**
- * The current page object
- */
+ /** The current page object */
private PageObject currentPageObject = null;
- /**
- * The current overlay object
- */
+ /** The current overlay object */
private Overlay currentOverlay = null;
- /**
- * The current page
- */
+ /** The current page */
private AbstractPageObject currentPage = null;
- /**
- * The page count
- */
+ /** The page count */
private int pageCount = 0;
- /**
- * The page group count
- */
+ /** The page group count */
private int pageGroupCount = 0;
- /**
- * The overlay count
- */
+ /** The overlay count */
private int overlayCount = 0;
- /**
- * The portrait rotation
- */
+ /** The portrait rotation */
private int portraitRotation = 0;
- /**
- * The landscape rotation
- */
+ /** The landscape rotation */
private int landscapeRotation = 270;
- /**
- * The rotation
- */
+ /** The rotation */
private int orientation;
- /**
- * The outputstream for the data stream
- */
+ /** The outputstream for the data stream */
private OutputStream outputStream = null;
/** Maintain a reference count of instream objects for referencing purposes */
private int instreamObjectCount = 0;
- /**
- * The MO:DCA interchange set in use (default to MO:DCA-P IS/2 set)
- */
+ /** The MO:DCA interchange set in use (default to MO:DCA-P IS/2 set) */
private InterchangeSet interchangeSet
= InterchangeSet.valueOf(InterchangeSet.MODCA_PRESENTATION_INTERCHANGE_SET_2);
+ private DataObjectCache cache = DataObjectCache.getInstance();
+
/**
* The external resource group manager
*/
}
/**
+ * Returns the document object
+ *
* @return the document object
*/
private Document getDocument() {
}
/**
+ * Returns the current page
+ *
* @return the current page
*/
protected AbstractPageObject getCurrentPage() {
pageRotation, pageWidthRes, pageHeightRes);
currentPage = currentPageObject;
currentOverlay = null;
-// setOffsets(0, 0, 0);
}
/**
dataObjectInfo.setObjectType(objectType);
}
- DataObjectCache cache = DataObjectCache.getInstance();
- String includeName = cache.put(dataObjectInfo);
+ DataObjectCache.Record record = cache.store(dataObjectInfo);
if (objectType != null) {
// Create and return include
DataObjectFactory factory = cache.getFactory();
- IncludeObject includeObj = factory.createInclude(includeName, dataObjectInfo);
+ IncludeObject includeObj = factory.createInclude(
+ record.getObjectName(), dataObjectInfo);
getCurrentPage().addObject(includeObj);
// Record the resource cache key (uri) in the ResourceGroup
ResourceGroup resourceGroup = getResourceGroup(resourceLevel);
- resourceGroup.addObject(resourceInfo);
+ resourceGroup.addObject(record);
return;
} else {
log.warn("Data object located at '" + uri + "'"
+ " of type '" + objectType.getMimeType() + "'"
- + " cannot be included with an IOB so it will be embedded directly");
+ + " cannot be referenced with an include"
+ + " so it will be embedded directly");
}
} else {
if (resourceLevel.isExternal()) {
}
}
// Unrecognised/unsupported object type so add object reference directly in current page
- currentPageObject.addObject(resourceInfo);
+ currentPageObject.addObject(record);
}
// /**
public String getName() {
return name;
}
+
+ /** {@inheritDoc} */
+ public String toString() {
+ return getName();
+ }
}
if (obj instanceof Writable) {
Writable writableObject = (Writable)obj;
writableObject.write(os);
- } else if (obj instanceof ResourceInfo) {
- ResourceInfo resourceInfo = (ResourceInfo)obj;
- byte[] data = cache.get(resourceInfo);
+ } else if (obj instanceof DataObjectCache.Record) {
+ DataObjectCache.Record record = (DataObjectCache.Record)obj;
+ byte[] data = cache.retrieve(record);
os.write(data);
}
}
* @return the drawingOrder if it was added, null otherwise
*/
public PreparedAFPObject addObject(PreparedAFPObject preparedObject) {
-// log.debug(this + " adding " + preparedObject);
getObjects().add(preparedObject);
return preparedObject;
}
MimeConstants.MIME_PCL
)
);
+
+ // Entries without component and object ids
mimeEntryMap.put(
MimeConstants.MIME_SVG,
new ObjectType(
- COMPID_EPS,
- null, // no component id
"Scaleable Vector Graphics",
- false,
MimeConstants.MIME_SVG
)
);
+ mimeEntryMap.put(
+ MimeConstants.MIME_PNG,
+ new ObjectType(
+ "Portable Network Graphics",
+ MimeConstants.MIME_PNG
+ )
+ );
}
/**
private String name;
private boolean canBeIncluded;
private String mimeType;
-
+
+ /**
+ * Constructor
+ *
+ * @param name the object type name
+ * @param canBeIncluded true if this object can be included with an IOB structured field
+ * @param mimeType the mime type associated with this object type
+ */
+ private ObjectType(String name, boolean canBeIncluded, String mimeType) {
+ this.name = name;
+ this.canBeIncluded = canBeIncluded;
+ this.mimeType = mimeType;
+ }
+
+ /**
+ * Constructor
+ *
+ * @param name the object type name
+ * @param mimeType the mime type associated with this object type
+ */
+ private ObjectType(String name, String mimeType) {
+ this(name, false, mimeType);
+ }
+
/**
* Main constructor
+ *
* @param componentId the component id of this object type
* @param oid the object id of this object type
* @param name the object type name
*/
public ObjectType(byte componentId, byte[] oid, String name,
boolean canBeIncluded, String mimeType) {
+ this(name, canBeIncluded, mimeType);
this.componentId = componentId;
this.oid = oid;
- this.name = name;
- this.canBeIncluded = canBeIncluded;
- this.mimeType = mimeType;
}
-
+
/**
* 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.
*/
}
/**
+ * Returns the object type name for the given componentId
+ *
* @return the object type name for the given componentId
*/
public String getName() {
}
/**
+ * Returns the compontentId for this entry
+ *
* @return the compontentId for this entry
*/
public byte getComponentId() {
}
/**
+ * Returns true if this component can be included with an IOB structured field
+ *
* @return true if this component can be included with an IOB structured field
*/
public boolean canBeIncluded() {
}
/**
+ * Returns the mime type associated with this object type
+ *
* @return the mime type associated with this object type
*/
public String getMimeType() {
}
/**
+ * Returns true if this is an image type
+ *
* @return true if this is an image type
*/
public boolean isImage() {
return mimeType == MimeConstants.MIME_TIFF
|| mimeType == MimeConstants.MIME_GIF
+ || mimeType == MimeConstants.MIME_PNG
|| mimeType == MimeConstants.MIME_JPEG;
}
/**
+ * Returns true if this is a graphic type
+ *
* @return true if this is a graphic type
*/
public boolean isGraphic() {
return mimeType == MimeConstants.MIME_SVG;
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
public String toString() {
return this.getName();
}
import java.util.Set;
import org.apache.fop.render.afp.DataObjectCache;
-import org.apache.fop.render.afp.ResourceInfo;
+import org.apache.fop.render.afp.DataObjectCache.Record;
/**
* A Resource Group contains a set of overlays.
/** Set of resource uri */
private Set/*<String>*/ resourceSet = new java.util.HashSet/*<String>*/();
+ private DataObjectCache cache = DataObjectCache.getInstance();
+
/**
* Default constructor
*/
// }
/**
- * Add this object cache resource info to this resource group
+ * Add this object cache record to this resource group
*
- * @param resourceInfo the resource info
+ * @param record the cache record
*/
- public void addObject(ResourceInfo resourceInfo) {
- resourceSet.add(resourceInfo);
+ public void addObject(Record record) {
+ resourceSet.add(record);
}
/**
/** {@inheritDoc} */
public void writeContent(OutputStream os) throws IOException {
Iterator it = resourceSet.iterator();
- if (it.hasNext()) {
- DataObjectCache cache = DataObjectCache.getInstance();
- while (it.hasNext()) {
- ResourceInfo resourceInfo = (ResourceInfo)it.next();
- byte[] data = cache.get(resourceInfo);
- if (data != null) {
- os.write(data);
- } else {
- log.error("data was null");
- }
+ while (it.hasNext()) {
+ Record record = (Record)it.next();
+ byte[] data = cache.retrieve(record);
+ if (data != null) {
+ os.write(data);
+ } else {
+ log.error("data was null");
}
}
}