git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_URI_Unification@1348871 13f79535-47bb-0310-9956-ffa450edef68pull/26/head
import org.apache.xmlgraphics.image.loader.ImageSize; | import org.apache.xmlgraphics.image.loader.ImageSize; | ||||
import org.apache.xmlgraphics.util.MimeConstants; | import org.apache.xmlgraphics.util.MimeConstants; | ||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
import org.apache.fop.afp.modca.triplets.MappingOptionTriplet; | import org.apache.fop.afp.modca.triplets.MappingOptionTriplet; | ||||
import org.apache.fop.util.bitmap.DitherUtil; | import org.apache.fop.util.bitmap.DitherUtil; | ||||
AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo(); | AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo(); | ||||
imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS10); | imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS10); | ||||
//imageObjectInfo.setCreatePageSegment(true); | //imageObjectInfo.setCreatePageSegment(true); | ||||
imageObjectInfo.getResourceInfo().setLevel(new AFPResourceLevel(AFPResourceLevel.INLINE)); | |||||
imageObjectInfo.getResourceInfo().setLevel(new AFPResourceLevel(ResourceType.INLINE)); | |||||
imageObjectInfo.getResourceInfo().setImageDimension(ditherSize); | imageObjectInfo.getResourceInfo().setImageDimension(ditherSize); | ||||
imageObjectInfo.setBitsPerPixel(1); | imageObjectInfo.setBitsPerPixel(1); | ||||
imageObjectInfo.setColor(false); | imageObjectInfo.setColor(false); |
import java.awt.Dimension; | import java.awt.Dimension; | ||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
/** | /** | ||||
* The level at which a resource is to reside in the AFP output | * The level at which a resource is to reside in the AFP output | ||||
/** the general default resource level */ | /** the general default resource level */ | ||||
public static final AFPResourceLevel DEFAULT_LEVEL | public static final AFPResourceLevel DEFAULT_LEVEL | ||||
= new AFPResourceLevel(AFPResourceLevel.PRINT_FILE); | |||||
= new AFPResourceLevel(ResourceType.PRINT_FILE); | |||||
/** the URI of this resource */ | /** the URI of this resource */ | ||||
private String uri = null; | private String uri = null; |
package org.apache.fop.afp; | package org.apache.fop.afp; | ||||
import java.net.URI; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.DOCUMENT; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.EXTERNAL; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.INLINE; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PAGE; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PAGE_GROUP; | |||||
import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PRINT_FILE; | |||||
/** | /** | ||||
* A resource level | * A resource level | ||||
*/ | */ | ||||
public class AFPResourceLevel { | public class AFPResourceLevel { | ||||
public enum ResourceType { | |||||
/** directly in page **/ | |||||
INLINE("inline"), | |||||
/** page level **/ | |||||
PAGE("page"), | |||||
/** page group level **/ | |||||
PAGE_GROUP("page-group"), | |||||
/** document level **/ | |||||
DOCUMENT("document"), | |||||
/** print file level **/ | |||||
PRINT_FILE("print-file"), | |||||
/** external level **/ | |||||
EXTERNAL("external"); | |||||
private final String name; | |||||
private ResourceType(String name) { | |||||
this.name = name; | |||||
} | |||||
/** directly in page **/ | |||||
public static final int INLINE = 0; | |||||
/** page level **/ | |||||
public static final int PAGE = 1; | |||||
/** page group level **/ | |||||
public static final int PAGE_GROUP = 2; | |||||
/** document level **/ | |||||
public static final int DOCUMENT = 3; | |||||
/** print file level **/ | |||||
public static final int PRINT_FILE = 4; | |||||
/** external level **/ | |||||
public static final int EXTERNAL = 5; | |||||
private static final String NAME_INLINE = "inline"; | |||||
private static final String NAME_PAGE = "page"; | |||||
private static final String NAME_PAGE_GROUP = "page-group"; | |||||
private static final String NAME_DOCUMENT = "document"; | |||||
private static final String NAME_PRINT_FILE = "print-file"; | |||||
private static final String NAME_EXTERNAL = "external"; | |||||
private static final String[] NAMES = new String[] { | |||||
NAME_INLINE, NAME_PAGE, NAME_PAGE_GROUP, NAME_DOCUMENT, NAME_PRINT_FILE, NAME_EXTERNAL | |||||
}; | |||||
public static ResourceType getValueOf(String levelString) { | |||||
for (ResourceType resType : ResourceType.values()) { | |||||
if (resType.name.equalsIgnoreCase(levelString)) { | |||||
return resType; | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
/** where the resource will reside in the AFP output */ | |||||
private int level = PRINT_FILE; // default is print-file level (images) | |||||
public String getName() { | |||||
return name; | |||||
} | |||||
} | |||||
/** the external resource group file path */ | /** the external resource group file path */ | ||||
private String extFilePath = null; | |||||
private URI extUri = null; | |||||
private ResourceType resourceType; | |||||
/** | /** | ||||
* Sets the resource placement level within the AFP output | * Sets the resource placement level within the AFP output | ||||
* @return true if the resource level was successfully set | * @return true if the resource level was successfully set | ||||
*/ | */ | ||||
public static AFPResourceLevel valueOf(String levelString) { | public static AFPResourceLevel valueOf(String levelString) { | ||||
if (levelString != null) { | |||||
levelString = levelString.toLowerCase(); | |||||
AFPResourceLevel resourceLevel = null; | |||||
for (int i = 0; i < NAMES.length; i++) { | |||||
if (NAMES[i].equals(levelString)) { | |||||
resourceLevel = new AFPResourceLevel(i); | |||||
break; | |||||
} | |||||
} | |||||
return resourceLevel; | |||||
} | |||||
return null; | |||||
ResourceType resType = ResourceType.getValueOf(levelString); | |||||
return resType != null ? new AFPResourceLevel(resType) : null; | |||||
} | } | ||||
/** | /** | ||||
* | * | ||||
* @param level the resource level | * @param level the resource level | ||||
*/ | */ | ||||
public AFPResourceLevel(int level) { | |||||
setLevel(level); | |||||
} | |||||
/** | |||||
* Sets the resource level | |||||
* | |||||
* @param level the resource level | |||||
*/ | |||||
public void setLevel(int level) { | |||||
this.level = level; | |||||
public AFPResourceLevel(ResourceType resourceType) { | |||||
this.resourceType = resourceType; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this is at page level | * @return true if this is at page level | ||||
*/ | */ | ||||
public boolean isPage() { | public boolean isPage() { | ||||
return level == PAGE; | |||||
return resourceType == PAGE; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this is at page group level | * @return true if this is at page group level | ||||
*/ | */ | ||||
public boolean isPageGroup() { | public boolean isPageGroup() { | ||||
return level == PAGE_GROUP; | |||||
return resourceType == PAGE_GROUP; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this is at document level | * @return true if this is at document level | ||||
*/ | */ | ||||
public boolean isDocument() { | public boolean isDocument() { | ||||
return level == DOCUMENT; | |||||
return resourceType == DOCUMENT; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this is at external level | * @return true if this is at external level | ||||
*/ | */ | ||||
public boolean isExternal() { | public boolean isExternal() { | ||||
return level == EXTERNAL; | |||||
return resourceType == EXTERNAL; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this is at print-file level | * @return true if this is at print-file level | ||||
*/ | */ | ||||
public boolean isPrintFile() { | public boolean isPrintFile() { | ||||
return level == PRINT_FILE; | |||||
return resourceType == PRINT_FILE; | |||||
} | } | ||||
/** | /** | ||||
* @return true if this resource level is inline | * @return true if this resource level is inline | ||||
*/ | */ | ||||
public boolean isInline() { | public boolean isInline() { | ||||
return level == INLINE; | |||||
return resourceType == INLINE; | |||||
} | } | ||||
/** | /** | ||||
* | * | ||||
* @return the destination file path of the external resource group file | * @return the destination file path of the external resource group file | ||||
*/ | */ | ||||
public String getExternalFilePath() { | |||||
return this.extFilePath; | |||||
public URI getExternalUri() { | |||||
return this.extUri; | |||||
} | } | ||||
/** | /** | ||||
* | * | ||||
* @param filePath the external resource group file | * @param filePath the external resource group file | ||||
*/ | */ | ||||
public void setExternalFilePath(String filePath) { | |||||
this.extFilePath = filePath; | |||||
public void setExternalUri(URI uri) { | |||||
this.extUri = uri; | |||||
} | } | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public String toString() { | public String toString() { | ||||
return NAMES[level] + (isExternal() ? ", file=" + extFilePath : ""); | |||||
return resourceType + (isExternal() ? ", uri=" + extUri : ""); | |||||
} | } | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
} | } | ||||
AFPResourceLevel rl = (AFPResourceLevel)obj; | AFPResourceLevel rl = (AFPResourceLevel)obj; | ||||
return (level == rl.level) | |||||
&& (extFilePath == rl.extFilePath | |||||
|| extFilePath != null && extFilePath.equals(rl.extFilePath)); | |||||
return (resourceType == rl.resourceType) | |||||
&& (extUri == rl.extUri | |||||
|| extUri != null && extUri.equals(rl.extUri)); | |||||
} | } | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public int hashCode() { | public int hashCode() { | ||||
int hash = 7; | int hash = 7; | ||||
hash = 31 * hash + level; | |||||
hash = 31 * hash + (null == extFilePath ? 0 : extFilePath.hashCode()); | |||||
hash = 31 * hash + resourceType.hashCode(); | |||||
hash = 31 * hash + (null == extUri ? 0 : extUri.hashCode()); | |||||
return hash; | return hash; | ||||
} | } | ||||
} | } |
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
import org.apache.fop.afp.modca.ResourceObject; | import org.apache.fop.afp.modca.ResourceObject; | ||||
/** | /** | ||||
// level not explicitly set/changed so default to inline for GOCA graphic objects | // level not explicitly set/changed so default to inline for GOCA graphic objects | ||||
// (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine) | // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine) | ||||
setDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC, | setDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC, | ||||
new AFPResourceLevel(AFPResourceLevel.INLINE)); | |||||
new AFPResourceLevel(ResourceType.INLINE)); | |||||
} | } | ||||
/** | /** |
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | ||||
import org.apache.commons.logging.LogFactory; | import org.apache.commons.logging.LogFactory; | ||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
import org.apache.fop.afp.fonts.AFPFont; | import org.apache.fop.afp.fonts.AFPFont; | ||||
import org.apache.fop.afp.fonts.CharacterSet; | import org.apache.fop.afp.fonts.CharacterSet; | ||||
import org.apache.fop.afp.modca.AbstractNamedAFPObject; | import org.apache.fop.afp.modca.AbstractNamedAFPObject; | ||||
import org.apache.fop.afp.modca.Registry; | import org.apache.fop.afp.modca.Registry; | ||||
import org.apache.fop.afp.modca.ResourceGroup; | import org.apache.fop.afp.modca.ResourceGroup; | ||||
import org.apache.fop.afp.modca.ResourceObject; | import org.apache.fop.afp.modca.ResourceObject; | ||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceUtil; | import org.apache.fop.afp.util.AFPResourceUtil; | ||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | |||||
/** | /** | ||||
* Manages the creation and storage of document resources | * Manages the creation and storage of document resources | ||||
/** | /** | ||||
* Main constructor | * Main constructor | ||||
*/ | */ | ||||
public AFPResourceManager() { | |||||
public AFPResourceManager(URIResolverWrapper uriResolverWrapper) { | |||||
this.factory = new Factory(); | this.factory = new Factory(); | ||||
this.streamer = new AFPStreamer(factory); | |||||
this.streamer = new AFPStreamer(factory, uriResolverWrapper); | |||||
this.dataObjectFactory = new AFPDataObjectFactory(factory); | this.dataObjectFactory = new AFPDataObjectFactory(factory); | ||||
} | } | ||||
* @param filePath the default resource group file path | * @param filePath the default resource group file path | ||||
*/ | */ | ||||
public void setDefaultResourceGroupFilePath(String filePath) { | |||||
streamer.setDefaultResourceGroupFilePath(filePath); | |||||
public void setDefaultResourceGroupUri(URI uri) { | |||||
streamer.setDefaultResourceGroupUri(uri); | |||||
} | } | ||||
/** | /** | ||||
if (afpFont.isEmbeddable()) { | if (afpFont.isEmbeddable()) { | ||||
//Embed fonts (char sets and code pages) | //Embed fonts (char sets and code pages) | ||||
if (charSet.getResourceAccessor() != null) { | if (charSet.getResourceAccessor() != null) { | ||||
ResourceAccessor accessor = charSet.getResourceAccessor(); | |||||
AFPResourceAccessor accessor = charSet.getResourceAccessor(); | |||||
createIncludedResource( | createIncludedResource( | ||||
charSet.getName(), accessor, | charSet.getName(), accessor, | ||||
ResourceObject.TYPE_FONT_CHARACTER_SET); | ResourceObject.TYPE_FONT_CHARACTER_SET); | ||||
* @param resourceObjectType the resource object type ({@link ResourceObject}.*) | * @param resourceObjectType the resource object type ({@link ResourceObject}.*) | ||||
* @throws IOException if an I/O error occurs while loading the resource | * @throws IOException if an I/O error occurs while loading the resource | ||||
*/ | */ | ||||
public void createIncludedResource(String resourceName, ResourceAccessor accessor, | |||||
public void createIncludedResource(String resourceName, AFPResourceAccessor accessor, | |||||
byte resourceObjectType) throws IOException { | byte resourceObjectType) throws IOException { | ||||
URI uri; | URI uri; | ||||
try { | try { | ||||
* @param resourceObjectType the resource object type ({@link ResourceObject}.*) | * @param resourceObjectType the resource object type ({@link ResourceObject}.*) | ||||
* @throws IOException if an I/O error occurs while loading the resource | * @throws IOException if an I/O error occurs while loading the resource | ||||
*/ | */ | ||||
public void createIncludedResource(String resourceName, URI uri, ResourceAccessor accessor, | |||||
public void createIncludedResource(String resourceName, URI uri, AFPResourceAccessor accessor, | |||||
byte resourceObjectType) throws IOException { | byte resourceObjectType) throws IOException { | ||||
AFPResourceLevel resourceLevel = new AFPResourceLevel(AFPResourceLevel.PRINT_FILE); | |||||
AFPResourceLevel resourceLevel = new AFPResourceLevel(ResourceType.PRINT_FILE); | |||||
AFPResourceInfo resourceInfo = new AFPResourceInfo(); | AFPResourceInfo resourceInfo = new AFPResourceInfo(); | ||||
resourceInfo.setLevel(resourceLevel); | resourceInfo.setLevel(resourceLevel); | ||||
* @throws IOException if an I/O error occurs while loading the resource | * @throws IOException if an I/O error occurs while loading the resource | ||||
*/ | */ | ||||
public void createIncludedResourceFromExternal(final String resourceName, | public void createIncludedResourceFromExternal(final String resourceName, | ||||
final URI uri, final ResourceAccessor accessor) throws IOException { | |||||
final URI uri, final AFPResourceAccessor accessor) throws IOException { | |||||
AFPResourceLevel resourceLevel = new AFPResourceLevel(AFPResourceLevel.PRINT_FILE); | |||||
AFPResourceLevel resourceLevel = new AFPResourceLevel(ResourceType.PRINT_FILE); | |||||
AFPResourceInfo resourceInfo = new AFPResourceInfo(); | AFPResourceInfo resourceInfo = new AFPResourceInfo(); | ||||
resourceInfo.setLevel(resourceLevel); | resourceInfo.setLevel(resourceLevel); |
package org.apache.fop.afp; | package org.apache.fop.afp; | ||||
import java.io.BufferedOutputStream; | import java.io.BufferedOutputStream; | ||||
import java.io.File; | |||||
import java.io.FileNotFoundException; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | |||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.io.RandomAccessFile; | |||||
import java.net.URI; | |||||
import java.net.URISyntaxException; | |||||
import java.util.HashMap; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.concurrent.atomic.AtomicLong; | |||||
import org.apache.commons.io.IOUtils; | |||||
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | ||||
import org.apache.commons.logging.LogFactory; | import org.apache.commons.logging.LogFactory; | ||||
import org.apache.fop.afp.modca.ResourceGroup; | import org.apache.fop.afp.modca.ResourceGroup; | ||||
import org.apache.fop.afp.modca.StreamedResourceGroup; | import org.apache.fop.afp.modca.StreamedResourceGroup; | ||||
import org.apache.fop.apps.io.URIResolverWrapper; | |||||
/** | /** | ||||
* Manages the streaming of the AFP output | * Manages the streaming of the AFP output | ||||
/** Static logging instance */ | /** Static logging instance */ | ||||
private static final Log LOG = LogFactory.getLog(AFPStreamer.class); | private static final Log LOG = LogFactory.getLog(AFPStreamer.class); | ||||
private static final String AFPDATASTREAM_TEMP_FILE_PREFIX = "AFPDataStream_"; | |||||
private static final int BUFFER_SIZE = 4096; // 4k writing buffer | |||||
private static final String DEFAULT_EXTERNAL_RESOURCE_FILENAME = "resources.afp"; | private static final String DEFAULT_EXTERNAL_RESOURCE_FILENAME = "resources.afp"; | ||||
private final Factory factory; | private final Factory factory; | ||||
private final URIResolverWrapper uriResolverWrapper; | |||||
/** A mapping of external resource destinations to resource groups */ | /** A mapping of external resource destinations to resource groups */ | ||||
private final Map/*<String,AFPExternalResourceGroup>*/pathResourceGroupMap | |||||
= new java.util.HashMap/*<String,AFPExternalResourceGroup>*/(); | |||||
private final Map<URI, ResourceGroup> pathResourceGroupMap = new HashMap<URI, ResourceGroup>(); | |||||
private StreamedResourceGroup printFileResourceGroup; | private StreamedResourceGroup printFileResourceGroup; | ||||
/** Sets the default resource group file path */ | /** Sets the default resource group file path */ | ||||
private String defaultResourceGroupFilePath = DEFAULT_EXTERNAL_RESOURCE_FILENAME; | |||||
private URI defaultResourceGroupUri; | |||||
private File tempFile; | |||||
private final URI tempUri; | |||||
/** temporary document outputstream */ | /** temporary document outputstream */ | ||||
private OutputStream documentOutputStream; | |||||
private OutputStream tempOutputStream; | |||||
/** the final outputstream */ | /** the final outputstream */ | ||||
private OutputStream outputStream; | private OutputStream outputStream; | ||||
private RandomAccessFile documentFile; | |||||
private DataStream dataStream; | private DataStream dataStream; | ||||
/** | /** | ||||
* | * | ||||
* @param factory a factory | * @param factory a factory | ||||
*/ | */ | ||||
public AFPStreamer(Factory factory) { | |||||
public AFPStreamer(Factory factory, URIResolverWrapper uriResolverWrapper) { | |||||
this.factory = factory; | this.factory = factory; | ||||
this.uriResolverWrapper = uriResolverWrapper; | |||||
this.tempUri = TempUriGenerator.INSTANCE.generate(); | |||||
defaultResourceGroupUri = URI.create(DEFAULT_EXTERNAL_RESOURCE_FILENAME); | |||||
} | } | ||||
/** | /** | ||||
* @throws IOException thrown if an I/O exception of some sort has occurred | * @throws IOException thrown if an I/O exception of some sort has occurred | ||||
*/ | */ | ||||
public DataStream createDataStream(AFPPaintingState paintingState) throws IOException { | public DataStream createDataStream(AFPPaintingState paintingState) throws IOException { | ||||
this.tempFile = File.createTempFile(AFPDATASTREAM_TEMP_FILE_PREFIX, null); | |||||
this.documentFile = new RandomAccessFile(tempFile, "rw"); | |||||
this.documentOutputStream = new BufferedOutputStream( | |||||
new FileOutputStream(documentFile.getFD())); | |||||
this.dataStream = factory.createDataStream(paintingState, documentOutputStream); | |||||
this.tempOutputStream = new BufferedOutputStream(uriResolverWrapper.resolveOut(tempUri)); | |||||
this.dataStream = factory.createDataStream(paintingState, tempOutputStream); | |||||
return dataStream; | return dataStream; | ||||
} | } | ||||
* | * | ||||
* @param filePath the default resource group file path | * @param filePath the default resource group file path | ||||
*/ | */ | ||||
public void setDefaultResourceGroupFilePath(String filePath) { | |||||
this.defaultResourceGroupFilePath = filePath; | |||||
public void setDefaultResourceGroupUri(URI uri) { | |||||
this.defaultResourceGroupUri = uri; | |||||
} | } | ||||
/** | /** | ||||
return null; | return null; | ||||
} | } | ||||
if (level.isExternal()) { | if (level.isExternal()) { | ||||
String filePath = level.getExternalFilePath(); | |||||
if (filePath == null) { | |||||
URI uri = level.getExternalUri(); | |||||
if (uri == null) { | |||||
LOG.warn("No file path provided for external resource, using default."); | LOG.warn("No file path provided for external resource, using default."); | ||||
filePath = defaultResourceGroupFilePath; | |||||
uri = defaultResourceGroupUri; | |||||
} | } | ||||
resourceGroup = (ResourceGroup)pathResourceGroupMap.get(filePath); | |||||
resourceGroup = pathResourceGroupMap.get(uri); | |||||
if (resourceGroup == null) { | if (resourceGroup == null) { | ||||
OutputStream os = null; | OutputStream os = null; | ||||
try { | try { | ||||
os = new BufferedOutputStream(new FileOutputStream(filePath)); | |||||
} catch (FileNotFoundException fnfe) { | |||||
LOG.error("Failed to create/open external resource group file '" | |||||
+ filePath + "'"); | |||||
os = new BufferedOutputStream(uriResolverWrapper.resolveOut(uri)); | |||||
} catch (IOException ioe) { | |||||
LOG.error("Failed to create/open external resource group for uri '" | |||||
+ uri + "'"); | |||||
} finally { | } finally { | ||||
if (os != null) { | if (os != null) { | ||||
resourceGroup = factory.createStreamedResourceGroup(os); | resourceGroup = factory.createStreamedResourceGroup(os); | ||||
pathResourceGroupMap.put(filePath, resourceGroup); | |||||
pathResourceGroupMap.put(uri, resourceGroup); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
* | * | ||||
* @throws IOException if an an I/O exception of some sort has occurred | * @throws IOException if an an I/O exception of some sort has occurred | ||||
*/ | */ | ||||
// write out any external resource groups | |||||
// write out any external resource groups | |||||
public void close() throws IOException { | public void close() throws IOException { | ||||
Iterator it = pathResourceGroupMap.values().iterator(); | Iterator it = pathResourceGroupMap.values().iterator(); | ||||
while (it.hasNext()) { | while (it.hasNext()) { | ||||
StreamedResourceGroup resourceGroup = (StreamedResourceGroup)it.next(); | StreamedResourceGroup resourceGroup = (StreamedResourceGroup)it.next(); | ||||
resourceGroup.close(); | resourceGroup.close(); | ||||
} | } | ||||
// close any open print-file resource group | // close any open print-file resource group | ||||
if (printFileResourceGroup != null) { | if (printFileResourceGroup != null) { | ||||
printFileResourceGroup.close(); | printFileResourceGroup.close(); | ||||
} | } | ||||
// write out document | // write out document | ||||
writeToStream(outputStream); | writeToStream(outputStream); | ||||
outputStream.close(); | outputStream.close(); | ||||
if (documentOutputStream != null) { | |||||
documentOutputStream.close(); | |||||
} | |||||
if (documentFile != null) { | |||||
documentFile.close(); | |||||
} | |||||
// delete temporary file | |||||
tempFile.delete(); | |||||
} | } | ||||
/** | /** | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public void writeToStream(OutputStream os) throws IOException { | public void writeToStream(OutputStream os) throws IOException { | ||||
// long start = System.currentTimeMillis(); | |||||
int len = (int)documentFile.length(); | |||||
int numChunks = len / BUFFER_SIZE; | |||||
int remainingChunkSize = len % BUFFER_SIZE; | |||||
byte[] buffer; | |||||
documentFile.seek(0); | |||||
if (numChunks > 0) { | |||||
buffer = new byte[BUFFER_SIZE]; | |||||
for (int i = 0; i < numChunks; i++) { | |||||
documentFile.read(buffer, 0, BUFFER_SIZE); | |||||
os.write(buffer, 0, BUFFER_SIZE); | |||||
} | |||||
} else { | |||||
buffer = new byte[remainingChunkSize]; | |||||
tempOutputStream.close(); | |||||
InputStream tempInputStream = uriResolverWrapper.resolveIn(tempUri); | |||||
IOUtils.copy(tempInputStream, os); | |||||
//TODO this should notify the stream provider that it is safe to delete the temp data | |||||
tempInputStream.close(); | |||||
os.flush(); | |||||
} | |||||
private static final class TempUriGenerator { | |||||
private static final TempUriGenerator INSTANCE = new TempUriGenerator(); | |||||
private static final String AFPDATASTREAM_TEMP_URL_PREFIX = "tmp:///AFPDataStream_"; | |||||
private final AtomicLong counter; | |||||
private TempUriGenerator() { | |||||
counter = new AtomicLong(); | |||||
} | } | ||||
if (remainingChunkSize > 0) { | |||||
documentFile.read(buffer, 0, remainingChunkSize); | |||||
os.write(buffer, 0, remainingChunkSize); | |||||
public URI generate() { | |||||
try { | |||||
return new URI(AFPDATASTREAM_TEMP_URL_PREFIX + counter.getAndIncrement()); | |||||
} catch (URISyntaxException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
} | } | ||||
os.flush(); | |||||
// long end = System.currentTimeMillis(); | |||||
// log.debug("writing time " + (end - start) + "ms"); | |||||
} | } | ||||
} | } |
import org.apache.fop.afp.AFPConstants; | import org.apache.fop.afp.AFPConstants; | ||||
import org.apache.fop.afp.AFPEventProducer; | import org.apache.fop.afp.AFPEventProducer; | ||||
import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars; | import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars; | ||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.afp.util.StringUtils; | import org.apache.fop.afp.util.StringUtils; | ||||
/** | /** | ||||
protected final String name; | protected final String name; | ||||
/** The path to the installed fonts */ | /** The path to the installed fonts */ | ||||
private final ResourceAccessor accessor; | |||||
private final AFPResourceAccessor accessor; | |||||
/** The current orientation (currently only 0 is supported by FOP) */ | /** The current orientation (currently only 0 is supported by FOP) */ | ||||
private final String currentOrientation = "0"; | private final String currentOrientation = "0"; | ||||
* @param eventProducer for handling AFP related events | * @param eventProducer for handling AFP related events | ||||
*/ | */ | ||||
CharacterSet(String codePage, String encoding, CharacterSetType charsetType, String name, | CharacterSet(String codePage, String encoding, CharacterSetType charsetType, String name, | ||||
ResourceAccessor accessor, AFPEventProducer eventProducer) { | |||||
AFPResourceAccessor accessor, AFPEventProducer eventProducer) { | |||||
if (name.length() > MAX_NAME_LEN) { | if (name.length() > MAX_NAME_LEN) { | ||||
String msg = "Character set name '" + name + "' must be a maximum of " | String msg = "Character set name '" + name + "' must be a maximum of " | ||||
+ MAX_NAME_LEN + " characters"; | + MAX_NAME_LEN + " characters"; | ||||
* Returns the resource accessor to load the font resources with. | * Returns the resource accessor to load the font resources with. | ||||
* @return the resource accessor to load the font resources with | * @return the resource accessor to load the font resources with | ||||
*/ | */ | ||||
public ResourceAccessor getResourceAccessor() { | |||||
public AFPResourceAccessor getResourceAccessor() { | |||||
return this.accessor; | return this.accessor; | ||||
} | } | ||||
package org.apache.fop.afp.fonts; | package org.apache.fop.afp.fonts; | ||||
import java.io.FileNotFoundException; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.net.MalformedURLException; | |||||
import java.net.URI; | import java.net.URI; | ||||
import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import org.apache.fop.afp.AFPConstants; | import org.apache.fop.afp.AFPConstants; | ||||
import org.apache.fop.afp.AFPEventProducer; | import org.apache.fop.afp.AFPEventProducer; | ||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.afp.util.StructuredFieldReader; | import org.apache.fop.afp.util.StructuredFieldReader; | ||||
import org.apache.fop.fonts.Typeface; | import org.apache.fop.fonts.Typeface; | ||||
* Returns an InputStream to a given file path and filename | * Returns an InputStream to a given file path and filename | ||||
* | * | ||||
* * @param accessor the resource accessor | * * @param accessor the resource accessor | ||||
* @param filename the file name | |||||
* @param uriStr the URI | |||||
* @param eventProducer for handling AFP related events | * @param eventProducer for handling AFP related events | ||||
* @return an inputStream | * @return an inputStream | ||||
* | |||||
* @throws IOException in the event that an I/O exception of some sort has occurred | * @throws IOException in the event that an I/O exception of some sort has occurred | ||||
*/ | */ | ||||
protected InputStream openInputStream(ResourceAccessor accessor, String filename, | |||||
protected InputStream openInputStream(AFPResourceAccessor accessor, String uriStr, | |||||
AFPEventProducer eventProducer) | AFPEventProducer eventProducer) | ||||
throws IOException { | throws IOException { | ||||
URI uri; | URI uri; | ||||
try { | try { | ||||
uri = new URI(filename.trim()); | |||||
uri = new URI(uriStr.trim()); | |||||
} catch (URISyntaxException e) { | } catch (URISyntaxException e) { | ||||
throw new FileNotFoundException("Invalid filename: " | |||||
+ filename + " (" + e.getMessage() + ")"); | |||||
throw new MalformedURLException("Invalid uri: " + uriStr + " (" + e.getMessage() + ")"); | |||||
} | } | ||||
if (LOG.isDebugEnabled()) { | if (LOG.isDebugEnabled()) { | ||||
LOG.debug("Opening " + uri); | LOG.debug("Opening " + uri); | ||||
} | } | ||||
InputStream inputStream = accessor.createInputStream(uri); | |||||
return inputStream; | |||||
return accessor.createInputStream(uri); | |||||
} | } | ||||
/** | /** | ||||
* @throws IOException if an I/O error occurs | * @throws IOException if an I/O error occurs | ||||
*/ | */ | ||||
public CharacterSet buildSBCS(String characterSetName, String codePageName, String encoding, | public CharacterSet buildSBCS(String characterSetName, String codePageName, String encoding, | ||||
ResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
return processFont(characterSetName, codePageName, encoding, CharacterSetType.SINGLE_BYTE, | return processFont(characterSetName, codePageName, encoding, CharacterSetType.SINGLE_BYTE, | ||||
accessor, eventProducer); | accessor, eventProducer); | ||||
} | } | ||||
* @throws IOException if an I/O error occurs | * @throws IOException if an I/O error occurs | ||||
*/ | */ | ||||
public CharacterSet buildDBCS(String characterSetName, String codePageName, String encoding, | public CharacterSet buildDBCS(String characterSetName, String codePageName, String encoding, | ||||
CharacterSetType charsetType, ResourceAccessor accessor, AFPEventProducer eventProducer) | |||||
CharacterSetType charsetType, AFPResourceAccessor accessor, AFPEventProducer eventProducer) | |||||
throws IOException { | throws IOException { | ||||
return processFont(characterSetName, codePageName, encoding, charsetType, accessor, | return processFont(characterSetName, codePageName, encoding, charsetType, accessor, | ||||
eventProducer); | eventProducer); | ||||
} | } | ||||
private CharacterSet processFont(String characterSetName, String codePageName, String encoding, | private CharacterSet processFont(String characterSetName, String codePageName, String encoding, | ||||
CharacterSetType charsetType, ResourceAccessor accessor, AFPEventProducer eventProducer) | |||||
CharacterSetType charsetType, AFPResourceAccessor accessor, AFPEventProducer eventProducer) | |||||
throws IOException { | throws IOException { | ||||
// check for cached version of the characterset | // check for cached version of the characterset | ||||
String descriptor = characterSetName + "_" + encoding + "_" + codePageName; | String descriptor = characterSetName + "_" + encoding + "_" + codePageName; | ||||
* @throws IOException if an I/O exception of some sort has occurred. | * @throws IOException if an I/O exception of some sort has occurred. | ||||
*/ | */ | ||||
protected Map<String, String> loadCodePage(String codePage, String encoding, | protected Map<String, String> loadCodePage(String codePage, String encoding, | ||||
ResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
// Create the HashMap to store code page information | // Create the HashMap to store code page information | ||||
Map<String, String> codePages = new HashMap<String, String>(); | Map<String, String> codePages = new HashMap<String, String>(); | ||||
InputStream inputStream = null; | InputStream inputStream = null; | ||||
try { | try { | ||||
inputStream = openInputStream(accessor, codePage.trim(), eventProducer); | inputStream = openInputStream(accessor, codePage.trim(), eventProducer); | ||||
} catch (IOException e) { | |||||
eventProducer.codePageNotFound(this, e); | |||||
throw e; | |||||
} | |||||
try { | |||||
StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream); | StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream); | ||||
byte[] data = structuredFieldReader.getNext(CHARACTER_TABLE_SF); | byte[] data = structuredFieldReader.getNext(CHARACTER_TABLE_SF); | ||||
position++; | position++; | ||||
} | } | ||||
} | } | ||||
} catch (FileNotFoundException e) { | |||||
eventProducer.codePageNotFound(this, e); | |||||
} finally { | } finally { | ||||
closeInputStream(inputStream); | closeInputStream(inputStream); | ||||
} | } | ||||
} | } | ||||
protected Map<String, String> loadCodePage(String codePage, String encoding, | protected Map<String, String> loadCodePage(String codePage, String encoding, | ||||
ResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException { | |||||
// Create the HashMap to store code page information | // Create the HashMap to store code page information | ||||
Map<String, String> codePages = new HashMap<String, String>(); | Map<String, String> codePages = new HashMap<String, String>(); | ||||
InputStream inputStream = null; | InputStream inputStream = null; | ||||
try { | try { | ||||
inputStream = openInputStream(accessor, codePage.trim(), eventProducer); | inputStream = openInputStream(accessor, codePage.trim(), eventProducer); | ||||
StructuredFieldReader structuredFieldReader | |||||
= new StructuredFieldReader(inputStream); | |||||
} catch (IOException e) { | |||||
eventProducer.codePageNotFound(this, e); | |||||
throw e; | |||||
} | |||||
try { | |||||
StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream); | |||||
byte[] data; | byte[] data; | ||||
while ((data = structuredFieldReader.getNext(CHARACTER_TABLE_SF)) != null) { | while ((data = structuredFieldReader.getNext(CHARACTER_TABLE_SF)) != null) { | ||||
int position = 0; | int position = 0; | ||||
byte[] gcgiBytes = new byte[8]; | byte[] gcgiBytes = new byte[8]; | ||||
byte[] charBytes = new byte[2]; | byte[] charBytes = new byte[2]; | ||||
// Read data, ignoring bytes 0 - 2 | // Read data, ignoring bytes 0 - 2 | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} catch (FileNotFoundException e) { | |||||
eventProducer.codePageNotFound(this, e); | |||||
} finally { | } finally { | ||||
closeInputStream(inputStream); | closeInputStream(inputStream); | ||||
} | } | ||||
return codePages; | return codePages; | ||||
} | } | ||||
package org.apache.fop.afp.fonts; | package org.apache.fop.afp.fonts; | ||||
import org.apache.fop.afp.AFPEventProducer; | import org.apache.fop.afp.AFPEventProducer; | ||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.fonts.Typeface; | import org.apache.fop.fonts.Typeface; | ||||
/** | /** | ||||
*/ | */ | ||||
public FopCharacterSet(String codePage, String encoding, String name, Typeface charSet, | public FopCharacterSet(String codePage, String encoding, String name, Typeface charSet, | ||||
AFPEventProducer eventProducer) { | AFPEventProducer eventProducer) { | ||||
super(codePage, encoding, CharacterSetType.SINGLE_BYTE, name, (ResourceAccessor) null, | |||||
super(codePage, encoding, CharacterSetType.SINGLE_BYTE, name, (AFPResourceAccessor) null, | |||||
eventProducer); | eventProducer); | ||||
this.charSet = charSet; | this.charSet = charSet; | ||||
} | } |
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import org.apache.fop.afp.util.AFPResourceUtil; | import org.apache.fop.afp.util.AFPResourceUtil; | ||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
/** | /** | ||||
*/ | */ | ||||
public class IncludedResourceObject extends AbstractNamedAFPObject { | public class IncludedResourceObject extends AbstractNamedAFPObject { | ||||
private ResourceAccessor resourceAccessor; | |||||
private AFPResourceAccessor resourceAccessor; | |||||
private URI uri; | private URI uri; | ||||
/** | /** | ||||
* @param uri the URI of the external file | * @param uri the URI of the external file | ||||
*/ | */ | ||||
public IncludedResourceObject(String name, | public IncludedResourceObject(String name, | ||||
ResourceAccessor resourceAccessor, URI uri) { | |||||
AFPResourceAccessor resourceAccessor, URI uri) { | |||||
super(name); | super(name); | ||||
this.resourceAccessor = resourceAccessor; | this.resourceAccessor = resourceAccessor; | ||||
this.uri = uri; | this.uri = uri; |
import java.net.URI; | import java.net.URI; | ||||
import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
/** | /** | ||||
* Default implementation of the {@link ResourceAccessor} interface for use inside FOP. | |||||
* Defines an interface through which external resource objects can be accessed. | |||||
*/ | */ | ||||
public class DefaultFOPResourceAccessor extends SimpleResourceAccessor { | |||||
public final class AFPResourceAccessor { | |||||
private final URIResolverWrapper resolver; | private final URIResolverWrapper resolver; | ||||
private final String baseURI; | private final String baseURI; | ||||
* @param categoryBaseURI the category base URI (may be null) | * @param categoryBaseURI the category base URI (may be null) | ||||
* @param baseURI the custom base URI to resolve relative URIs against (may be null) | * @param baseURI the custom base URI to resolve relative URIs against (may be null) | ||||
*/ | */ | ||||
public DefaultFOPResourceAccessor(URIResolverWrapper resolver, String baseURI) { | |||||
super(resolver.getBaseURI()); | |||||
public AFPResourceAccessor(URIResolverWrapper resolver, String baseURI) { | |||||
this.resolver = resolver; | this.resolver = resolver; | ||||
this.baseURI = baseURI; | this.baseURI = baseURI; | ||||
} | } | ||||
public DefaultFOPResourceAccessor(URIResolverWrapper resolver) { | |||||
super(resolver.getBaseURI()); | |||||
public AFPResourceAccessor(URIResolverWrapper resolver) { | |||||
this.resolver = resolver; | this.resolver = resolver; | ||||
this.baseURI = null; | this.baseURI = null; | ||||
} | } | ||||
public InputStream createInputStream(URI uri) throws IOException { | public InputStream createInputStream(URI uri) throws IOException { | ||||
return resolver.resolveIn(getResourceURI(uri)); | return resolver.resolveIn(getResourceURI(uri)); | ||||
} | } | ||||
} | } |
/* | |||||
* 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.util; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.net.URI; | |||||
import java.net.URL; | |||||
/** | |||||
* Simple implementation of the {@link ResourceAccessor} interface for access relative to a | |||||
* base URI. | |||||
*/ | |||||
public class SimpleResourceAccessor implements ResourceAccessor { | |||||
private URI baseURI; | |||||
/** | |||||
* Creates a new simple resource accessor. | |||||
* @param baseURI the base URI to resolve relative URIs against (may be null) | |||||
*/ | |||||
public SimpleResourceAccessor(URI baseURI) { | |||||
this.baseURI = baseURI; | |||||
} | |||||
/** | |||||
* Creates a new simple resource accessor. | |||||
* @param baseDir the base directory to resolve relative filenames against (may be null) | |||||
*/ | |||||
public SimpleResourceAccessor(File baseDir) { | |||||
this(baseDir != null ? baseDir.toURI() : null); | |||||
} | |||||
/** | |||||
* Returns the base URI. | |||||
* @return the base URI (or null if no base URI was set) | |||||
*/ | |||||
public URI getBaseURI() { | |||||
return this.baseURI; | |||||
} | |||||
/** | |||||
* Resolve the given URI against the baseURI. | |||||
* @param uri the URI to resolve | |||||
* @return the resolved URI | |||||
*/ | |||||
protected URI resolveAgainstBase(URI uri) { | |||||
return (getBaseURI() != null ? getBaseURI().resolve(uri) : uri); | |||||
} | |||||
/** {@inheritDoc} */ | |||||
public InputStream createInputStream(URI uri) throws IOException { | |||||
URI resolved = resolveAgainstBase(uri); | |||||
URL url = resolved.toURL(); | |||||
return url.openStream(); | |||||
} | |||||
} |
import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry; | import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry; | ||||
import org.apache.xmlgraphics.image.loader.util.Penalty; | import org.apache.xmlgraphics.image.loader.util.Penalty; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolver; | import org.apache.fop.apps.io.ResourceResolver; | ||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import org.apache.fop.fonts.FontManagerConfigurator; | import org.apache.fop.fonts.FontManagerConfigurator; | ||||
import org.apache.fop.hyphenation.HyphenationTreeCache; | import org.apache.fop.hyphenation.HyphenationTreeCache; | ||||
*/ | */ | ||||
public FopConfParser(InputStream fopConfStream, URI defaultBaseURI) throws SAXException, | public FopConfParser(InputStream fopConfStream, URI defaultBaseURI) throws SAXException, | ||||
IOException { | IOException { | ||||
this(fopConfStream, defaultBaseURI, new DefaultResourceResolver()); | |||||
this(fopConfStream, defaultBaseURI, ResourceResolverFactory.createDefaultResourceResolver()); | |||||
} | } | ||||
/** | /** | ||||
* @throws IOException if an I/O error is thrown while parsing the FOP conf | * @throws IOException if an I/O error is thrown while parsing the FOP conf | ||||
*/ | */ | ||||
public FopConfParser(File fopConfFile) throws SAXException, IOException { | public FopConfParser(File fopConfFile) throws SAXException, IOException { | ||||
this(fopConfFile, new DefaultResourceResolver()); | |||||
this(fopConfFile, ResourceResolverFactory.createDefaultResourceResolver()); | |||||
} | } | ||||
/** | /** |
import org.apache.xmlgraphics.image.loader.ImageContext; | import org.apache.xmlgraphics.image.loader.ImageContext; | ||||
import org.apache.xmlgraphics.image.loader.ImageManager; | import org.apache.xmlgraphics.image.loader.ImageManager; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.FOURIResolver; | import org.apache.fop.apps.io.FOURIResolver; | ||||
import org.apache.fop.apps.io.ResourceResolver; | import org.apache.fop.apps.io.ResourceResolver; | ||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.fonts.FontManager; | import org.apache.fop.fonts.FontManager; | ||||
import org.apache.fop.hyphenation.HyphenationTreeResolver; | import org.apache.fop.hyphenation.HyphenationTreeResolver; | ||||
import org.apache.fop.layoutmgr.LayoutManagerMaker; | import org.apache.fop.layoutmgr.LayoutManagerMaker; | ||||
* @param defaultBaseURI the default base URI for resolving URIs against | * @param defaultBaseURI the default base URI for resolving URIs against | ||||
*/ | */ | ||||
public FopFactoryBuilder(URI defaultBaseURI) { | public FopFactoryBuilder(URI defaultBaseURI) { | ||||
this(defaultBaseURI, new DefaultResourceResolver()); | |||||
this(defaultBaseURI, ResourceResolverFactory.createDefaultResourceResolver()); | |||||
} | } | ||||
/** | /** |
/* | |||||
* 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.apps.io; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
import java.net.MalformedURLException; | |||||
import java.net.URI; | |||||
public class DefaultResourceResolver implements ResourceResolver { | |||||
public Resource getResource(URI uri) throws IOException { | |||||
try { | |||||
return new Resource(uri.toURL().openStream()); | |||||
} catch (MalformedURLException mue) { | |||||
throw new RuntimeException(mue); | |||||
} | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
throw new UnsupportedOperationException(); | |||||
} | |||||
public static URIResolverWrapper createDefaultWrapper() { | |||||
// Not sure if this is the right place for this, but I don't have any better ideas as of yet | |||||
URI thisUri = new File(".").getAbsoluteFile().toURI(); | |||||
return new URIResolverWrapper(thisUri, new DefaultResourceResolver()); | |||||
} | |||||
} |
/* | |||||
* 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.apps.io; | |||||
import java.io.File; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
import java.net.URI; | |||||
import java.util.Collections; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
public final class ResourceResolverFactory { | |||||
private ResourceResolverFactory() { | |||||
} | |||||
public static ResourceResolver createDefaultResourceResolver() { | |||||
return DefaultResourceResolver.INSTANCE; | |||||
} | |||||
public static ResourceResolver createTempAwareResourceResolver(TempResourceResolver tempResourceResolver, | |||||
ResourceResolver defaultResourceResolver) { | |||||
return new TempAwareResourceResolver(tempResourceResolver, defaultResourceResolver); | |||||
} | |||||
public static URIResolverWrapper createDefaultWrapper() { | |||||
// Not sure if this is the right place for this, but I don't have any better ideas as of yet | |||||
URI thisUri = new File(".").getAbsoluteFile().toURI(); | |||||
return new URIResolverWrapper(thisUri, new DefaultResourceResolver()); | |||||
} | |||||
public static SchemaAwareResourceResolverBuilder createSchemaAwareResourceResolverBuilder( | |||||
ResourceResolver defaultResolver) { | |||||
return new SchemaAwareResourceResolverBuilderImpl(defaultResolver); | |||||
} | |||||
private static final class DefaultResourceResolver implements ResourceResolver { | |||||
private static final ResourceResolver INSTANCE = new DefaultResourceResolver(); | |||||
private final TempAwareResourceResolver delegate; | |||||
private DefaultResourceResolver() { | |||||
delegate = new TempAwareResourceResolver(new DefaultTempResourceResolver(), | |||||
new NormalResourceResolver()); | |||||
} | |||||
public Resource getResource(URI uri) throws IOException { | |||||
return delegate.getResource(uri); | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
return delegate.getOutputStream(uri); | |||||
} | |||||
} | |||||
private static final class TempAwareResourceResolver implements ResourceResolver { | |||||
private final TempResourceResolver tempResourceResolver; | |||||
private final ResourceResolver defaultResourceResolver; | |||||
public TempAwareResourceResolver(TempResourceResolver tempResourceHandler, | |||||
ResourceResolver defaultResourceResolver) { | |||||
this.tempResourceResolver = tempResourceHandler; | |||||
this.defaultResourceResolver = defaultResourceResolver; | |||||
} | |||||
private static boolean isTempUri(URI uri) { | |||||
return "tmp".equals(uri.getScheme()); | |||||
} | |||||
public Resource getResource(URI uri) throws IOException { | |||||
if (isTempUri(uri)) { | |||||
return tempResourceResolver.getResource(uri.getPath()); | |||||
} else { | |||||
return defaultResourceResolver.getResource(uri); | |||||
} | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
if (isTempUri(uri)) { | |||||
return tempResourceResolver.getOutputStream(uri.getPath()); | |||||
} else { | |||||
return defaultResourceResolver.getOutputStream(uri); | |||||
} | |||||
} | |||||
} | |||||
private static class DefaultTempResourceResolver implements TempResourceResolver { | |||||
private static File getTempFile(String path) throws IOException { | |||||
File file = new File(System.getProperty("java.io.tmpdir"), path); | |||||
file.deleteOnExit(); | |||||
return file; | |||||
} | |||||
public Resource getResource(String id) throws IOException { | |||||
return new Resource(getTempFile(id).toURI().toURL().openStream()); | |||||
} | |||||
public OutputStream getOutputStream(String id) throws IOException { | |||||
File file = getTempFile(id); | |||||
// TODO handle error | |||||
file.createNewFile(); | |||||
return new FileOutputStream(file); | |||||
} | |||||
} | |||||
private static class NormalResourceResolver implements ResourceResolver { | |||||
public Resource getResource(URI uri) throws IOException { | |||||
return new Resource(uri.toURL().openStream()); | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
return new FileOutputStream(new File(uri)); | |||||
} | |||||
} | |||||
private static final class SchemaAwareResourceResolver implements ResourceResolver { | |||||
private final Map<String, ResourceResolver> schemaHandlingResourceResolvers; | |||||
private final ResourceResolver defaultResolver; | |||||
private SchemaAwareResourceResolver(Map<String, ResourceResolver> schemaHandlingResourceResolvers, | |||||
ResourceResolver defaultResolver) { | |||||
this.schemaHandlingResourceResolvers = schemaHandlingResourceResolvers; | |||||
this.defaultResolver = defaultResolver; | |||||
} | |||||
private ResourceResolver getResourceResolverForSchema(URI uri) { | |||||
String schema = uri.getScheme(); | |||||
if (schemaHandlingResourceResolvers.containsKey(schema)) { | |||||
return schemaHandlingResourceResolvers.get(schema); | |||||
} else { | |||||
return defaultResolver; | |||||
} | |||||
} | |||||
public Resource getResource(URI uri) throws IOException { | |||||
return getResourceResolverForSchema(uri).getResource(uri); | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
return getResourceResolverForSchema(uri).getOutputStream(uri); | |||||
} | |||||
} | |||||
public interface SchemaAwareResourceResolverBuilder { | |||||
void registerResourceResolverForSchema(String schema, ResourceResolver resourceResolver); | |||||
ResourceResolver build(); | |||||
} | |||||
private static final class CompletedSchemaAwareResourceResolverBuilder | |||||
implements SchemaAwareResourceResolverBuilder { | |||||
private static final SchemaAwareResourceResolverBuilder INSTANCE | |||||
= new CompletedSchemaAwareResourceResolverBuilder(); | |||||
public ResourceResolver build() { | |||||
throw new IllegalStateException("Resource resolver already built"); | |||||
} | |||||
public void registerResourceResolverForSchema(String schema, | |||||
ResourceResolver resourceResolver) { | |||||
throw new IllegalStateException("Resource resolver already built"); | |||||
} | |||||
} | |||||
private static final class ActiveSchemaAwareResourceResolverBuilder | |||||
implements SchemaAwareResourceResolverBuilder { | |||||
private final Map<String, ResourceResolver> schemaHandlingResourceResolvers | |||||
= new HashMap<String, ResourceResolver>(); | |||||
private final ResourceResolver defaultResolver; | |||||
private ActiveSchemaAwareResourceResolverBuilder(ResourceResolver defaultResolver) { | |||||
this.defaultResolver = defaultResolver; | |||||
} | |||||
public void registerResourceResolverForSchema(String schema, ResourceResolver resourceResolver) { | |||||
schemaHandlingResourceResolvers.put(schema, resourceResolver); | |||||
} | |||||
public ResourceResolver build() { | |||||
return new SchemaAwareResourceResolver( | |||||
Collections.unmodifiableMap(schemaHandlingResourceResolvers), defaultResolver); | |||||
} | |||||
} | |||||
private static final class SchemaAwareResourceResolverBuilderImpl | |||||
implements SchemaAwareResourceResolverBuilder { | |||||
private SchemaAwareResourceResolverBuilder delegate; | |||||
private SchemaAwareResourceResolverBuilderImpl(ResourceResolver defaultResolver) { | |||||
this.delegate = new ActiveSchemaAwareResourceResolverBuilder(defaultResolver); | |||||
} | |||||
public void registerResourceResolverForSchema(String schema, ResourceResolver resourceResolver) { | |||||
delegate.registerResourceResolverForSchema(schema, resourceResolver); | |||||
} | |||||
public ResourceResolver build() { | |||||
ResourceResolver resourceResolver = delegate.build(); | |||||
delegate = CompletedSchemaAwareResourceResolverBuilder.INSTANCE; | |||||
return resourceResolver; | |||||
} | |||||
} | |||||
} |
/* $Id$ */ | /* $Id$ */ | ||||
package org.apache.fop.afp.util; | |||||
package org.apache.fop.apps.io; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | |||||
import java.net.URI; | |||||
import java.io.OutputStream; | |||||
/** | |||||
* Defines an interface through which external resource objects can be accessed. | |||||
*/ | |||||
public interface ResourceAccessor { | |||||
public interface TempResourceResolver { | |||||
/** | |||||
* Creates a new {@link InputStream} for the given URI that allows read access to an external | |||||
* resource. | |||||
* @param uri the URI of an external resource. | |||||
* @return the new input stream | |||||
* @throws IOException if an I/O error occurs while opening the resource | |||||
*/ | |||||
InputStream createInputStream(URI uri) throws IOException; | |||||
Resource getResource(String id) throws IOException; | |||||
OutputStream getOutputStream(String id) throws IOException; | |||||
} | } |
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.util.LinkedHashMap; | import java.util.LinkedHashMap; | ||||
import java.util.LinkedHashSet; | import java.util.LinkedHashSet; | ||||
import java.util.LinkedHashMap; | |||||
import java.util.LinkedHashSet; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Set; | import java.util.Set; | ||||
package org.apache.fop.render.afp; | package org.apache.fop.render.afp; | ||||
import java.net.URI; | |||||
import org.apache.fop.afp.AFPResourceLevelDefaults; | import org.apache.fop.afp.AFPResourceLevelDefaults; | ||||
/** | /** | ||||
boolean isStrokeGOCAText(); | boolean isStrokeGOCAText(); | ||||
/** | /** | ||||
* Sets the default resource group file path | |||||
* @param filePath the default resource group file path | |||||
* Sets the default resource group URI | |||||
* @param uri the default resource group URI | |||||
*/ | */ | ||||
void setDefaultResourceGroupFilePath(String filePath); | |||||
void setDefaultResourceGroupUri(URI uri); | |||||
/** | /** | ||||
* Sets the resource level defaults. The object passed in provides information which resource | * Sets the resource level defaults. The object passed in provides information which resource |
import java.awt.Dimension; | import java.awt.Dimension; | ||||
import java.awt.geom.AffineTransform; | import java.awt.geom.AffineTransform; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.net.URI; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.fop.afp.fonts.AFPFontCollection; | import org.apache.fop.afp.fonts.AFPFontCollection; | ||||
import org.apache.fop.afp.fonts.AFPPageFonts; | import org.apache.fop.afp.fonts.AFPPageFonts; | ||||
import org.apache.fop.afp.modca.ResourceObject; | import org.apache.fop.afp.modca.ResourceObject; | ||||
import org.apache.fop.afp.util.DefaultFOPResourceAccessor; | |||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.fonts.FontCollection; | import org.apache.fop.fonts.FontCollection; | ||||
import org.apache.fop.fonts.FontEventAdapter; | import org.apache.fop.fonts.FontEventAdapter; | ||||
*/ | */ | ||||
public AFPDocumentHandler(IFContext context) { | public AFPDocumentHandler(IFContext context) { | ||||
super(context); | super(context); | ||||
this.resourceManager = new AFPResourceManager(); | |||||
this.resourceManager = new AFPResourceManager(context.getUserAgent().getNewURIResolver()); | |||||
this.paintingState = new AFPPaintingState(); | this.paintingState = new AFPPaintingState(); | ||||
this.unitConv = paintingState.getUnitConverter(); | this.unitConv = paintingState.getUnitConverter(); | ||||
} | } | ||||
} | } | ||||
} else if (extension instanceof AFPIncludeFormMap) { | } else if (extension instanceof AFPIncludeFormMap) { | ||||
AFPIncludeFormMap formMap = (AFPIncludeFormMap)extension; | AFPIncludeFormMap formMap = (AFPIncludeFormMap)extension; | ||||
ResourceAccessor accessor = new DefaultFOPResourceAccessor( | |||||
AFPResourceAccessor accessor = new AFPResourceAccessor( | |||||
getUserAgent().getNewURIResolver()); | getUserAgent().getNewURIResolver()); | ||||
try { | try { | ||||
getResourceManager().createIncludedResource(formMap.getName(), | getResourceManager().createIncludedResource(formMap.getName(), | ||||
return paintingState.getFS45(); | return paintingState.getFS45(); | ||||
} | } | ||||
/** {@inheritDoc} */ | |||||
public void setDefaultResourceGroupFilePath(String filePath) { | |||||
resourceManager.setDefaultResourceGroupFilePath(filePath); | |||||
public void setDefaultResourceGroupUri(URI uri) { | |||||
resourceManager.setDefaultResourceGroupUri(uri); | |||||
} | } | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ |
import org.apache.fop.afp.fonts.DoubleByteFont; | import org.apache.fop.afp.fonts.DoubleByteFont; | ||||
import org.apache.fop.afp.fonts.OutlineFont; | import org.apache.fop.afp.fonts.OutlineFont; | ||||
import org.apache.fop.afp.fonts.RasterFont; | import org.apache.fop.afp.fonts.RasterFont; | ||||
import org.apache.fop.afp.util.DefaultFOPResourceAccessor; | |||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import org.apache.fop.events.EventProducer; | import org.apache.fop.events.EventProducer; | ||||
abstract AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) | abstract AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) | ||||
throws IOException; | throws IOException; | ||||
ResourceAccessor getAccessor(URIResolverWrapper resolver) { | |||||
return new DefaultFOPResourceAccessor(resolver, uri); | |||||
AFPResourceAccessor getAccessor(URIResolverWrapper resolver) { | |||||
return new AFPResourceAccessor(resolver, uri); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) | AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) | ||||
throws IOException { | throws IOException { | ||||
ResourceAccessor accessor = getAccessor(resolver); | |||||
AFPResourceAccessor accessor = getAccessor(resolver); | |||||
CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS( | CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS( | ||||
characterset, super.codePage, super.encoding, charsetType, accessor, eventProducer); | characterset, super.codePage, super.encoding, charsetType, accessor, eventProducer); | ||||
return getFontInfo(new DoubleByteFont(super.codePage, super.embeddable, characterSet), | return getFontInfo(new DoubleByteFont(super.codePage, super.embeddable, characterSet), | ||||
LOG.error(msg); | LOG.error(msg); | ||||
} | } | ||||
} else { | } else { | ||||
ResourceAccessor accessor = getAccessor(resolver); | |||||
AFPResourceAccessor accessor = getAccessor(resolver); | |||||
characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS( | characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS( | ||||
characterset, super.codePage, super.encoding, accessor, eventProducer); | characterset, super.codePage, super.encoding, accessor, eventProducer); | ||||
} | } | ||||
LOG.error(msg); | LOG.error(msg); | ||||
} | } | ||||
} else { | } else { | ||||
ResourceAccessor accessor = getAccessor(resolver); | |||||
AFPResourceAccessor accessor = getAccessor(resolver); | |||||
rasterFont.addCharacterSet(charset.size, | rasterFont.addCharacterSet(charset.size, | ||||
CharacterSetBuilder.getSingleByteInstance().buildSBCS(charset.characterset, | CharacterSetBuilder.getSingleByteInstance().buildSBCS(charset.characterset, | ||||
super.codePage, super.encoding, accessor, eventProducer)); | super.codePage, super.encoding, accessor, eventProducer)); |
package org.apache.fop.render.afp; | package org.apache.fop.render.afp; | ||||
import java.io.File; | |||||
import java.net.URI; | |||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | ||||
AFPElementMapping.NAMESPACE, "afp:resource-level"); | AFPElementMapping.NAMESPACE, "afp:resource-level"); | ||||
/** the resource-group-file attribute */ | /** the resource-group-file attribute */ | ||||
public static final QName RESOURCE_GROUP_FILE = new QName( | |||||
public static final QName RESOURCE_GROUP_URI = new QName( | |||||
AFPElementMapping.NAMESPACE, "afp:resource-group-file"); | AFPElementMapping.NAMESPACE, "afp:resource-group-file"); | ||||
/** | /** | ||||
public AFPResourceInfo getResourceInfo(Map/*<QName, String>*/ foreignAttributes) { | public AFPResourceInfo getResourceInfo(Map/*<QName, String>*/ foreignAttributes) { | ||||
AFPResourceInfo resourceInfo = new AFPResourceInfo(); | AFPResourceInfo resourceInfo = new AFPResourceInfo(); | ||||
if (foreignAttributes != null && !foreignAttributes.isEmpty()) { | if (foreignAttributes != null && !foreignAttributes.isEmpty()) { | ||||
String resourceName = (String)foreignAttributes.get(RESOURCE_NAME); | |||||
String resourceName = (String) foreignAttributes.get(RESOURCE_NAME); | |||||
if (resourceName != null) { | if (resourceName != null) { | ||||
resourceInfo.setName(resourceName); | resourceInfo.setName(resourceName); | ||||
} | } | ||||
* @param foreignAttributes the foreign attributes | * @param foreignAttributes the foreign attributes | ||||
* @return the resource level | * @return the resource level | ||||
*/ | */ | ||||
public AFPResourceLevel getResourceLevel(Map/*<QName, String>*/ foreignAttributes) { | |||||
public AFPResourceLevel getResourceLevel(Map<QName, String> foreignAttributes) { | |||||
AFPResourceLevel resourceLevel = null; | AFPResourceLevel resourceLevel = null; | ||||
if (foreignAttributes != null && !foreignAttributes.isEmpty()) { | if (foreignAttributes != null && !foreignAttributes.isEmpty()) { | ||||
if (foreignAttributes.containsKey(RESOURCE_LEVEL)) { | if (foreignAttributes.containsKey(RESOURCE_LEVEL)) { | ||||
String levelString = (String)foreignAttributes.get(RESOURCE_LEVEL); | |||||
String levelString = foreignAttributes.get(RESOURCE_LEVEL); | |||||
resourceLevel = AFPResourceLevel.valueOf(levelString); | resourceLevel = AFPResourceLevel.valueOf(levelString); | ||||
// if external get resource group file attributes | // if external get resource group file attributes | ||||
if (resourceLevel != null && resourceLevel.isExternal()) { | if (resourceLevel != null && resourceLevel.isExternal()) { | ||||
String resourceGroupFile | |||||
= (String)foreignAttributes.get(RESOURCE_GROUP_FILE); | |||||
if (resourceGroupFile == null) { | |||||
String msg = RESOURCE_GROUP_FILE + " not specified"; | |||||
LOG.error(msg); | |||||
String resourceGroupUri = foreignAttributes.get(RESOURCE_GROUP_URI); | |||||
if (resourceGroupUri == null) { | |||||
String msg = RESOURCE_GROUP_URI + " not specified"; | |||||
throw new UnsupportedOperationException(msg); | throw new UnsupportedOperationException(msg); | ||||
} | } | ||||
File resourceExternalGroupFile = new File(resourceGroupFile); | |||||
SecurityManager security = System.getSecurityManager(); | |||||
try { | |||||
if (security != null) { | |||||
security.checkWrite(resourceExternalGroupFile.getPath()); | |||||
} | |||||
} catch (SecurityException ex) { | |||||
String msg = "unable to gain write access to external resource file: " | |||||
+ resourceGroupFile; | |||||
LOG.error(msg); | |||||
} | |||||
try { | |||||
boolean exists = resourceExternalGroupFile.exists(); | |||||
if (exists) { | |||||
LOG.warn("overwriting external resource file: " | |||||
+ resourceGroupFile); | |||||
} | |||||
resourceLevel.setExternalFilePath(resourceGroupFile); | |||||
} catch (SecurityException ex) { | |||||
String msg = "unable to gain read access to external resource file: " | |||||
+ resourceGroupFile; | |||||
LOG.error(msg); | |||||
} | |||||
resourceLevel.setExternalUri(URI.create(resourceGroupUri)); | |||||
} | } | ||||
} | } | ||||
} | } |
import org.apache.fop.afp.AFPPaintingState; | import org.apache.fop.afp.AFPPaintingState; | ||||
import org.apache.fop.afp.AFPResourceInfo; | import org.apache.fop.afp.AFPResourceInfo; | ||||
import org.apache.fop.afp.AFPResourceLevel; | import org.apache.fop.afp.AFPResourceLevel; | ||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
import org.apache.fop.afp.AFPResourceManager; | import org.apache.fop.afp.AFPResourceManager; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.image.loader.batik.BatikImageFlavors; | import org.apache.fop.image.loader.batik.BatikImageFlavors; | ||||
//level not explicitly set/changed so default to inline for GOCA graphic objects | //level not explicitly set/changed so default to inline for GOCA graphic objects | ||||
// (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine) | // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine) | ||||
if (!resourceInfo.levelChanged()) { | if (!resourceInfo.levelChanged()) { | ||||
resourceInfo.setLevel(new AFPResourceLevel(AFPResourceLevel.INLINE)); | |||||
resourceInfo.setLevel(new AFPResourceLevel(ResourceType.INLINE)); | |||||
} | } | ||||
} | } | ||||
import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
import java.util.Map; | import java.util.Map; | ||||
import org.w3c.dom.Document; | |||||
import org.apache.xmlgraphics.image.loader.ImageProcessingHints; | |||||
import org.apache.xmlgraphics.image.loader.ImageSessionContext; | |||||
import org.apache.fop.afp.AFPBorderPainter; | import org.apache.fop.afp.AFPBorderPainter; | ||||
import org.apache.fop.afp.AFPPaintingState; | import org.apache.fop.afp.AFPPaintingState; | ||||
import org.apache.fop.afp.AFPUnitConverter; | import org.apache.fop.afp.AFPUnitConverter; | ||||
import org.apache.fop.afp.modca.PresentationTextObject; | import org.apache.fop.afp.modca.PresentationTextObject; | ||||
import org.apache.fop.afp.ptoca.PtocaBuilder; | import org.apache.fop.afp.ptoca.PtocaBuilder; | ||||
import org.apache.fop.afp.ptoca.PtocaProducer; | import org.apache.fop.afp.ptoca.PtocaProducer; | ||||
import org.apache.fop.afp.util.DefaultFOPResourceAccessor; | |||||
import org.apache.fop.afp.util.ResourceAccessor; | |||||
import org.apache.fop.afp.util.AFPResourceAccessor; | |||||
import org.apache.fop.fonts.Font; | import org.apache.fop.fonts.Font; | ||||
import org.apache.fop.fonts.FontInfo; | import org.apache.fop.fonts.FontInfo; | ||||
import org.apache.fop.fonts.FontTriplet; | import org.apache.fop.fonts.FontTriplet; | ||||
import org.apache.fop.traits.BorderProps; | import org.apache.fop.traits.BorderProps; | ||||
import org.apache.fop.traits.RuleStyle; | import org.apache.fop.traits.RuleStyle; | ||||
import org.apache.fop.util.CharUtilities; | import org.apache.fop.util.CharUtilities; | ||||
import org.apache.xmlgraphics.image.loader.ImageProcessingHints; | |||||
import org.apache.xmlgraphics.image.loader.ImageSessionContext; | |||||
import org.w3c.dom.Document; | |||||
/** | /** | ||||
* IFPainter implementation that produces AFP (MO:DCA). | * IFPainter implementation that produces AFP (MO:DCA). | ||||
//Do we need to embed an external page segment? | //Do we need to embed an external page segment? | ||||
if (pageSegment.getURI() != null) { | if (pageSegment.getURI() != null) { | ||||
ResourceAccessor accessor = new DefaultFOPResourceAccessor ( | |||||
AFPResourceAccessor accessor = new AFPResourceAccessor( | |||||
documentHandler.getUserAgent().getNewURIResolver()); | documentHandler.getUserAgent().getNewURIResolver()); | ||||
try { | try { | ||||
URI resourceUri = new URI(pageSegment.getURI()); | URI resourceUri = new URI(pageSegment.getURI()); |
package org.apache.fop.render.afp; | package org.apache.fop.render.afp; | ||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.net.URI; | |||||
import java.util.EnumMap; | import java.util.EnumMap; | ||||
import org.apache.avalon.framework.configuration.Configuration; | import org.apache.avalon.framework.configuration.Configuration; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_BITMAP_ENCODING_QUALITY; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_BITMAP_ENCODING_QUALITY; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.LINE_WIDTH_CORRECTION; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.LINE_WIDTH_CORRECTION; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RENDERER_RESOLUTION; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.RENDERER_RESOLUTION; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RESOURCE_GROUP_FILE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.SHADING; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.SHADING; | ||||
public final class AFPRendererConfig implements RendererConfig { | public final class AFPRendererConfig implements RendererConfig { | ||||
JPEG_ALLOW_JPEG_EMBEDDING("allow-embedding", Boolean.class), | JPEG_ALLOW_JPEG_EMBEDDING("allow-embedding", Boolean.class), | ||||
JPEG_BITMAP_ENCODING_QUALITY("bitmap-encoding-quality", Float.class), | JPEG_BITMAP_ENCODING_QUALITY("bitmap-encoding-quality", Float.class), | ||||
RENDERER_RESOLUTION("renderer-resolution", Integer.class), | RENDERER_RESOLUTION("renderer-resolution", Integer.class), | ||||
RESOURCE_GROUP_FILE("resource-group-file", String.class), | |||||
RESOURCE_GROUP_URI("resource-group-file", URI.class), | |||||
SHADING("shading", AFPShadingMode.class), | SHADING("shading", AFPShadingMode.class), | ||||
LINE_WIDTH_CORRECTION("line-width-correction", Float.class), | LINE_WIDTH_CORRECTION("line-width-correction", Float.class), | ||||
GOCA("goca", Boolean.class), | GOCA("goca", Boolean.class), | ||||
return getParam(RENDERER_RESOLUTION, Integer.class); | return getParam(RENDERER_RESOLUTION, Integer.class); | ||||
} | } | ||||
public String getDefaultResourceGroupFilePath() { | |||||
return getParam(RESOURCE_GROUP_FILE, String.class); | |||||
public URI getDefaultResourceGroupUri() { | |||||
return getParam(RESOURCE_GROUP_URI, URI.class); | |||||
} | } | ||||
public AFPResourceLevelDefaults getResourceLevelDefaults() { | public AFPResourceLevelDefaults getResourceLevelDefaults() { | ||||
private void configure() throws ConfigurationException, FOPException { | private void configure() throws ConfigurationException, FOPException { | ||||
configureImages(); | configureImages(); | ||||
setParam(SHADING, AFPShadingMode.valueOf( | |||||
setParam(SHADING, AFPShadingMode.getValueOf( | |||||
cfg.getChild(SHADING.getName()).getValue(AFPShadingMode.COLOR.getName()))); | cfg.getChild(SHADING.getName()).getValue(AFPShadingMode.COLOR.getName()))); | ||||
Configuration rendererResolutionCfg = cfg.getChild(RENDERER_RESOLUTION.getName(), false); | Configuration rendererResolutionCfg = cfg.getChild(RENDERER_RESOLUTION.getName(), false); | ||||
setParam(RENDERER_RESOLUTION, rendererResolutionCfg == null ? 240 | setParam(RENDERER_RESOLUTION, rendererResolutionCfg == null ? 240 | ||||
private void createResourceGroupFile() throws FOPException { | private void createResourceGroupFile() throws FOPException { | ||||
try { | try { | ||||
Configuration resourceGroupFileCfg = cfg.getChild(RESOURCE_GROUP_FILE.getName(), false); | |||||
if (resourceGroupFileCfg != null) { | |||||
String resourceGroupDest = null; | |||||
resourceGroupDest = resourceGroupFileCfg.getValue(); | |||||
if (resourceGroupDest != null) { | |||||
File resourceGroupFile = new File(resourceGroupDest); | |||||
boolean created = resourceGroupFile.createNewFile(); | |||||
if (created && resourceGroupFile.canWrite()) { | |||||
setParam(RESOURCE_GROUP_FILE, resourceGroupDest); | |||||
} else { | |||||
LOG.warn("Unable to write to default external resource group file '" | |||||
+ resourceGroupDest + "'"); | |||||
} | |||||
} | |||||
Configuration resourceGroupUriCfg = cfg.getChild(RESOURCE_GROUP_URI.getName(), false); | |||||
if (resourceGroupUriCfg != null) { | |||||
URI resourceGroupUri = URI.create(resourceGroupUriCfg.getValue()); | |||||
// TODO validate? | |||||
setParam(RESOURCE_GROUP_URI, resourceGroupUri); | |||||
} | } | ||||
} catch (ConfigurationException e) { | } catch (ConfigurationException e) { | ||||
LogUtil.handleException(LOG, e, strict); | LogUtil.handleException(LOG, e, strict); | ||||
} catch (IOException ioe) { | |||||
throw new FOPException("Could not create default external resource group file", ioe); | |||||
} | } | ||||
} | } | ||||
if (config.getResolution() != null) { | if (config.getResolution() != null) { | ||||
documentHandler.setResolution(config.getResolution()); | documentHandler.setResolution(config.getResolution()); | ||||
} | } | ||||
if (config.getDefaultResourceGroupFilePath() != null) { | |||||
documentHandler.setDefaultResourceGroupFilePath(config.getDefaultResourceGroupFilePath()); | |||||
} | |||||
if (config.isWrapPseg() != null) { | if (config.isWrapPseg() != null) { | ||||
documentHandler.setWrapPSeg(config.isWrapPseg()); | documentHandler.setWrapPSeg(config.isWrapPseg()); | ||||
} | } | ||||
if (config.isFs45() != null) { | if (config.isFs45() != null) { | ||||
documentHandler.setFS45(config.isFs45()); | documentHandler.setFS45(config.isFs45()); | ||||
} | } | ||||
if (config.allowJpegEmbedding() != null) { | if (config.allowJpegEmbedding() != null) { | ||||
documentHandler.canEmbedJpeg(config.allowJpegEmbedding()); | documentHandler.canEmbedJpeg(config.allowJpegEmbedding()); | ||||
} | } | ||||
if (config.isStrokeGocaText() != null) { | if (config.isStrokeGocaText() != null) { | ||||
documentHandler.setStrokeGOCAText(config.isStrokeGocaText()); | documentHandler.setStrokeGOCAText(config.isStrokeGocaText()); | ||||
} | } | ||||
if (config.getDefaultResourceGroupUri() != null) { | |||||
documentHandler.setDefaultResourceGroupUri(config.getDefaultResourceGroupUri()); | |||||
} | |||||
AFPResourceLevelDefaults resourceLevelDefaults = config.getResourceLevelDefaults(); | AFPResourceLevelDefaults resourceLevelDefaults = config.getResourceLevelDefaults(); | ||||
if (resourceLevelDefaults != null) { | if (resourceLevelDefaults != null) { | ||||
documentHandler.setResourceLevelDefaults(resourceLevelDefaults); | documentHandler.setResourceLevelDefaults(resourceLevelDefaults); |
import java.io.ObjectStreamException; | import java.io.ObjectStreamException; | ||||
import java.io.Serializable; | import java.io.Serializable; | ||||
/** Enumeration class for the AFP shading mode. */ | |||||
public final class AFPShadingMode implements Serializable { | |||||
private static final long serialVersionUID = 8579867898716480779L; | |||||
/** Enumeration of the AFP shading modes. */ | |||||
public enum AFPShadingMode implements Serializable { | |||||
/** the color mode (the default) */ | /** the color mode (the default) */ | ||||
public static final AFPShadingMode COLOR = new AFPShadingMode("COLOR"); | |||||
COLOR("COLOR"), | |||||
/** the dithered mode */ | /** the dithered mode */ | ||||
public static final AFPShadingMode DITHERED = new AFPShadingMode("DITHERED"); | |||||
DITHERED("DITHERED"); | |||||
private String name; | private String name; | ||||
* @param name the name of the enumeration value | * @param name the name of the enumeration value | ||||
* @return the enumeration object | * @return the enumeration object | ||||
*/ | */ | ||||
public static AFPShadingMode valueOf(String name) { | |||||
public static AFPShadingMode getValueOf(String name) { | |||||
if (COLOR.getName().equalsIgnoreCase(name)) { | if (COLOR.getName().equalsIgnoreCase(name)) { | ||||
return COLOR; | return COLOR; | ||||
} else if (DITHERED.getName().equalsIgnoreCase(name)) { | } else if (DITHERED.getName().equalsIgnoreCase(name)) { | ||||
public String toString() { | public String toString() { | ||||
return getClass().getName() + ":" + name; | return getClass().getName() + ":" + name; | ||||
} | } | ||||
} | } |
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.io.URIResolverWrapper; | |||||
import org.apache.fop.fonts.EmbedFontInfo; | |||||
import org.apache.fop.fonts.FontCollection; | import org.apache.fop.fonts.FontCollection; | ||||
import org.apache.fop.render.RendererConfig.RendererConfigParser; | import org.apache.fop.render.RendererConfig.RendererConfigParser; | ||||
import org.apache.fop.render.bitmap.BitmapRendererConfig.BitmapRendererConfigParser; | import org.apache.fop.render.bitmap.BitmapRendererConfig.BitmapRendererConfigParser; | ||||
import org.apache.fop.render.intermediate.IFDocumentHandler; | import org.apache.fop.render.intermediate.IFDocumentHandler; | ||||
import org.apache.fop.render.java2d.Base14FontCollection; | import org.apache.fop.render.java2d.Base14FontCollection; | ||||
import org.apache.fop.render.java2d.ConfiguredFontCollection; | |||||
import org.apache.fop.render.java2d.InstalledFontCollection; | import org.apache.fop.render.java2d.InstalledFontCollection; | ||||
import org.apache.fop.render.java2d.Java2DFontMetrics; | import org.apache.fop.render.java2d.Java2DFontMetrics; | ||||
import org.apache.fop.render.java2d.Java2DRendererConfigurator; | import org.apache.fop.render.java2d.Java2DRendererConfigurator; | ||||
super(userAgent, rendererConfigParser); | super(userAgent, rendererConfigParser); | ||||
} | } | ||||
/** | |||||
* Default constructor | |||||
* @param userAgent user agent | |||||
*/ | |||||
public BitmapRendererConfigurator(FOUserAgent userAgent) { | |||||
super(userAgent, null); | |||||
} | |||||
// ---=== IFDocumentHandler configuration ===--- | // ---=== IFDocumentHandler configuration ===--- | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
} | } | ||||
} | } | ||||
@Override | |||||
protected FontCollection createCollectionFromFontList(URIResolverWrapper uriResolverWrapper, | |||||
List<EmbedFontInfo> fontList) { | |||||
return new ConfiguredFontCollection(uriResolverWrapper, fontList, userAgent.isComplexScriptFeaturesEnabled()); | |||||
} | |||||
@Override | @Override | ||||
protected List<FontCollection> getDefaultFontCollection() { | protected List<FontCollection> getDefaultFontCollection() { | ||||
final Java2DFontMetrics java2DFontMetrics = new Java2DFontMetrics(); | final Java2DFontMetrics java2DFontMetrics = new Java2DFontMetrics(); |
package org.apache.fop.render.bitmap; | package org.apache.fop.render.bitmap; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.render.bitmap.PNGRendererConfig.PNGRendererConfigParser; | |||||
import org.apache.fop.render.intermediate.IFContext; | import org.apache.fop.render.intermediate.IFContext; | ||||
import org.apache.fop.render.intermediate.IFDocumentHandler; | import org.apache.fop.render.intermediate.IFDocumentHandler; | ||||
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; | import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; | ||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public IFDocumentHandlerConfigurator getConfigurator() { | public IFDocumentHandlerConfigurator getConfigurator() { | ||||
// TODO what constructir params? | |||||
return new BitmapRendererConfigurator(getUserAgent()); | |||||
return new BitmapRendererConfigurator(getUserAgent(), new PNGRendererConfigParser()); | |||||
} | } | ||||
} | } |
/* | |||||
* 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. | |||||
*/ | |||||
package org.apache.fop.render.bitmap; | |||||
import org.apache.avalon.framework.configuration.Configuration; | |||||
import org.apache.xmlgraphics.util.MimeConstants; | |||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.fonts.DefaultFontConfig; | |||||
import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser; | |||||
public final class PNGRendererConfig extends BitmapRendererConfig { | |||||
private PNGRendererConfig(DefaultFontConfig fontConfig) { | |||||
super(fontConfig); | |||||
} | |||||
public static class PNGRendererConfigParser implements RendererConfigParser { | |||||
public PNGRendererConfig build(FOUserAgent userAgent, Configuration cfg) | |||||
throws FOPException { | |||||
return new PNGRendererConfig(new DefaultFontConfigParser().parse(cfg, | |||||
userAgent.validateStrictly())); | |||||
} | |||||
public String getMimeType() { | |||||
return MimeConstants.MIME_PNG; | |||||
} | |||||
} | |||||
} |
*/ | */ | ||||
public boolean updateFont(String name, int size) { | public boolean updateFont(String name, int size) { | ||||
FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(name); | |||||
FontMetricsMapper mapper = (FontMetricsMapper) fontInfo.getMetricsFor(name); | |||||
boolean updateName = (!mapper.getFontName().equals( | boolean updateName = (!mapper.getFontName().equals( | ||||
getGraph().getFont().getFontName())); | getGraph().getFont().getFontName())); | ||||
boolean updateSize = (size != (getGraph().getFont().getSize() * 1000)); | boolean updateSize = (size != (getGraph().getFont().getSize() * 1000)); |
import org.apache.avalon.framework.configuration.ConfigurationException; | import org.apache.avalon.framework.configuration.ConfigurationException; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import org.apache.fop.fonts.DefaultFontConfig; | import org.apache.fop.fonts.DefaultFontConfig; | ||||
import org.apache.fop.fonts.DefaultFontConfigurator; | import org.apache.fop.fonts.DefaultFontConfigurator; | ||||
FontInfo fontInfo = new FontInfo(); | FontInfo fontInfo = new FontInfo(); | ||||
final boolean strict = false; | final boolean strict = false; | ||||
if (cfg != null) { | if (cfg != null) { | ||||
//TODO Wire in the FontEventListener | |||||
URIResolverWrapper resolver = DefaultResourceResolver.createDefaultWrapper(); | |||||
URIResolverWrapper resolver = ResourceResolverFactory.createDefaultWrapper(); | |||||
//TODO The following could be optimized by retaining the FontManager somewhere | //TODO The following could be optimized by retaining the FontManager somewhere | ||||
FontManager fontManager = new FontManager(resolver, FontDetectorFactory.createDefault(), | FontManager fontManager = new FontManager(resolver, FontDetectorFactory.createDefault(), | ||||
FontCacheManagerFactory.createDefault()); | FontCacheManagerFactory.createDefault()); |
import org.apache.fop.apps.FopFactory; | import org.apache.fop.apps.FopFactory; | ||||
import org.apache.fop.apps.FopFactoryBuilder; | import org.apache.fop.apps.FopFactoryBuilder; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.Resource; | import org.apache.fop.apps.io.Resource; | ||||
import org.apache.fop.apps.io.ResourceResolver; | import org.apache.fop.apps.io.ResourceResolver; | ||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.render.xml.XMLRenderer; | import org.apache.fop.render.xml.XMLRenderer; | ||||
import static org.apache.fop.FOPTestUtils.getBaseDir; | import static org.apache.fop.FOPTestUtils.getBaseDir; | ||||
} | } | ||||
private static final class CustomURIResolver implements ResourceResolver { | private static final class CustomURIResolver implements ResourceResolver { | ||||
private final DefaultResourceResolver defaultImpl = new DefaultResourceResolver(); | |||||
private final ResourceResolver defaultImpl = ResourceResolverFactory.createDefaultResourceResolver(); | |||||
public Resource getResource(URI uri) throws IOException { | public Resource getResource(URI uri) throws IOException { | ||||
if (uri.getScheme().equals("funky") && uri.getSchemeSpecificPart().equals("myimage123")) { | if (uri.getScheme().equals("funky") && uri.getSchemeSpecificPart().equals("myimage123")) { |
package org.apache.fop.afp; | package org.apache.fop.afp; | ||||
import static org.junit.Assert.assertFalse; | |||||
import static org.junit.Assert.assertTrue; | |||||
import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import org.apache.xmlgraphics.util.MimeConstants; | import org.apache.xmlgraphics.util.MimeConstants; | ||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import static org.junit.Assert.assertFalse; | |||||
import static org.junit.Assert.assertTrue; | |||||
/** | /** | ||||
* Test case for {@link AFPResourceManager}. | * Test case for {@link AFPResourceManager}. | ||||
*/ | */ | ||||
@Before | @Before | ||||
public void setUp() throws IOException { | public void setUp() throws IOException { | ||||
sut = new AFPResourceManager(); | |||||
sut = new AFPResourceManager(ResourceResolverFactory.createDefaultWrapper()); | |||||
AFPPaintingState paintingState = new AFPPaintingState(); | AFPPaintingState paintingState = new AFPPaintingState(); | ||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | ||||
DataStream stream = sut.createDataStream(paintingState, outStream); | DataStream stream = sut.createDataStream(paintingState, outStream); |
package org.apache.fop.apps; | package org.apache.fop.apps; | ||||
import java.net.URI; | |||||
import java.util.Map; | import java.util.Map; | ||||
import org.w3c.dom.Element; | import org.w3c.dom.Element; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.DEFAULT_RESOURCE_LEVELS; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.DEFAULT_RESOURCE_LEVELS; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_DITHERING_QUALITY; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_DITHERING_QUALITY; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_FS45; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_JPEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_MAPPING_OPTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_MODE; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_MODE; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_NATIVE; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_NATIVE; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_WRAP_PSEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_ALLOW_JPEG_EMBEDDING; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_BITMAP_ENCODING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RENDERER_RESOLUTION; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.RENDERER_RESOLUTION; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.LINE_WIDTH_CORRECTION; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.LINE_WIDTH_CORRECTION; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RESOURCE_GROUP_FILE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererConfig.Options.SHADING; | import static org.apache.fop.render.afp.AFPRendererConfig.Options.SHADING; | ||||
/** | /** | ||||
return this; | return this; | ||||
} | } | ||||
public AFPRendererConfBuilder setResourceGroupFile(String value) { | |||||
createTextElement(RESOURCE_GROUP_FILE, value); | |||||
public AFPRendererConfBuilder setResourceGroupUri(String uri) { | |||||
createTextElement(RESOURCE_GROUP_URI, uri); | |||||
return this; | return this; | ||||
} | } | ||||
public AFPRendererConfBuilder setResourceResourceLevels(Map<String, String> levels) { | |||||
public AFPRendererConfBuilder setDefaultResourceLevels(Map<String, String> levels) { | |||||
Element e = createElement(DEFAULT_RESOURCE_LEVELS.getName()); | Element e = createElement(DEFAULT_RESOURCE_LEVELS.getName()); | ||||
for (String key : levels.keySet()) { | for (String key : levels.keySet()) { | ||||
e.setAttribute(key, levels.get(key)); | e.setAttribute(key, levels.get(key)); | ||||
private final Element el; | private final Element el; | ||||
private Element jpeg; | |||||
private ImagesBuilder(AFPRendererConfig.ImagesModeOptions mode) { | private ImagesBuilder(AFPRendererConfig.ImagesModeOptions mode) { | ||||
el = createElement(IMAGES.getName()); | el = createElement(IMAGES.getName()); | ||||
setAttribute(IMAGES_MODE, mode.getName()); | setAttribute(IMAGES_MODE, mode.getName()); | ||||
return setAttribute(name, value); | return setAttribute(name, value); | ||||
} | } | ||||
public ImagesBuilder setAllowJpegEmbedding(boolean value) { | |||||
getJpeg().setAttribute(JPEG_ALLOW_JPEG_EMBEDDING.getName(), String.valueOf(value)); | |||||
return this; | |||||
} | |||||
public ImagesBuilder setBitmapEncodingQuality(float value) { | |||||
getJpeg().setAttribute(JPEG_BITMAP_ENCODING_QUALITY.getName(), String.valueOf(value)); | |||||
return this; | |||||
} | |||||
public ImagesBuilder setDitheringQuality(String value) { | public ImagesBuilder setDitheringQuality(String value) { | ||||
return setAttribute(IMAGES_DITHERING_QUALITY, value); | return setAttribute(IMAGES_DITHERING_QUALITY, value); | ||||
} | } | ||||
public ImagesBuilder setDitheringQuality(float value) { | public ImagesBuilder setDitheringQuality(float value) { | ||||
return setAttribute(IMAGES_DITHERING_QUALITY, String.valueOf(value)); | |||||
return setAttribute(IMAGES_DITHERING_QUALITY, value); | |||||
} | |||||
public ImagesBuilder setFs45(boolean value) { | |||||
return setAttribute(IMAGES_FS45, value); | |||||
} | |||||
public ImagesBuilder setMappingOption(String value) { | |||||
return setAttribute(IMAGES_MAPPING_OPTION, value); | |||||
} | |||||
public ImagesBuilder setWrapPseg(boolean value) { | |||||
return setAttribute(IMAGES_WRAP_PSEG, value); | |||||
} | } | ||||
public ImagesBuilder setNativeImageSupport(boolean value) { | public ImagesBuilder setNativeImageSupport(boolean value) { | ||||
return setAttribute(IMAGES_NATIVE, String.valueOf(value)); | |||||
return setAttribute(IMAGES_NATIVE, value); | |||||
} | } | ||||
public AFPRendererConfBuilder endImages() { | public AFPRendererConfBuilder endImages() { | ||||
return AFPRendererConfBuilder.this.endImages(); | return AFPRendererConfBuilder.this.endImages(); | ||||
} | } | ||||
private ImagesBuilder setAttribute(Options options, String value) { | |||||
private ImagesBuilder setAttribute(Options options, Object value) { | |||||
return setAttribute(options.getName(), value); | return setAttribute(options.getName(), value); | ||||
} | } | ||||
private ImagesBuilder setAttribute(String name, String value) { | |||||
el.setAttribute(name, value); | |||||
private ImagesBuilder setAttribute(String name, Object value) { | |||||
el.setAttribute(name, String.valueOf(value)); | |||||
return this; | return this; | ||||
} | } | ||||
private Element getJpeg() { | |||||
if (jpeg == null) { | |||||
jpeg = createElement(IMAGES_JPEG.getName(), el); | |||||
} | |||||
return jpeg; | |||||
} | |||||
} | } | ||||
} | } |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.area.AreaTreeHandler; | import org.apache.fop.area.AreaTreeHandler; | ||||
import org.apache.fop.area.Block; | import org.apache.fop.area.Block; | ||||
import org.apache.fop.fo.FONode; | import org.apache.fop.fo.FONode; | ||||
@Test(expected = IllegalArgumentException.class) | @Test(expected = IllegalArgumentException.class) | ||||
public void testNullParamsInConstructor() throws URISyntaxException { | public void testNullParamsInConstructor() throws URISyntaxException { | ||||
new FopFactoryBuilder(null, new DefaultResourceResolver()); | |||||
new FopFactoryBuilder(null, ResourceResolverFactory.createDefaultResourceResolver()); | |||||
} | } | ||||
@Test | @Test |
/* | |||||
* 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.apps.io; | |||||
import java.io.File; | |||||
import java.io.FileInputStream; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.io.OutputStream; | |||||
import java.net.URI; | |||||
import org.junit.Test; | |||||
import static org.junit.Assert.assertArrayEquals; | |||||
import static org.junit.Assert.assertEquals; | |||||
import static org.mockito.Mockito.mock; | |||||
import static org.mockito.Mockito.never; | |||||
import static org.mockito.Mockito.times; | |||||
import static org.mockito.Mockito.verify; | |||||
public class ResourceResolverFactoryTestCase { | |||||
private static final byte[] DATA = new byte[]{(byte) 0, (byte) 1, (byte) 2}; | |||||
private void writeDataTo(File f) throws IOException { | |||||
writeDataTo(new FileOutputStream(f)); | |||||
} | |||||
private void writeDataTo(OutputStream os) throws IOException { | |||||
os.write(DATA); | |||||
os.close(); | |||||
} | |||||
private void checkStream(InputStream inputStream) throws IOException { | |||||
byte[] actual = new byte[DATA.length]; | |||||
for (int i = 0; i < DATA.length; i++) { | |||||
actual[i] = (byte) inputStream.read(); | |||||
} | |||||
assertEquals(-1, inputStream.read()); | |||||
assertArrayEquals(DATA, actual); | |||||
} | |||||
@Test | |||||
public void testDefaultResourceResolverGetResource() throws Exception { | |||||
ResourceResolver sut = ResourceResolverFactory.createDefaultResourceResolver(); | |||||
File inputFile = File.createTempFile("prefix", "suffix"); | |||||
InputStream is = null; | |||||
try { | |||||
writeDataTo(inputFile); | |||||
is = sut.getResource(inputFile.toURI()); | |||||
checkStream(is); | |||||
} finally { | |||||
if (is != null) { | |||||
is.close(); | |||||
} | |||||
inputFile.delete(); | |||||
} | |||||
} | |||||
@Test | |||||
public void testDefaultResourceResolverGetOutput() throws Exception { | |||||
ResourceResolver sut = ResourceResolverFactory.createDefaultResourceResolver(); | |||||
File outputFile = File.createTempFile("prefix", "suffix"); | |||||
writeDataTo(sut.getOutputStream(outputFile.toURI())); | |||||
InputStream is = new FileInputStream(outputFile); | |||||
try { | |||||
checkStream(is); | |||||
} finally { | |||||
is.close(); | |||||
} | |||||
} | |||||
private static class TestCreateTempAwareResourceResolverHelper implements ResourceResolver { | |||||
final TempResourceResolver tempResourceResolver = mock(TempResourceResolver.class); | |||||
final ResourceResolver defaultResourceResolver = mock(ResourceResolver.class); | |||||
final ResourceResolver sut = ResourceResolverFactory.createTempAwareResourceResolver( | |||||
tempResourceResolver, defaultResourceResolver); | |||||
public Resource getResource(URI uri) throws IOException { | |||||
return sut.getResource(uri); | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
return sut.getOutputStream(uri); | |||||
} | |||||
} | |||||
@Test | |||||
public void testCreateTempAwareResourceResolverForTmpResource() throws Exception { | |||||
URI uri = URI.create("tmp:///id"); | |||||
TestCreateTempAwareResourceResolverHelper helper = new TestCreateTempAwareResourceResolverHelper(); | |||||
helper.getResource(uri); | |||||
verify(helper.tempResourceResolver, times(1)).getResource(uri.getPath()); | |||||
verify(helper.defaultResourceResolver, never()).getResource(uri); | |||||
} | |||||
@Test | |||||
public void testCreateTempAwareResourceResolverForRegularResource() throws Exception { | |||||
URI uri = URI.create("file:///path/to/file"); | |||||
TestCreateTempAwareResourceResolverHelper helper = new TestCreateTempAwareResourceResolverHelper(); | |||||
helper.getResource(uri); | |||||
verify(helper.tempResourceResolver, never()).getResource(uri.getPath()); | |||||
verify(helper.defaultResourceResolver, times(1)).getResource(uri); | |||||
} | |||||
@Test | |||||
public void testCreateTempAwareResourceResolverForTmpOuput() throws Exception { | |||||
URI uri = URI.create("tmp:///id"); | |||||
TestCreateTempAwareResourceResolverHelper helper = new TestCreateTempAwareResourceResolverHelper(); | |||||
helper.getOutputStream(uri); | |||||
verify(helper.tempResourceResolver, times(1)).getOutputStream(uri.getPath()); | |||||
verify(helper.defaultResourceResolver, never()).getOutputStream(uri); | |||||
} | |||||
@Test | |||||
public void testCreateTempAwareResourceResolverForRegularOutput() throws Exception { | |||||
URI uri = URI.create("file:///path/to/file"); | |||||
TestCreateTempAwareResourceResolverHelper helper = new TestCreateTempAwareResourceResolverHelper(); | |||||
helper.getOutputStream(uri); | |||||
verify(helper.tempResourceResolver, never()).getOutputStream(uri.getPath()); | |||||
verify(helper.defaultResourceResolver, times(1)).getOutputStream(uri); | |||||
} | |||||
@Test | |||||
public void testCreateSchemaAwareResourceResolverForDefaultResource() throws Exception { | |||||
URI uri = URI.create("file:///path/to/file"); | |||||
TestCreateSchemaAwareResourceResolverBuilderHelper helper | |||||
= new TestCreateSchemaAwareResourceResolverBuilderHelper(); | |||||
helper.getResource(uri); | |||||
verify(helper.registedResourceResolver, never()).getResource(uri); | |||||
verify(helper.defaultResourceResolver, times(1)).getResource(uri); | |||||
} | |||||
@Test | |||||
public void testCreateSchemaAwareResourceResolverForRegisteredResource() throws Exception { | |||||
URI uri = URI.create(TestCreateSchemaAwareResourceResolverBuilderHelper.SCHEMA + ":///path"); | |||||
TestCreateSchemaAwareResourceResolverBuilderHelper helper | |||||
= new TestCreateSchemaAwareResourceResolverBuilderHelper(); | |||||
helper.getResource(uri); | |||||
verify(helper.registedResourceResolver, times(1)).getResource(uri); | |||||
verify(helper.defaultResourceResolver, never()).getResource(uri); | |||||
} | |||||
@Test | |||||
public void testCreateSchemaAwareResourceResolverForDefaultOutput() throws Exception { | |||||
URI uri = URI.create("file:///path/to/file"); | |||||
TestCreateSchemaAwareResourceResolverBuilderHelper helper | |||||
= new TestCreateSchemaAwareResourceResolverBuilderHelper(); | |||||
helper.getOutputStream(uri); | |||||
verify(helper.registedResourceResolver, never()).getOutputStream(uri); | |||||
verify(helper.defaultResourceResolver, times(1)).getOutputStream(uri); | |||||
} | |||||
@Test | |||||
public void testCreateSchemaAwareResourceResolverForRegisteredOutput() throws Exception { | |||||
URI uri = URI.create(TestCreateSchemaAwareResourceResolverBuilderHelper.SCHEMA + ":///path"); | |||||
TestCreateSchemaAwareResourceResolverBuilderHelper helper | |||||
= new TestCreateSchemaAwareResourceResolverBuilderHelper(); | |||||
helper.getOutputStream(uri); | |||||
verify(helper.registedResourceResolver, times(1)).getOutputStream(uri); | |||||
verify(helper.defaultResourceResolver, never()).getOutputStream(uri); | |||||
} | |||||
private static class TestCreateSchemaAwareResourceResolverBuilderHelper implements ResourceResolver { | |||||
private static final String SCHEMA = "protocol"; | |||||
final ResourceResolver registedResourceResolver = mock(ResourceResolver.class); | |||||
final ResourceResolver defaultResourceResolver = mock(ResourceResolver.class); | |||||
final ResourceResolver sut; | |||||
TestCreateSchemaAwareResourceResolverBuilderHelper() { | |||||
ResourceResolverFactory.SchemaAwareResourceResolverBuilder builder | |||||
= ResourceResolverFactory.createSchemaAwareResourceResolverBuilder( | |||||
defaultResourceResolver); | |||||
builder.registerResourceResolverForSchema(SCHEMA, registedResourceResolver); | |||||
sut = builder.build(); | |||||
} | |||||
public Resource getResource(URI uri) throws IOException { | |||||
return sut.getResource(uri); | |||||
} | |||||
public OutputStream getOutputStream(URI uri) throws IOException { | |||||
return sut.getOutputStream(uri); | |||||
} | |||||
} | |||||
} | |||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
public class DejaVuLGCSerifTestCase { | public class DejaVuLGCSerifTestCase { | ||||
private URIResolverWrapper resolver = new URIResolverWrapper(new File(".").toURI(), | private URIResolverWrapper resolver = new URIResolverWrapper(new File(".").toURI(), | ||||
new DefaultResourceResolver()); | |||||
ResourceResolverFactory.createDefaultResourceResolver()); | |||||
private CustomFont font; | private CustomFont font; | ||||
/** | /** |
import org.junit.Test; | import org.junit.Test; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import org.apache.fop.fonts.EncodingMode; | import org.apache.fop.fonts.EncodingMode; | ||||
boolean useComplexScriptFeatures = false; | boolean useComplexScriptFeatures = false; | ||||
File file = new File("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"); | File file = new File("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"); | ||||
URI absoluteFilePath = file.toURI(); | URI absoluteFilePath = file.toURI(); | ||||
URIResolverWrapper resolver = DefaultResourceResolver.createDefaultWrapper(); | |||||
URIResolverWrapper resolver = ResourceResolverFactory.createDefaultWrapper(); | |||||
String fontName = "Deja Vu"; | String fontName = "Deja Vu"; | ||||
boolean embedded = false; | boolean embedded = false; | ||||
boolean useKerning = true; | boolean useKerning = true; |
package org.apache.fop.pdf; | package org.apache.fop.pdf; | ||||
import static org.junit.Assert.assertEquals; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.apache.fop.apps.io.DefaultResourceResolver; | |||||
import org.apache.fop.apps.io.ResourceResolverFactory; | |||||
import org.apache.fop.apps.io.URIResolverWrapper; | import org.apache.fop.apps.io.URIResolverWrapper; | ||||
import org.apache.fop.fonts.CIDSubset; | import org.apache.fop.fonts.CIDSubset; | ||||
import org.apache.fop.fonts.MultiByteFont; | import org.apache.fop.fonts.MultiByteFont; | ||||
import static org.junit.Assert.assertEquals; | |||||
/** | /** | ||||
* Test case for {@link PDFFactory}. | * Test case for {@link PDFFactory}. | ||||
*/ | */ | ||||
} | } | ||||
PDFDocument doc = new PDFDocument("Test"); | PDFDocument doc = new PDFDocument("Test"); | ||||
PDFFactory pdfFactory = new PDFFactory(doc); | PDFFactory pdfFactory = new PDFFactory(doc); | ||||
MockedFont font = new MockedFont(DefaultResourceResolver.createDefaultWrapper()); | |||||
MockedFont font = new MockedFont(ResourceResolverFactory.createDefaultWrapper()); | |||||
PDFFont pdfDejaVu = pdfFactory.makeFont("DejaVu", "DejaVu", "TTF", font, font); | PDFFont pdfDejaVu = pdfFactory.makeFont("DejaVu", "DejaVu", "TTF", font, font); | ||||
assertEquals("/EAAAAA+DejaVu", pdfDejaVu.getBaseFont().toString()); | assertEquals("/EAAAAA+DejaVu", pdfDejaVu.getBaseFont().toString()); |
import static org.junit.Assert.assertNull; | import static org.junit.Assert.assertNull; | ||||
public class AFPRendererConfigParserTestCase | public class AFPRendererConfigParserTestCase | ||||
extends AbstractRendererConfigParserTester<AFPRendererConfBuilder, AFPRendererConfig> { | |||||
extends AbstractRendererConfigParserTester<AFPRendererConfBuilder, AFPRendererConfig> { | |||||
public AFPRendererConfigParserTestCase() { | public AFPRendererConfigParserTestCase() { | ||||
super(new AFPRendererConfigParser(), AFPRendererConfBuilder.class); | super(new AFPRendererConfigParser(), AFPRendererConfBuilder.class); | ||||
@Test | @Test | ||||
public void testLineWidthCorrection() throws Exception { | public void testLineWidthCorrection() throws Exception { | ||||
parseConfig(createRenderer()); | parseConfig(createRenderer()); | ||||
assertEquals(AFPConstants.LINE_WIDTH_CORRECTION, conf.getLineWidthCorrection().floatValue(), 0.0001f); | |||||
assertEquals(AFPConstants.LINE_WIDTH_CORRECTION, | |||||
conf.getLineWidthCorrection().floatValue(), 0.0001f); | |||||
parseConfig(createRenderer().setLineWidthCorrection(1f)); | parseConfig(createRenderer().setLineWidthCorrection(1f)); | ||||
assertEquals(Float.valueOf(1f), conf.getLineWidthCorrection()); | assertEquals(Float.valueOf(1f), conf.getLineWidthCorrection()); | ||||
} | } | ||||
@Test | @Test | ||||
public void testResourceGroupFile() throws Exception { | |||||
public void testResourceGroupUri() throws Exception { | |||||
parseConfig(createRenderer()); | parseConfig(createRenderer()); | ||||
assertEquals(null, conf.getDefaultResourceGroupFilePath()); | |||||
assertEquals(null, conf.getDefaultResourceGroupUri()); | |||||
// TODO yuck! | // TODO yuck! | ||||
File file = File.createTempFile("AFPRendererConfigParserTestCase", ""); | File file = File.createTempFile("AFPRendererConfigParserTestCase", ""); | ||||
try { | try { | ||||
file.delete(); | file.delete(); | ||||
parseConfig(createRenderer().setResourceGroupFile(file.getAbsolutePath())); | |||||
assertEquals(file.getAbsolutePath(), conf.getDefaultResourceGroupFilePath()); | |||||
parseConfig(createRenderer().setResourceGroupUri(file.toURI().toASCIIString())); | |||||
assertEquals(file.toURI(), conf.getDefaultResourceGroupUri()); | |||||
} finally { | } finally { | ||||
file.delete(); | file.delete(); | ||||
} | } | ||||
assertNull(conf.getResourceLevelDefaults()); | assertNull(conf.getResourceLevelDefaults()); | ||||
Map<String, String> levels = new HashMap<String, String>(); | Map<String, String> levels = new HashMap<String, String>(); | ||||
levels.put("goca", "page"); | levels.put("goca", "page"); | ||||
parseConfig(createRenderer().setResourceResourceLevels(levels)); | |||||
parseConfig(createRenderer().setDefaultResourceLevels(levels)); | |||||
assertNotNull(conf.getResourceLevelDefaults()); | assertNotNull(conf.getResourceLevelDefaults()); | ||||
} | } | ||||
assertEquals(false, conf.isColorImages()); | assertEquals(false, conf.isColorImages()); | ||||
assertEquals(Integer.valueOf(8), conf.getBitsPerPixel()); | assertEquals(Integer.valueOf(8), conf.getBitsPerPixel()); | ||||
ImagesModeOptions mode = MODE_GRAYSCALE; | ImagesModeOptions mode = MODE_GRAYSCALE; | ||||
parseConfig(createRenderer() | |||||
.startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), String.valueOf(1)) | |||||
.endImages()); | |||||
parseConfig(createRenderer().startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), String.valueOf(1)) | |||||
.endImages()); | |||||
assertEquals(false, conf.isColorImages()); | assertEquals(false, conf.isColorImages()); | ||||
assertEquals(Integer.valueOf(1), conf.getBitsPerPixel()); | assertEquals(Integer.valueOf(1), conf.getBitsPerPixel()); | ||||
mode = MODE_COLOR; | mode = MODE_COLOR; | ||||
parseConfig(createRenderer() | parseConfig(createRenderer() | ||||
.startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), String.valueOf(false)) | |||||
.endImages()); | |||||
.startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), | |||||
String.valueOf(false)) | |||||
.endImages()); | |||||
assertEquals(true, conf.isColorImages()); | assertEquals(true, conf.isColorImages()); | ||||
assertEquals(false, conf.isCmykImagesSupported()); | assertEquals(false, conf.isCmykImagesSupported()); | ||||
parseConfig(createRenderer() | |||||
.startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), String.valueOf(true)) | |||||
.endImages()); | |||||
parseConfig(createRenderer().startImages(mode) | |||||
.setModeAttribute(mode.getModeAttribute(), String.valueOf(true)) | |||||
.endImages()); | |||||
assertEquals(true, conf.isColorImages()); | assertEquals(true, conf.isColorImages()); | ||||
assertEquals(true, conf.isCmykImagesSupported()); | assertEquals(true, conf.isCmykImagesSupported()); | ||||
} | } | ||||
assertEquals(0.25f, conf.getDitheringQuality(), 0.001f); | assertEquals(0.25f, conf.getDitheringQuality(), 0.001f); | ||||
} | } | ||||
@Test | |||||
public void testAllowJpegEmbedding() throws Exception { | |||||
parseConfig(); | |||||
assertEquals(false, conf.allowJpegEmbedding()); | |||||
parseConfig(createRenderer().startImages().setAllowJpegEmbedding(true).endImages()); | |||||
assertEquals(true, conf.allowJpegEmbedding()); | |||||
} | |||||
@Test | |||||
public void testBitmapEncodingQuality() throws Exception { | |||||
parseConfig(); | |||||
assertEquals(1.0f, conf.getBitmapEncodingQuality(), 0.001f); | |||||
parseConfig(createRenderer().startImages().setBitmapEncodingQuality(0.5f).endImages()); | |||||
assertEquals(0.5f, conf.getBitmapEncodingQuality(), 0.001f); | |||||
} | |||||
@Test | |||||
public void testFS45() throws Exception { | |||||
parseConfig(); | |||||
assertEquals(false, conf.isFs45()); | |||||
parseConfig(createRenderer().startImages().setFs45(true).endImages()); | |||||
assertEquals(true, conf.isFs45()); | |||||
} | |||||
@Test | |||||
public void tesPseg() throws Exception { | |||||
parseConfig(); | |||||
assertEquals(false, conf.isWrapPseg()); | |||||
parseConfig(createRenderer().startImages().setWrapPseg(true).endImages()); | |||||
assertEquals(true, conf.isWrapPseg()); | |||||
} | |||||
@Test(expected = IllegalArgumentException.class) | @Test(expected = IllegalArgumentException.class) | ||||
public void testForNameException() throws Exception { | public void testForNameException() throws Exception { | ||||
ImagesModeOptions.forName("_"); | ImagesModeOptions.forName("_"); |
* limitations under the License. | * limitations under the License. | ||||
*/ | */ | ||||
/* $Id$ */ | |||||
package org.apache.fop.render.afp; | package org.apache.fop.render.afp; | ||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.net.URI; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.xml.sax.SAXException; | |||||
import org.mockito.ArgumentCaptor; | |||||
import org.apache.fop.afp.AFPPaintingState; | |||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.afp.AFPResourceLevel; | |||||
import org.apache.fop.afp.AFPResourceLevel.ResourceType; | |||||
import org.apache.fop.afp.AFPResourceLevelDefaults; | |||||
import org.apache.fop.apps.AFPRendererConfBuilder; | |||||
import org.apache.fop.apps.AbstractRendererConfiguratorTest; | |||||
import org.apache.fop.apps.MimeConstants; | |||||
import org.apache.fop.render.afp.AFPRendererConfig.AFPRendererConfigParser; | import org.apache.fop.render.afp.AFPRendererConfig.AFPRendererConfigParser; | ||||
import org.apache.fop.render.intermediate.IFContext; | |||||
import org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions; | |||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.fail; | |||||
import static org.mockito.Mockito.verify; | |||||
/** | |||||
* Test case for {@link AFPRendererConfigurator}. | |||||
*/ | |||||
public class AFPRendererConfiguratorTestCase { | |||||
private FOUserAgent userAgent; | |||||
private AFPRendererConfigurator sut; | |||||
/** | |||||
* Assigns an FOUserAgen with a config file at <code>uri</code> | |||||
* | |||||
* @param uri the URI of the config file | |||||
*/ | |||||
private void setConfigFile(String uri) { | |||||
String confTestsDir = "test/resources/conf/afp/"; | |||||
try { | |||||
userAgent = FopFactory.newInstance(new File(confTestsDir + uri)).newFOUserAgent(); | |||||
sut = new AFPRendererConfigurator(userAgent, new AFPRendererConfigParser()); | |||||
} catch (IOException ioe) { | |||||
fail("IOException: " + ioe); | |||||
} catch (SAXException se) { | |||||
fail("SAXException: " + se); | |||||
public class AFPRendererConfiguratorTestCase extends | |||||
AbstractRendererConfiguratorTest<AFPRendererConfigurator, AFPRendererConfBuilder> { | |||||
public AFPRendererConfiguratorTestCase() { | |||||
super(MimeConstants.MIME_AFP, AFPRendererConfBuilder.class, AFPDocumentHandler.class); | |||||
} | |||||
@Override | |||||
public void setUpDocumentHandler() { | |||||
} | |||||
@Override | |||||
protected AFPRendererConfigurator createConfigurator() { | |||||
return new AFPRendererConfigurator(userAgent, new AFPRendererConfigParser()); | |||||
} | |||||
private AFPDocumentHandler getDocHandler() { | |||||
return (AFPDocumentHandler) docHandler; | |||||
} | |||||
@Test | |||||
public void testColorImages() throws Exception { | |||||
parseConfig(createBuilder().startImages(ImagesModeOptions.MODE_COLOR) | |||||
.endImages()); | |||||
verify(getDocHandler()).setColorImages(true); | |||||
parseConfig(createBuilder().startImages(ImagesModeOptions.MODE_GRAYSCALE) | |||||
.endImages()); | |||||
verify(getDocHandler()).setColorImages(false); | |||||
} | |||||
@Test | |||||
public void testCMYKImagesSupport() throws Exception { | |||||
parseConfig(createBuilder().startImages(ImagesModeOptions.MODE_COLOR) | |||||
.setModeAttribute("cmyk", "true") | |||||
.endImages()); | |||||
verify(getDocHandler()).setCMYKImagesSupported(true); | |||||
parseConfig(createBuilder().startImages(ImagesModeOptions.MODE_COLOR) | |||||
.setModeAttribute("cmyk", "false") | |||||
.endImages()); | |||||
verify(getDocHandler()).setCMYKImagesSupported(false); | |||||
} | |||||
@Test | |||||
public void testBitsPerPixel() throws Exception { | |||||
for (int bpp = 0; bpp < 40; bpp += 8) { | |||||
parseConfig(createBuilder().startImages() | |||||
.setModeAttribute("bits-per-pixel", String.valueOf(bpp)) | |||||
.endImages()); | |||||
verify(getDocHandler()).setBitsPerPixel(bpp); | |||||
} | |||||
} | |||||
@Test | |||||
public void testDitheringQuality() throws Exception { | |||||
float ditheringQuality = 100f; | |||||
parseConfig(createBuilder().startImages() | |||||
.setDitheringQuality(ditheringQuality) | |||||
.endImages()); | |||||
verify(getDocHandler()).setDitheringQuality(ditheringQuality); | |||||
ditheringQuality = 1000f; | |||||
parseConfig(createBuilder().startImages() | |||||
.setDitheringQuality(ditheringQuality) | |||||
.endImages()); | |||||
verify(getDocHandler()).setDitheringQuality(ditheringQuality); | |||||
} | |||||
@Test | |||||
public void testNativeImagesSupported() throws Exception { | |||||
parseConfig(createBuilder().startImages() | |||||
.setNativeImageSupport(true) | |||||
.endImages()); | |||||
verify(getDocHandler()).setNativeImagesSupported(true); | |||||
parseConfig(createBuilder().startImages() | |||||
.setNativeImageSupport(false) | |||||
.endImages()); | |||||
verify(getDocHandler()).setNativeImagesSupported(false); | |||||
} | |||||
@Test | |||||
public void testShadingMode() throws Exception { | |||||
for (AFPShadingMode mode : AFPShadingMode.values()) { | |||||
parseConfig(createBuilder().setShading(mode)); | |||||
verify(getDocHandler()).setShadingMode(mode); | |||||
} | } | ||||
} | } | ||||
/** | |||||
* Test several config files relating to JPEG images in AFP. | |||||
* | |||||
* @throws FOPException if an error is thrown | |||||
*/ | |||||
@Test | @Test | ||||
public void testJpegImageConfig() throws FOPException { | |||||
testJpegSettings("no_image_config.xconf", 1.0f, false); | |||||
testJpegSettings("can_embed_jpeg.xconf", 1.0f, true); | |||||
testJpegSettings("bitmap_encode_quality.xconf", 0.5f, false); | |||||
public void testRendererResolution() throws Exception { | |||||
for (int resolution = 0; resolution < 1000; resolution += 100) { | |||||
parseConfig(createBuilder().setRenderingResolution(resolution)); | |||||
verify(getDocHandler()).setResolution(resolution); | |||||
} | |||||
} | |||||
@Test | |||||
public void testLineWidthCorrection() throws Exception { | |||||
for (float resolution = 0; resolution < 50; resolution += 5) { | |||||
parseConfig(createBuilder().setLineWidthCorrection(resolution)); | |||||
verify(getDocHandler()).setLineWidthCorrection(resolution); | |||||
} | |||||
} | |||||
@Test | |||||
public void testResourceGroupURI() throws Exception { | |||||
URI uri = URI.create("test://URI/just/used/for/testing"); | |||||
parseConfig(createBuilder().setResourceGroupUri(uri.toASCIIString())); | |||||
verify(getDocHandler()).setDefaultResourceGroupUri(uri); | |||||
} | |||||
@Test | |||||
public void testResourceLevelDefaults() throws Exception { | |||||
testResourceLevelDefault(ResourceType.DOCUMENT); | |||||
} | |||||
private void testResourceLevelDefault(ResourceType resType) throws Exception { | |||||
Map<String, String> resourceLevels = new HashMap<String, String>(); | |||||
resourceLevels.put("goca", resType.getName()); | |||||
parseConfig(createBuilder().setDefaultResourceLevels(resourceLevels)); | |||||
ArgumentCaptor<AFPResourceLevelDefaults> argument = ArgumentCaptor.forClass(AFPResourceLevelDefaults.class); | |||||
verify(getDocHandler()).setResourceLevelDefaults(argument.capture()); | |||||
AFPResourceLevel expectedLevel = new AFPResourceLevel(resType); | |||||
assertEquals(expectedLevel, argument.getValue().getDefaultResourceLevel((byte) 3)); | |||||
} | |||||
@Test | |||||
public void testExternalResourceDefault() throws Exception { | |||||
testResourceLevelDefault(ResourceType.EXTERNAL); | |||||
} | } | ||||
private void testJpegSettings(String uri, float bitmapEncodingQual, boolean canEmbed) | |||||
throws FOPException { | |||||
AFPDocumentHandler docHandler = new AFPDocumentHandler(new IFContext(userAgent)); | |||||
@Test | |||||
public void testInlineResourceDefault() throws Exception { | |||||
testResourceLevelDefault(ResourceType.INLINE); | |||||
} | |||||
@Test | |||||
public void testPageResourceDefault() throws Exception { | |||||
testResourceLevelDefault(ResourceType.PAGE); | |||||
} | |||||
@Test | |||||
public void testPageGroupResourceDefault() throws Exception { | |||||
testResourceLevelDefault(ResourceType.PAGE_GROUP); | |||||
} | |||||
setConfigFile(uri); | |||||
sut.configure(docHandler); | |||||
@Test | |||||
public void testPrintFileResourceDefault() throws Exception { | |||||
testResourceLevelDefault(ResourceType.PRINT_FILE); | |||||
} | |||||
@Test | |||||
public void testBitmapEncodeQuality() throws Exception { | |||||
parseConfig(createBuilder().startImages() | |||||
.setBitmapEncodingQuality(0.5f) | |||||
.endImages()); | |||||
verify(getDocHandler()).setBitmapEncodingQuality(0.5f); | |||||
} | |||||
@Test | |||||
public void testCanEmbedJpeg() throws Exception { | |||||
parseConfig(createBuilder().startImages() | |||||
.setAllowJpegEmbedding(true) | |||||
.endImages()); | |||||
verify(getDocHandler()).canEmbedJpeg(true); | |||||
AFPPaintingState paintingState = docHandler.getPaintingState(); | |||||
assertEquals(bitmapEncodingQual, paintingState.getBitmapEncodingQuality(), 0.01f); | |||||
assertEquals(canEmbed, paintingState.canEmbedJpeg()); | |||||
parseConfig(createBuilder().startImages() | |||||
.setAllowJpegEmbedding(false) | |||||
.endImages()); | |||||
verify(getDocHandler()).canEmbedJpeg(false); | |||||
} | } | ||||
} | } |