}
}*/
// Construct URI ID map, this map is used for resolving URIs to record IDs during the generation process.
uriIdMap.clear();
for (Iterator iterator = recordList.iterator(); iterator.hasNext();) {
Record record = (Record) iterator.next();
if (record instanceof LinkableRecord) {
LinkableRecord linkableRecord = (LinkableRecord) record;
Integer id = new Integer(record.getRecordId());
String uri = linkableRecord.getURI();
uriIdMap.put(uri, id);
if (linkableRecord.getAlternateURI() != null) {
uriIdMap.put(linkableRecord.getAlternateURI(), id);
}
}
}
if (includeURIInfo) {
/*
If we include URI info, we must first construct URI index and data records, then write the PDB and
finally remove the URI index and data records. We do this because we might want to create multiple
versions of the Document and in between writing different PDBs we might add a record or change a setting.
*/
// Create URI index and URI data records
List uriDataRecordList = new ArrayList();
List uriList = new ArrayList();
/*
Create URI data for URIs that resolve to records in the pluckerDocument, taking into account the maximum size
of 200 URIs per URI data record.
*/
int uriRecordCount = 1 + (recordList.size() / 200) + (((recordList.size() % 200) > 0) ? 1 : 0);
for (int i = 0, j = recordList.size(); i < j; i++) {
Record record = (Record) recordList.get(i);
if (record instanceof TextRecord || record instanceof ImageRecord) {
uriList.add(((LinkableRecord) record).getURI());
} else {
uriList.add("");
}
if ((uriList.size() == 200) || (i == (j - 1))) {
URIDataRecord uriDataRecord = new URIDataRecord((String[]) uriList.toArray(new String[uriList.size()]),
record.getRecordId());
uriDataRecordList.add(uriDataRecord);
uriList.clear();
}
}
// Create a list of unresolved links.
List unresolvedLinkList = new ArrayList();
Set uriSet = uriIdMap.keySet();
for (Iterator iterator = recordList.iterator(); iterator.hasNext();) {
Record record = (Record) iterator.next();
if (record instanceof TextRecord) {
TextRecord textRecord = (TextRecord) record;
String[] uris = textRecord.getLinkURIs();
for (int i = 0; i < uris.length; i++) {
String uri = URIUtil.removeAnchor(uris[i]);
if (!uriSet.contains(uri) && !unresolvedLinkList.contains(uri)) {
unresolvedLinkList.add(uri);
}
}
}
}
/*
Add the unresolved links to the URI data. We use dummy record IDs, beyond the range of actual
record IDs, for the URI data
*/
uriRecordCount += ((unresolvedLinkList.size() / 200) +
(((unresolvedLinkList.size() % 200) > 0) ? 1 : 0));
for (int i = 0; i < uriRecordCount; i++) {
uriList.add("");
}
int dummyRecordId = recordList.size() + uriRecordCount + 1;
for (int i = 0, j = unresolvedLinkList.size(); i < j; i++) {
String uri = (String) unresolvedLinkList.get(i);
uriList.add(uri);
uriIdMap.put(uri, new Integer(dummyRecordId++));
if ((uriList.size() == 200) || (i == (j - 1))) {
URIDataRecord uriDataRecord = new URIDataRecord((String[]) uriList.toArray(new String[uriList.size()]),
dummyRecordId++);
uriDataRecordList.add(uriDataRecord);
uriList.clear();
}
}
URIDataRecord[] uriDataRecords = (URIDataRecord[]) uriDataRecordList.toArray(new URIDataRecord[uriDataRecordList.size()]);
// Add URI index record and the URI data records
URIIndexRecord uriIndexRecord = new URIIndexRecord(uriDataRecords);
addRecord(uriIndexRecord);
for (int i = 0; i < uriDataRecords.length; i++) {
URIDataRecord uriDataRecord = uriDataRecords[i];
addRecord(uriDataRecord);
}
try {
// Write the PDB
size = super.write(out);
} finally {
// Now remove the URI index and data records
removeRecord(uriIndexRecord);
for (int i = 0; i < uriDataRecords.length; i++) {
URIDataRecord uriDataRecord = uriDataRecords[i];
removeRecord(uriDataRecord);
}
}
} else {
// If we do not include URI info we can just write the PDB without any special considerations.
size = super.write(out);
}
for (int i = 0; i < recordList.size(); i++) {
Record record = (Record) recordList.get(i);
if (record instanceof ImageRecord) {
ImageRecord imageRecord = (ImageRecord) record;
if (imageRecord.getURI().endsWith(".fullsize")) {
removeRecord(imageRecord);
i--;