Revision 7824
Added by ben leinfelder almost 11 years ago
DocumentImpl.java | ||
---|---|---|
31 | 31 |
import java.io.ByteArrayInputStream; |
32 | 32 |
import java.io.ByteArrayOutputStream; |
33 | 33 |
import java.io.File; |
34 |
import java.io.FileInputStream; |
|
34 | 35 |
import java.io.FileOutputStream; |
35 | 36 |
import java.io.IOException; |
36 | 37 |
import java.io.InputStream; |
... | ... | |
1058 | 1059 |
* matter withinInlineData's value, the document will |
1059 | 1060 |
* |
1060 | 1061 |
*/ |
1061 |
public void toXml(OutputStream out, String user, String[] groups, boolean withInLineData)
|
|
1062 |
public InputStream toXml(OutputStream out, String user, String[] groups, boolean withInLineData)
|
|
1062 | 1063 |
throws McdbException { |
1063 | 1064 |
String documentDir = null; |
1064 | 1065 |
String documentPath = null; |
... | ... | |
1084 | 1085 |
throw new McdbException("Attempting to read a zero length document from disk: " + documentPath); |
1085 | 1086 |
} |
1086 | 1087 |
|
1087 |
readFromFileSystem(out, user, groups, documentPath); |
|
1088 |
return readFromFileSystem(out, user, groups, documentPath);
|
|
1088 | 1089 |
} |
1089 | 1090 |
|
1090 | 1091 |
/** |
... | ... | |
1406 | 1407 |
* the location of the document on disk |
1407 | 1408 |
* |
1408 | 1409 |
*/ |
1409 |
public void readFromFileSystem(
|
|
1410 |
public InputStream readFromFileSystem(
|
|
1410 | 1411 |
OutputStream out, String user, String[] groups, String documentPath) throws McdbException { |
1411 | 1412 |
|
1412 |
String xmlFileContents = ""; |
|
1413 |
String xmlFileContents = null; |
|
1414 |
String encoding = null; |
|
1413 | 1415 |
|
1414 | 1416 |
try { |
1415 |
// detect and use correct encoding |
|
1416 |
xmlFileContents = FileUtil.readFileToString(documentPath); |
|
1417 |
// guess the encoding from default bytes |
|
1418 |
XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlFileContents.getBytes())); |
|
1419 |
String encoding = xsr.getEncoding(); |
|
1420 |
// reread the contents using the correct encoding |
|
1421 |
if (encoding != null) { |
|
1422 |
xmlFileContents = FileUtil.readFileToString(documentPath, encoding); |
|
1423 |
} |
|
1424 | 1417 |
|
1425 | 1418 |
// get a list of inline data sections that are not readable |
1426 | 1419 |
// by this user |
... | ... | |
1431 | 1424 |
// If this is for each unreadable section, strip the inline data |
1432 | 1425 |
// from the doc |
1433 | 1426 |
if (unReadableInlineDataList.size() > 0 && doctype != null) { |
1427 |
|
|
1428 |
// detect and use correct encoding |
|
1429 |
xmlFileContents = FileUtil.readFileToString(documentPath); |
|
1430 |
// guess the encoding from default bytes |
|
1431 |
XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlFileContents.getBytes())); |
|
1432 |
encoding = xsr.getEncoding(); |
|
1433 |
xsr.close(); |
|
1434 |
// reread the contents using the correct encoding |
|
1435 |
if (encoding != null) { |
|
1436 |
xmlFileContents = FileUtil.readFileToString(documentPath, encoding); |
|
1437 |
} |
|
1438 |
|
|
1434 | 1439 |
Set<String> inlineKeySet = unReadableInlineDataList.keySet(); |
1435 |
|
|
1436 | 1440 |
boolean pre210Doc = doctype.equals(EML2_0_0NAMESPACE) |
1437 | 1441 |
|| doctype.equals(EML2_0_1NAMESPACE); |
1438 | 1442 |
|
... | ... | |
1443 | 1447 |
// on whether this is a 2.0.1 or earlier doc or 2.1.0 and later. This |
1444 | 1448 |
// is because of eml schema changes for inline access. |
1445 | 1449 |
if (pre210Doc) { |
1446 |
xmlFileContents = stripInline20XData(xmlFileContents, |
|
1447 |
inlineKey); |
|
1450 |
xmlFileContents = stripInline20XData(xmlFileContents, inlineKey); |
|
1448 | 1451 |
} else { |
1449 | 1452 |
xmlFileContents = stripInline21XData(xmlFileContents, inlineKey); |
1450 | 1453 |
} |
... | ... | |
1452 | 1455 |
} |
1453 | 1456 |
} |
1454 | 1457 |
|
1455 |
// write with the detected encoding |
|
1456 |
Writer pw = new OutputStreamWriter(out, encoding); |
|
1457 |
// write the doc |
|
1458 |
pw.write(xmlFileContents); |
|
1459 |
pw.close(); |
|
1458 |
// will get input either from string content or file on disk |
|
1459 |
InputStream is = null; |
|
1460 | 1460 |
|
1461 |
// get the input stream |
|
1462 |
if (xmlFileContents != null) { |
|
1463 |
is = IOUtils.toInputStream(xmlFileContents, encoding); |
|
1464 |
} else { |
|
1465 |
is = new FileInputStream(documentPath); |
|
1466 |
} |
|
1467 |
|
|
1468 |
// send it to out |
|
1469 |
if (out != null) { |
|
1470 |
IOUtils.copyLarge(is, out); |
|
1471 |
} |
|
1472 |
// return the stream |
|
1473 |
return is; |
|
1474 |
|
|
1461 | 1475 |
} catch (UtilException e) { |
1462 | 1476 |
throw new McdbException(e.getMessage()); |
1463 | 1477 |
} catch (IOException e) { |
1464 | 1478 |
throw new McdbException(e.getMessage()); |
1465 | 1479 |
} |
1480 |
|
|
1466 | 1481 |
} |
1467 | 1482 |
|
1468 | 1483 |
/** |
Also available in: Unified diff
do not use tmp file to return an inputstream on read() operations - just read from the file we already have. https://projects.ecoinformatics.org/ecoinfo/issues/6009