|
|
|
|
|
|
|
|
throws UnsupportedEncodingException { |
|
|
throws UnsupportedEncodingException { |
|
|
//The first 8 bytes of the field data represent the resource name |
|
|
//The first 8 bytes of the field data represent the resource name |
|
|
byte[] nameBytes = new byte[8]; |
|
|
byte[] nameBytes = new byte[8]; |
|
|
System.arraycopy(field.getData(), 0, nameBytes, 0, 8); |
|
|
|
|
|
String asciiName; |
|
|
|
|
|
asciiName = new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); |
|
|
|
|
|
return asciiName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] fieldData = field.getData(); |
|
|
|
|
|
if (fieldData.length < 8) { |
|
|
|
|
|
throw new IllegalArgumentException("Field data does not contain a resource name"); |
|
|
|
|
|
} |
|
|
|
|
|
System.arraycopy(fieldData, 0, nameBytes, 0, 8); |
|
|
|
|
|
return new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
public static void copyNamedResource(String name, |
|
|
public static void copyNamedResource(String name, |
|
|
final InputStream in, final OutputStream out) throws IOException { |
|
|
final InputStream in, final OutputStream out) throws IOException { |
|
|
final MODCAParser parser = new MODCAParser(in); |
|
|
final MODCAParser parser = new MODCAParser(in); |
|
|
Collection resourceNames = new java.util.HashSet(); |
|
|
|
|
|
|
|
|
Collection<String> resourceNames = new java.util.HashSet<String>(); |
|
|
|
|
|
|
|
|
//Find matching "Begin" field |
|
|
//Find matching "Begin" field |
|
|
final UnparsedStructuredField fieldBegin; |
|
|
final UnparsedStructuredField fieldBegin; |
|
|
while (true) { |
|
|
while (true) { |
|
|
UnparsedStructuredField field = parser.readNextStructuredField(); |
|
|
|
|
|
|
|
|
final UnparsedStructuredField field = parser.readNextStructuredField(); |
|
|
|
|
|
|
|
|
if (field == null) { |
|
|
if (field == null) { |
|
|
throw new IOException("Requested resource '" + name |
|
|
throw new IOException("Requested resource '" + name |
|
|
+ "' not found. Encountered resource names: " + resourceNames); |
|
|
+ "' not found. Encountered resource names: " + resourceNames); |
|
|
|
|
|
|
|
|
if (field.getSfTypeCode() != TYPE_CODE_BEGIN) { //0xA8=Begin |
|
|
if (field.getSfTypeCode() != TYPE_CODE_BEGIN) { //0xA8=Begin |
|
|
continue; //Not a "Begin" field |
|
|
continue; //Not a "Begin" field |
|
|
} |
|
|
} |
|
|
String resourceName = getResourceName(field); |
|
|
|
|
|
|
|
|
final String resourceName = getResourceName(field); |
|
|
|
|
|
|
|
|
resourceNames.add(resourceName); |
|
|
resourceNames.add(resourceName); |
|
|
|
|
|
|
|
|
if (resourceName.equals(name)) { |
|
|
if (resourceName.equals(name)) { |
|
|
if (LOG.isDebugEnabled()) { |
|
|
if (LOG.isDebugEnabled()) { |
|
|
LOG.debug("Start of requested structured field found:\n" |
|
|
LOG.debug("Start of requested structured field found:\n" |
|
|
|
|
|
|
|
|
if (wrapInResource) { |
|
|
if (wrapInResource) { |
|
|
ResourceObject resourceObject = new ResourceObject(name) { |
|
|
ResourceObject resourceObject = new ResourceObject(name) { |
|
|
protected void writeContent(OutputStream os) throws IOException { |
|
|
protected void writeContent(OutputStream os) throws IOException { |
|
|
copyStructuredFields(name, fieldBegin, parser, out); |
|
|
|
|
|
|
|
|
copyNamedStructuredFields(name, fieldBegin, parser, out); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
resourceObject.setType(ResourceObject.TYPE_PAGE_SEGMENT); |
|
|
resourceObject.setType(ResourceObject.TYPE_PAGE_SEGMENT); |
|
|
resourceObject.writeToStream(out); |
|
|
resourceObject.writeToStream(out); |
|
|
} else { |
|
|
} else { |
|
|
copyStructuredFields(name, fieldBegin, parser, out); |
|
|
|
|
|
|
|
|
copyNamedStructuredFields(name, fieldBegin, parser, out); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static void copyStructuredFields(String name, UnparsedStructuredField fieldBegin, |
|
|
|
|
|
MODCAParser parser, OutputStream out) throws IOException { |
|
|
|
|
|
boolean inRequestedResource; |
|
|
|
|
|
|
|
|
private static void copyNamedStructuredFields(final String name, |
|
|
|
|
|
UnparsedStructuredField fieldBegin, MODCAParser parser, |
|
|
|
|
|
OutputStream out) throws IOException { |
|
|
|
|
|
|
|
|
//The "Begin" field first |
|
|
|
|
|
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); |
|
|
|
|
|
fieldBegin.writeTo(out); |
|
|
|
|
|
UnparsedStructuredField field; |
|
|
|
|
|
|
|
|
UnparsedStructuredField field = fieldBegin; |
|
|
|
|
|
|
|
|
//Then the rest of the fields until the corresponding "End" field |
|
|
|
|
|
inRequestedResource = true; |
|
|
|
|
|
do { |
|
|
|
|
|
field = parser.readNextStructuredField(); |
|
|
|
|
|
|
|
|
while (true) { |
|
|
if (field == null) { |
|
|
if (field == null) { |
|
|
break; //Unexpected EOF |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (field.getSfTypeCode() == TYPE_CODE_END) { |
|
|
|
|
|
String resourceName = getResourceName(field); |
|
|
|
|
|
if (resourceName.equals(name)) { |
|
|
|
|
|
inRequestedResource = false; //Signal end of loop |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
throw new IOException("Ending structured field not found for resource " + name); |
|
|
} |
|
|
} |
|
|
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); |
|
|
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); |
|
|
field.writeTo(out); |
|
|
field.writeTo(out); |
|
|
} while (inRequestedResource); |
|
|
|
|
|
if (inRequestedResource) { |
|
|
|
|
|
throw new IOException("Ending structured field not found for resource " + name); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (field.getSfTypeCode() == TYPE_CODE_END |
|
|
|
|
|
&& fieldBegin.getSfCategoryCode() == field.getSfCategoryCode() |
|
|
|
|
|
&& name.equals(getResourceName(field))) { |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
field = parser.readNextStructuredField(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |