Search in sources :

Example 1 with ContentReader

use of org.apache.sling.jcr.contentloader.ContentReader in project sling by apache.

the class BundleContentLoader method getParentNodeDescriptor.

/**
     * Return the parent node descriptor (ROOT).
     */
private Descriptor getParentNodeDescriptor(final Bundle bundle, final String path, final DefaultContentCreator contentCreator) {
    for (Map.Entry<String, ContentReader> entry : contentCreator.getContentReaders().entrySet()) {
        if (entry.getValue() != null) {
            final StringBuilder filePath = new StringBuilder(path);
            if (!path.endsWith("/")) {
                filePath.append("/");
            }
            filePath.append(PARENT_DESCRIPTOR);
            // add file extension, e.g. .jcr.xml, .xml, .zip (see BaseImportLoader)
            filePath.append(entry.getKey());
            URL url = bundle.getEntry(filePath.toString());
            if (url != null) {
                final Descriptor descriptor = new Descriptor();
                descriptor.url = url;
                descriptor.contentReader = entry.getValue();
                return descriptor;
            }
        }
    }
    return null;
}
Also used : ContentReader(org.apache.sling.jcr.contentloader.ContentReader) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL)

Example 2 with ContentReader

use of org.apache.sling.jcr.contentloader.ContentReader in project sling by apache.

the class BundleContentLoader method createNode.

/**
     * Create a new node from a content resource found in the bundle.
     *
     * @param parent         The parent node
     * @param name           The name of the new content node
     * @param resourceUrl    The resource url.
     * @param contentCreator the content creator
     * @param configuration  the configuration for the node that needs to be created
     * @return
     * @throws RepositoryException
     */
private Node createNode(Node parent, String name, URL resourceUrl, final DefaultContentCreator contentCreator, PathEntry configuration) throws RepositoryException {
    final String resourcePath = resourceUrl.getPath().toLowerCase();
    InputStream contentStream = null;
    try {
        // special treatment for system view imports
        if (resourcePath.endsWith(EXT_JCR_XML)) {
            contentStream = resourceUrl.openStream();
            return importJcrXml(parent, name, contentStream, false);
        }
        // get the node reader for this resource
        final ContentReader nodeReader = getContentReader(resourcePath, configuration);
        // cannot find out the type
        if (nodeReader == null) {
            return null;
        }
        final String contentReaderExtension = getContentReaderExtension(name);
        contentCreator.prepareParsing(parent, toPlainName(name, contentReaderExtension));
        nodeReader.parse(resourceUrl, contentCreator);
        return contentCreator.getCreatedRootNode();
    } catch (RepositoryException re) {
        throw re;
    } catch (Throwable t) {
        throw new RepositoryException(t.getMessage(), t);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }
}
Also used : InputStream(java.io.InputStream) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) RepositoryException(javax.jcr.RepositoryException)

Example 3 with ContentReader

use of org.apache.sling.jcr.contentloader.ContentReader in project sling by apache.

the class DefaultContentImporter method importContent.

/* (non-Javadoc)
     * @see org.apache.sling.jcr.contentloader.ContentImporter#importContent(javax.jcr.Node, java.lang.String, java.io.InputStream, org.apache.sling.jcr.contentloader.ImportOptions, org.apache.sling.jcr.contentloader.ContentImportListener)
     */
public void importContent(Node parent, String filename, InputStream contentStream, ImportOptions importOptions, ContentImportListener importListener) throws RepositoryException, IOException {
    // special treatment for system view imports
    if (filename.endsWith(EXT_JCR_XML)) {
        importJcrXml(parent, filename, contentStream, importOptions, importListener);
        return;
    }
    final DefaultContentCreator contentCreator = new DefaultContentCreator(this);
    final String readerExtension = getContentReaderExtension(filename);
    final String name = toPlainName(filename, readerExtension);
    final ContentReader contentReader = getContentReader(filename, importOptions);
    importContent(contentCreator, contentReader, parent, name, contentStream, importOptions, importListener);
}
Also used : ContentReader(org.apache.sling.jcr.contentloader.ContentReader)

Example 4 with ContentReader

use of org.apache.sling.jcr.contentloader.ContentReader in project sling by apache.

the class DefaultContentCreatorTest method createReferenceProperty.

@Test
public void createReferenceProperty() throws RepositoryException, NoSuchFieldException {
    final String propName = uniqueId();
    final String[] propValues = { uniqueId(), uniqueId() };
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    final Map<String, String[]> delayedMultipleReferences = (Map<String, String[]>) PrivateAccessor.getField(contentCreator, "delayedMultipleReferences");
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
    contentCreator.createProperty(propName, PropertyType.REFERENCE, propValues);
    assertTrue(parentNode.hasProperty(propName));
    assertTrue(parentNode.getProperty(propName).isNew());
    String referencesKey = parentNode.getPath() + "/" + propName;
    String[] uuidsOrPaths = delayedMultipleReferences.get(referencesKey);
    assertNotNull(uuidsOrPaths);
    assertEquals(propValues.length, uuidsOrPaths.length);
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 5 with ContentReader

use of org.apache.sling.jcr.contentloader.ContentReader in project sling by apache.

the class DefaultContentCreatorTest method testCreateTrueCheckedOutPreperty.

@Test
public void testCreateTrueCheckedOutPreperty() throws RepositoryException {
    parentNode = mockery.mock(Node.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(parentNode).hasProperty(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, null);
    contentCreator.prepareParsing(parentNode, null);
    int numberOfVersionablesNodes = contentCreator.getVersionables().size();
    contentCreator.createProperty("jcr:isCheckedOut", PropertyType.UNDEFINED, "true");
    //Checking that list of versionables doesn't changed
    assertEquals(numberOfVersionablesNodes, contentCreator.getVersionables().size());
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) Test(org.junit.Test)

Aggregations

ContentReader (org.apache.sling.jcr.contentloader.ContentReader)20 Expectations (org.jmock.Expectations)15 Test (org.junit.Test)15 ContentImportListener (org.apache.sling.jcr.contentloader.ContentImportListener)10 SlingRepository (org.apache.sling.jcr.api.SlingRepository)2 Before (org.junit.Before)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RepositoryException (javax.jcr.RepositoryException)1