use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class XMLDBStore method evalWithCollection.
@Override
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
String docName = args[1].isEmpty() ? null : args[1].getStringValue();
if (docName != null && docName.isEmpty()) {
docName = null;
} else if (docName != null) {
docName = new AnyURIValue(docName).toXmldbURI().toString();
}
final Item item = args[2].itemAt(0);
// determine the mime type
final boolean storeAsBinary = isCalledAs(FS_STORE_BINARY_NAME);
MimeType mimeType = null;
if (getSignature().getArgumentCount() == 4) {
final String strMimeType = args[3].getStringValue();
mimeType = MimeTable.getInstance().getContentType(strMimeType);
}
if (mimeType == null && docName != null) {
mimeType = MimeTable.getInstance().getContentTypeFor(docName);
}
if (mimeType == null) {
mimeType = (storeAsBinary || !Type.subTypeOf(item.getType(), Type.NODE)) ? MimeType.BINARY_TYPE : MimeType.XML_TYPE;
} else if (storeAsBinary) {
mimeType = new MimeType(mimeType.getName(), MimeType.BINARY);
}
Resource resource;
try {
if (Type.subTypeOf(item.getType(), Type.JAVA_OBJECT)) {
final Object obj = ((JavaObjectValue) item).getObject();
if (obj instanceof java.io.File) {
resource = loadFromFile(collection, ((java.io.File) obj).toPath(), docName, mimeType);
} else if (obj instanceof java.nio.file.Path) {
resource = loadFromFile(collection, (Path) obj, docName, mimeType);
} else {
LOGGER.error("Passed java object should be either a java.nio.file.Path or java.io.File");
throw new XPathException(this, "Passed java object should be either a java.nio.file.Path or java.io.File");
}
} else if (Type.subTypeOf(item.getType(), Type.ANY_URI)) {
try {
final URI uri = new URI(item.getStringValue());
resource = loadFromURI(collection, uri, docName, mimeType);
} catch (final URISyntaxException e) {
LOGGER.error("Invalid URI: {}", item.getStringValue());
throw new XPathException(this, "Invalid URI: " + item.getStringValue(), e);
}
} else {
if (mimeType.isXMLType()) {
resource = collection.createResource(docName, "XMLResource");
} else {
resource = collection.createResource(docName, "BinaryResource");
}
if (Type.subTypeOf(item.getType(), Type.STRING)) {
resource.setContent(item.getStringValue());
} else if (item.getType() == Type.BASE64_BINARY) {
resource.setContent(((BinaryValue) item).toJavaObject());
} else if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (mimeType.isXMLType()) {
final ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
handler.startDocument();
item.toSAX(context.getBroker(), handler, SERIALIZATION_PROPERTIES);
handler.endDocument();
} else {
try (final StringWriter writer = new StringWriter()) {
final SAXSerializer serializer = new SAXSerializer();
serializer.setOutput(writer, null);
item.toSAX(context.getBroker(), serializer, SERIALIZATION_PROPERTIES);
resource.setContent(writer.toString());
} catch (final IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
} else {
LOGGER.error("Data should be either a node or a string");
throw new XPathException(this, "Data should be either a node or a string");
}
((EXistResource) resource).setMimeType(mimeType.getName());
collection.storeResource(resource);
}
} catch (final XMLDBException e) {
LOGGER.error(e.getMessage(), e);
throw new XPathException(this, "XMLDB reported an exception while storing document: " + e.getMessage(), e);
} catch (final SAXException e) {
LOGGER.error(e.getMessage());
throw new XPathException(this, "SAX reported an exception while storing document", e);
}
if (resource == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
try {
// TODO : use dedicated function in XmldbURI
return new StringValue(collection.getName() + "/" + resource.getId());
} catch (final XMLDBException e) {
LOGGER.error(e.getMessage());
throw new XPathException(this, "XMLDB reported an exception while retrieving the " + "stored document", e);
}
}
}
use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class XMLDBLoadFromPattern method evalWithCollection.
@Override
protected Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
final Path baseDir = Paths.get(args[1].getStringValue()).normalize();
logger.debug("Loading files from directory: {}", baseDir.toAbsolutePath().toString());
final Sequence patternsSeq = args[2];
final int patternsLen = patternsSeq.getItemCount();
final String[] includes = new String[patternsLen];
for (int i = 0; i < patternsLen; i++) {
includes[i] = patternsSeq.itemAt(0).getStringValue();
}
// determine resource type - xml or binary?
MimeType mimeTypeFromArgs = null;
if (getSignature().getArgumentCount() > 3 && args[3].hasOne()) {
final String mimeTypeParam = args[3].getStringValue();
mimeTypeFromArgs = MimeTable.getInstance().getContentType(mimeTypeParam);
if (mimeTypeFromArgs == null) {
throw new XPathException(this, "Unknown mime type specified: " + mimeTypeParam);
}
}
// keep the directory structure?
boolean keepDirStructure = false;
if (getSignature().getArgumentCount() >= 5) {
keepDirStructure = args[4].effectiveBooleanValue();
}
final String[] excludes;
if (getSignature().getArgumentCount() == 6) {
final Sequence excludesSeq = args[5];
final int excludesLen = excludesSeq.getItemCount();
excludes = new String[excludesLen];
for (int i = 0; i < excludesLen; i++) {
excludes[i] = excludesSeq.itemAt(i).getStringValue();
}
} else {
excludes = null;
}
final ValueSequence stored = new ValueSequence();
// scan for files
final DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setIncludes(includes);
directoryScanner.setExcludes(excludes);
directoryScanner.setBasedir(baseDir.toFile());
directoryScanner.setCaseSensitive(true);
directoryScanner.scan();
Collection col = collection;
String relDir;
String prevDir = null;
// store according to each pattern
for (final String includedFile : directoryScanner.getIncludedFiles()) {
final Path file = baseDir.resolve(includedFile);
try {
if (logger.isDebugEnabled()) {
logger.debug(file.toAbsolutePath().toString());
}
String relPath = file.toString().substring(baseDir.toString().length());
final int p = relPath.lastIndexOf(java.io.File.separatorChar);
if (p >= 0) {
relDir = relPath.substring(0, p);
relDir = relDir.replace(java.io.File.separatorChar, '/');
} else {
relDir = relPath;
}
if (keepDirStructure && (prevDir == null || (!relDir.equals(prevDir)))) {
col = createCollectionPath(collection, relDir);
prevDir = relDir;
}
MimeType mimeType = mimeTypeFromArgs;
if (mimeType == null) {
mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
if (mimeType == null) {
mimeType = MimeType.BINARY_TYPE;
}
}
// TODO : these probably need to be encoded and checked for right mime type
final Resource resource = col.createResource(FileUtils.fileName(file), mimeType.getXMLDBType());
resource.setContent(file.toFile());
((EXistResource) resource).setMimeType(mimeType.getName());
col.storeResource(resource);
// TODO : use dedicated function in XmldbURI
stored.add(new StringValue(col.getName() + "/" + resource.getId()));
} catch (final XMLDBException e) {
logger.error("Could not store file {}: {}", file.toAbsolutePath(), e.getMessage());
}
}
return stored;
}
Aggregations