Example usage for org.apache.commons.collections4 CollectionUtils isNotEmpty

List of usage examples for org.apache.commons.collections4 CollectionUtils isNotEmpty

Introduction

In this page you can find the example usage for org.apache.commons.collections4 CollectionUtils isNotEmpty.

Prototype

public static boolean isNotEmpty(final Collection<?> coll) 

Source Link

Document

Null-safe check if the specified collection is not empty.

Usage

From source file:org.craftercms.core.processors.impl.resolvers.ItemProcessorResolverChain.java

/**
 * Returns the {@link ItemProcessor} to use for the given item. Iterates through the chain of resolvers until one
 * of them returns a non-null processor. If non of them returns a processor, the {@code defaultProcessor} will be
 * returned.//w w w  .ja  v a 2 s.  c  o  m
 */
@Override
public ItemProcessor getProcessor(Item item) {
    ItemProcessor processor;

    if (CollectionUtils.isNotEmpty(resolvers)) {
        for (ItemProcessorResolver resolver : resolvers) {
            processor = resolver.getProcessor(item);
            if (processor != null) {
                return processor;
            }
        }
    }

    return defaultProcessor;
}

From source file:org.craftercms.core.processors.impl.template.TemplateProcessor.java

/**
 * Processes the content of certain nodes (found by the {@code NodeScanner} in the item's descriptor as templates,
 * by compiling the node text templates through the {@code templateCompiler} and then processing the compiled
 * template with a model returned by {@code modelFactory}.
 *
 * @throws ItemProcessingException if an error occurred while processing a template
 *//* w  w w .  j  a v a 2 s.  co m*/
public Item process(Context context, CachingOptions cachingOptions, Item item) throws ItemProcessingException {
    String descriptorUrl = item.getDescriptorUrl();
    Document descriptorDom = item.getDescriptorDom();

    if (descriptorDom != null) {
        List<Node> templateNodes = templateNodeScanner.scan(descriptorDom);
        if (CollectionUtils.isNotEmpty(templateNodes)) {
            for (Node templateNode : templateNodes) {
                String templateNodePath = templateNode.getUniquePath();

                if (logger.isDebugEnabled()) {
                    logger.debug("Template found in " + descriptorUrl + " at " + templateNodePath);
                }

                String templateId = templateNodePath + "@" + descriptorUrl;
                String template = templateNode.getText();
                IdentifiableStringTemplateSource templateSource = new IdentifiableStringTemplateSource(
                        templateId, template);

                Object model = modelFactory.getModel(item, templateNode, template);
                StringWriter output = new StringWriter();

                try {
                    CompiledTemplate compiledTemplate = templateCompiler.compile(templateSource);
                    compiledTemplate.process(model, output);
                } catch (TemplateException e) {
                    throw new ItemProcessingException("Unable to process the template " + templateId, e);
                }

                templateNode.setText(output.toString());
            }
        }
    }

    return item;
}

From source file:org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor.java

/**
 * For every XPath query provided in {@code metaDataNodesXPathQueries}, a list of nodes is selected and for each
 * one of these nodes its text value is extracted and added to a list that is later put in the item's properties.
 *//*from   ww w. ja va 2s  . co m*/
@Override
public Item process(Context context, CachingOptions cachingOptions, Item item) throws ItemProcessingException {
    for (String xpathQuery : metaDataNodesXPathQueries) {
        List<String> metaDataValues = item.queryDescriptorValues(xpathQuery);
        if (CollectionUtils.isNotEmpty(metaDataValues)) {
            item.setProperty(xpathQuery, metaDataValues);
        }
    }

    return item;
}

From source file:org.craftercms.core.service.impl.CompositeItemFilter.java

/**
 * If {@code runningBeforeProcessing} is true, calls all filters that need to be run before processing. If
 * it is false, calls all filters that need to be run after processing. Filters in the chain are called until
 * one of them rejects the item.//from  www.  ja  va  2s .co m
 */
@Override
public boolean accepts(Item item, List<Item> acceptedItems, List<Item> rejectedItems,
        boolean runningBeforeProcessing) {
    boolean accepted = true;

    if (CollectionUtils.isNotEmpty(filters)) {
        for (Iterator<ItemFilter> filterIter = filters.iterator(); accepted && filterIter.hasNext();) {
            ItemFilter filter = filterIter.next();
            if (runningBeforeProcessing && filter.runBeforeProcessing()) {
                accepted = filter.accepts(item, acceptedItems, rejectedItems, runningBeforeProcessing);
            } else if (!runningBeforeProcessing && filter.runAfterProcessing()) {
                accepted = filter.accepts(item, acceptedItems, rejectedItems, runningBeforeProcessing);
            }
        }
    }

    return accepted;
}

From source file:org.craftercms.core.service.impl.ContentStoreServiceImplTest.java

private void assertTreeCaching(Tree expected, Tree actual) {
    assertCaching(expected, actual);//from   w w w .  jav a  2s.co m

    List<Item> expectedChildren = expected.getChildren();
    List<Item> actualChildren = actual.getChildren();

    if (CollectionUtils.isNotEmpty(expectedChildren)) {
        int childrenCount = expectedChildren.size();
        for (int i = 0; i < childrenCount; i++) {
            Item expectedChild = expectedChildren.get(i);
            Item actualChild = actualChildren.get(i);

            if (expectedChild instanceof Tree) {
                assertTreeCaching((Tree) expectedChild, (Tree) actualChild);
            } else {
                assertCaching(expectedChild, actualChild);
            }
        }
    }
}

From source file:org.craftercms.core.service.Tree.java

/**
 * Copy constructor that takes another tree. Performs a deep copy depending on the value of the {@code deepCopy}
 * flag. In a deep copy, a deep copy of each child is done (by calling {@link Item#Item(Item, boolean)} and
 * {@link #Tree(Tree, boolean)})./*from www . j  a va2s .c  o  m*/
 */
public Tree(Tree tree, boolean deepCopy) {
    super(tree, deepCopy);

    if (deepCopy) {
        if (CollectionUtils.isNotEmpty(tree.children)) {
            children = new ArrayList<Item>(tree.children.size());
            for (Item child : tree.children) {
                if (child instanceof Tree) {
                    children.add(new Tree((Tree) child, deepCopy));
                } else {
                    children.add(new Item(child, deepCopy));
                }
            }
        } else {
            children = new ArrayList<Item>();
        }
    } else {
        children = tree.children;
    }
}

From source file:org.craftercms.core.store.impl.AbstractFileBasedContentStoreAdapter.java

@Override
protected List<Item> doFindItems(Context context, CachingOptions cachingOptions, String path,
        boolean withDescriptor)
        throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);/*  ww w  . jav  a 2s .c o  m*/

    File dir = findFile(context, path);

    if (dir == null) {
        return null;
    }

    if (!dir.isDirectory()) {
        throw new StoreException("The path " + dir + " doesn't correspond to a dir");
    }

    List<File> children = getChildren(context, dir);
    CachingAwareList<Item> items = new CachingAwareList<>(children.size());

    if (CollectionUtils.isNotEmpty(children)) {
        for (File child : children) {
            // Ignore any item metadata file. Metadata file DOMs are included in their respective
            // items.
            if (!child.isFile() || !child.getName().endsWith(metadataFileExtension)) {
                String fileRelPath = path + (!path.equals("/") ? "/" : "") + child.getName();
                Item item = findItem(context, cachingOptions, fileRelPath, withDescriptor);

                if (item != null) {
                    items.add(item);
                    items.addDependencyKey(item.getKey());
                }
            }
        }
    }

    return items;
}

From source file:org.craftercms.core.url.impl.ShortToLongUrlTransformer.java

protected String getLongName(Context context, CachingOptions cachingOptions, String folderPath,
        String shortName) throws UrlTransformationException {
    try {/*w w w.ja  v a 2  s .  c om*/
        List<Item> items = context.getStoreAdapter().findItems(context, cachingOptions, folderPath, false);
        if (CollectionUtils.isNotEmpty(items)) {
            for (Item item : items) {
                String itemName = item.getName();
                if (UrlUtils.getShortName(itemName, containsShortNameRegex, shortNameRegexGroup)
                        .equalsIgnoreCase(shortName)) {
                    return itemName;
                }
            }
        }
    } catch (Exception e) {
        throw new UrlTransformationException("An error occurred while retrieving the items at " + folderPath
                + " and trying to map the short name '" + shortName + "' to an " + "item's name (long name)",
                e);
    }

    return null;
}

From source file:org.craftercms.core.url.impl.UrlTransformerPipeline.java

@Override
public String transformUrl(Context context, CachingOptions cachingOptions, String url)
        throws UrlTransformationException {
    if (CollectionUtils.isNotEmpty(transformers)) {
        for (UrlTransformer transformer : transformers) {
            url = transformer.transformUrl(context, cachingOptions, url);
        }//w  w  w . j  a  v  a 2 s.c o m
    }

    return url;
}

From source file:org.craftercms.core.util.spring.mvc.GsonView.java

/**
 * Filters out undesired attributes from the given model.
 * <p/>//from  w ww  .  j av  a  2  s. co  m
 * <p>Default implementation removes {@link BindingResult} instances and entries not included in the {@link
 * #setRenderedAttributes(java.util.Set)} property.</p>
 *
 * @param model the model, as passed on to {@link #renderMergedOutputModel}
 * @return the model with only the attributes to render
 */
protected Map<String, Object> filterModel(Map<String, Object> model) {
    Map<String, Object> filteredModel = new HashMap<String, Object>(model.size());
    Set<String> renderedAttributes = CollectionUtils.isNotEmpty(this.renderedAttributes)
            ? this.renderedAttributes
            : model.keySet();

    for (Map.Entry<String, Object> attribute : model.entrySet()) {
        if (!(attribute.getValue() instanceof BindingResult)
                && renderedAttributes.contains(attribute.getKey())) {
            filteredModel.put(attribute.getKey(), attribute.getValue());
        }
    }

    return filteredModel;
}