Search in sources :

Example 1 with XmlFileParseException

use of org.craftercms.core.exception.XmlFileParseException in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItem.

@Override
protected Item doFindItem(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File file = findFile(context, path);
    if (file == null) {
        return null;
    }
    Item item = new Item();
    item.setName(file.getName());
    item.setUrl(path);
    item.setFolder(file.isDirectory());
    if (withDescriptor) {
        File descriptorFile;
        // a DOM.
        if (file.isFile() && item.getName().endsWith(descriptorFileExtension)) {
            item.setDescriptorUrl(path);
            descriptorFile = file;
        // If it's not a file (a dir) or is not a descriptor (a static asset, like an image), locate the file's
        // descriptor by appending a metadata file extension to the file name. If the file exists, load it as
        // a DOM.
        } else {
            String descriptorPath = FilenameUtils.removeExtension(path) + metadataFileExtension;
            item.setDescriptorUrl(descriptorPath);
            descriptorFile = findFile(context, descriptorPath);
            if (descriptorFile != null && !descriptorFile.isFile()) {
                throw new StoreException("Descriptor file at " + descriptorFile + " is not really a file");
            }
        }
        if (descriptorFile != null) {
            try {
                InputStream fileInputStream = new BufferedInputStream(descriptorFile.getInputStream());
                Reader fileReader = new InputStreamReader(fileInputStream, charset);
                try {
                    item.setDescriptorDom(createXmlReader().read(fileReader));
                } finally {
                    IOUtils.closeQuietly(fileReader);
                }
            } catch (IOException e) {
                throw new StoreException("Unable to open input stream for descriptor file at " + descriptorFile, e);
            } catch (DocumentException e) {
                throw new XmlFileParseException("Error while parsing xml document at " + descriptorFile, e);
            }
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) DocumentException(org.dom4j.DocumentException) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) StoreException(org.craftercms.core.exception.StoreException) XmlFileParseException(org.craftercms.core.exception.XmlFileParseException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StoreException (org.craftercms.core.exception.StoreException)1 XmlFileParseException (org.craftercms.core.exception.XmlFileParseException)1 Item (org.craftercms.core.service.Item)1 DocumentException (org.dom4j.DocumentException)1 SAXReader (org.dom4j.io.SAXReader)1