CPD Results

The following document contains the results of PMD's CPD 4.2.5.

Duplications

FileLine
info/magnolia/cms/beans/config/PropertiesInitializer.java398
info/magnolia/init/AbstractMagnoliaConfigurationProperties.java123
        final StringBuffer buf = new StringBuffer(strVal.trim());

        int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
        while (startIndex != -1) {
            int endIndex = -1;

            int index = startIndex + PLACEHOLDER_PREFIX.length();
            int withinNestedPlaceholder = 0;
            while (index < buf.length()) {
                if (PLACEHOLDER_SUFFIX.equals(buf.subSequence(index, index + PLACEHOLDER_SUFFIX.length()))) {
                    if (withinNestedPlaceholder > 0) {
                        withinNestedPlaceholder--;
                        index = index + PLACEHOLDER_SUFFIX.length();
                    } else {
                        endIndex = index;
                        break;
                    }
                } else if (PLACEHOLDER_PREFIX.equals(buf.subSequence(index, index + PLACEHOLDER_PREFIX.length()))) {
                    withinNestedPlaceholder++;
                    index = index + PLACEHOLDER_PREFIX.length();
                } else {
                    index++;
                }
            }

            if (endIndex != -1) {
                String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
                if (!visitedPlaceholders.add(placeholder)) {
                    log.warn("Circular reference detected in properties, \"{}\" is not resolvable", strVal);
                    return strVal;
                }
                // Recursive invocation, parsing placeholders contained in the placeholder key.
                placeholder = parseStringValue(placeholder, visitedPlaceholders);
FileLine
info/magnolia/content2bean/impl/TypeMappingImpl.java81
info/magnolia/jcr/node2bean/impl/TypeMappingImpl.java228
    public Method getAddMethod(Class<?> type, String name, int numberOfParameters) {
        name = StringUtils.capitalize(name);
        Method method = getExactMethod(type, "add" + name, numberOfParameters);
        if (method == null) {
            method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "s"), numberOfParameters);
        }

        if (method == null) {
            method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "es"), numberOfParameters);
        }

        if (method == null) {
            method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ren"), numberOfParameters);
        }

        if (method == null) {
            method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ies") + "y", numberOfParameters);
        }
        return method;
    }
FileLine
info/magnolia/importexport/PropertiesImportExport.java153
info/magnolia/jcr/util/PropertiesImportExport.java169
    protected Object convertPropertyStringToObject(String valueStr) {
        if (contains(valueStr, ':')) {
            final String type = StringUtils.substringBefore(valueStr, ":");
            final String value = StringUtils.substringAfter(valueStr, ":");

            // there is no beanUtils converter for Calendar
            if (type.equalsIgnoreCase("date")) {
                return ISO8601.parse(value);
            } else if (type.equalsIgnoreCase("binary")) {
                return new ByteArrayInputStream(value.getBytes());
            } else {
                try {
                    final Class<?> typeCl;
                    if (type.equals("int")) {
                        typeCl = Integer.class;
                    } else {
                        typeCl = Class.forName("java.lang." + StringUtils.capitalize(type));
                    }
                    return ConvertUtils.convert(value, typeCl);
                } catch (ClassNotFoundException e) {
                    // possibly a stray :, let's ignore it for now
                    return valueStr;
                }
            }
        }
        // no type specified, we assume it's a string, no conversion
        return valueStr;
    }
FileLine
info/magnolia/cms/core/DefaultACLBasedPermissions.java111
info/magnolia/cms/core/NodeTypeBasedPermissions.java106
    }

    @Override
    public boolean canRead(Path itemPath, ItemId itemId) throws RepositoryException {

        if ((itemId != null && "cafebabe-cafe-babe-cafe-babecafebabe".equals(itemId.toString())) || (itemPath != null && "/".equals(itemPath.toString()))) {
            // quick check - allow access to root to all like in old mgnl security
            return true;
        }

        if (itemPath == null) {

            // we deal only with permissions on nodes
            if (!itemId.denotesNode()) {
                itemId = ((PropertyId)itemId).getParentId();
            }

            synchronized (monitor) {

                if (readCache.containsKey(itemId)) {
                    return readCache.get(itemId);
                }

                itemPath = session.getHierarchyManager().getPath(itemId);
                boolean canRead = canRead(itemPath, itemId);
                readCache.put(itemId, canRead);
                return canRead;
            }
        }

        String originalPath = pathResolver.getJCRPath(itemPath);
FileLine
info/magnolia/content2bean/impl/Content2BeanProcessorImpl.java220
info/magnolia/jcr/node2bean/impl/Node2BeanProcessorImpl.java251
    protected void setProperties(Map<String, Object> values, final Node2BeanTransformer transformer, TransformationState state) throws Node2BeanException, RepositoryException {
        Object bean = state.getCurrentBean();
        log.debug("will populate bean {} with the values {}", bean.getClass().getName(), values);

        if (bean instanceof Map) {
            ((Map<String, Object>)bean).putAll(values);
        }

        if (bean instanceof Collection) {
            ((Collection<Object>)bean).addAll(values.values());
        } else {
            TypeDescriptor beanTypeDescriptor = typeMapping.getTypeDescriptor(bean.getClass());
            final Collection<PropertyTypeDescriptor> dscrs = beanTypeDescriptor.getPropertyDescriptors(typeMapping).values();

            for (PropertyTypeDescriptor descriptor : dscrs) {
                transformer.setProperty(typeMapping, state, descriptor, values);
            }
        }
    }

    protected Node2BeanTransformer resolveTransformer(TypeDescriptor type, Node2BeanTransformer transformer) {
FileLine
info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java347
info/magnolia/jcr/node2bean/impl/Node2BeanTransformerImpl.java285
                throw new Node2BeanException(e);
            }
        }

        if (Locale.class.equals(propertyType)) {
            if (value instanceof String) {
                String localeStr = (String) value;
                if (StringUtils.isNotEmpty(localeStr)) {
                    return LocaleUtils.toLocale(localeStr);
                }
            }
        }

        if (Collection.class.equals(propertyType) && value instanceof Map) {
            // TODO never used ?
            return ((Map) value).values();
        }

        // this is mainly the case when we are flattening node hierarchies
        if (String.class.equals(propertyType) && value instanceof Map && ((Map) value).size() == 1) {
            return ((Map) value).values().iterator().next();
        }

        return value;
    }
FileLine
info/magnolia/importexport/PropertiesImportExport.java71
info/magnolia/jcr/util/PropertiesImportExport.java76
    public void createNodes(Node root, InputStream propertiesStream) throws IOException, RepositoryException {
        Properties properties = new OrderedProperties();

        properties.load(propertiesStream);

        properties = keysToInnerFormat(properties);

        for (Object o : properties.keySet()) {
            String key = (String) o;
            String valueStr = properties.getProperty(key);

            String propertyName = StringUtils.substringAfterLast(key, ".");
            String path = StringUtils.substringBeforeLast(key, ".");

            String type = null;
            if (propertyName.equals("@type")) {
                type = valueStr;
            } else if (properties.containsKey(path + ".@type")) {
                type = properties.getProperty(path + ".@type");
            }

            type = StringUtils.defaultIfEmpty(type, MgnlNodeType.NT_CONTENTNODE);
FileLine
info/magnolia/content2bean/TypeDescriptor.java109
info/magnolia/jcr/node2bean/TypeDescriptor.java115
        return (isMap() || isCollection()) && (getType().isInterface()) || isArray();
    }

    /**
     * This method is not synchronized to avoid thread blocking, but the method guarantees that the returned map is not mutated afterward.
     */
    public Map<String, PropertyTypeDescriptor> getPropertyDescriptors(TypeMapping typeMapping) {
         //TODO ---- moved this out to TypeDescriptorFactory or something ?
        if(this.descriptors == null){

            // for not making this method synchronized we create a local variable first
            // this guarantees that the map you get is not changed after return
            final Map<String, PropertyTypeDescriptor> tmpDescriptors = new HashMap<String, PropertyTypeDescriptor>();
            PropertyDescriptor[] dscrs = PropertyUtils.getPropertyDescriptors(this.getType());
            for (int i = 0; i < dscrs.length; i++) {
                PropertyDescriptor descriptor = dscrs[i];
                tmpDescriptors.put(descriptor.getName(), typeMapping.getPropertyTypeDescriptor(this.getType(), descriptor.getName()));
            }

            this.descriptors = Collections.unmodifiableMap(tmpDescriptors);
        }
        return this.descriptors;
    }

    /**
     * Can return a custom transformer. Otherwise null.
     */
    public Node2BeanTransformer getTransformer() {
FileLine
info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java144
info/magnolia/jcr/node2bean/impl/Node2BeanTransformerImpl.java165
                    String mapProperyName = state.peekNode(1).getName();
                    propDscr = state.peekType(1).getPropertyTypeDescriptor(mapProperyName, typeMapping);
                    if (propDscr != null) {
                        typeDscr = propDscr.getCollectionEntryType();
                    }
                }
            } else {
                propDscr = state.getCurrentType().getPropertyTypeDescriptor(node.getName(), typeMapping);
                if (propDscr != null) {
                    typeDscr = propDscr.getType();
                }
            }
        }

        typeDscr = onResolveType(typeMapping, state, typeDscr, componentProvider);

        if (typeDscr != null) {
            // might be that the factory util defines a default implementation for interfaces
            final Class<?> type = typeDscr.getType();
            typeDscr = typeMapping.getTypeDescriptor(componentProvider.getImplementation(type));
FileLine
info/magnolia/content2bean/impl/Content2BeanTransformerImpl.java239
info/magnolia/jcr/node2bean/impl/Node2BeanTransformerImpl.java333
            value = state.getCurrentNode().getName();
        } else if (propertyName.equals("className") && value == null) {
            value = values.get("class");
        }

        // do no try to set a bean-property that has no corresponding node-property
        if (value == null) {
            return;
        }

        log.debug("try to set {}.{} with value {}", new Object[] { bean, propertyName, value });
        // if the parent bean is a map, we can't guess the types.
        if (!(bean instanceof Map)) {
            try {
                PropertyTypeDescriptor dscr = mapping.getPropertyTypeDescriptor(bean.getClass(), propertyName);
                if (dscr.getType() != null) {
                    // try to use a setter method for a Collection property of
                    // the bean
                    if (dscr.isCollection() || dscr.isMap() || dscr.isArray()) {
FileLine
info/magnolia/module/model/reader/BetwixtModuleDefinitionReader.java142
info/magnolia/objectfactory/configuration/ComponentConfigurationReader.java114
        final InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream(resourcePath));
        try {
            return read(reader);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("Can't close input for " + resourcePath);
            }
        }
    }

    /**
     * @deprecated TODO very ugly hack to force documents to be validated against OUR DTD.
     *             We could use an EntityResolver, but it seems that with SAX, the documents will only be
     *             validated if they original have a doctype declaration.
     *             We could also use http://doctypechanger.sourceforge.net/
     *             ... or switch to XmlSchema.
     */
    private Reader replaceDtd(Reader reader) throws IOException {
        String content = IOUtils.toString(reader);

        // remove doctype
        Pattern pattern = Pattern.compile("<!DOCTYPE .*>");
        Matcher matcher = pattern.matcher(content);
        content = matcher.replaceFirst("");

        // set doctype to the dtd
        try {
            Document doc = new SAXBuilder().build(new StringReader(content));
            doc.setDocType(new DocType("components", dtdUrl));
FileLine
info/magnolia/jcr/node2bean/impl/Node2BeanTransformerImpl.java407
info/magnolia/jcr/node2bean/impl/Node2BeanTransformerImpl.java428
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}",
                        new Object[] { propertyName, value, bean.getClass().getName(),
                                state.getCurrentNode().getPath(), e.toString() });
            } else {
                log.error("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}",
                        new Object[] { propertyName, value, bean.getClass().getName(),
                                state.getCurrentNode().getPath(), e.toString() });
            }
        }
    }
FileLine
info/magnolia/cms/util/NodeDataUtil.java109
info/magnolia/jcr/util/PropertyUtil.java259
    public static String getValueString(Value value) {
        try {
            switch (value.getType()) {
            case (PropertyType.STRING):
                return value.getString();
            case (PropertyType.DOUBLE):
                return Double.toString(value.getDouble());
            case (PropertyType.LONG):
                return Long.toString(value.getLong());
            case (PropertyType.BOOLEAN):
                return Boolean.toString(value.getBoolean());
            case (PropertyType.DATE):
                Date valueDate = value.getDate().getTime();
            return DateUtil.format(valueDate, PropertyUtil.getDateFormat());
FileLine
info/magnolia/jcr/decoration/ContentDecoratorNodeWrapper.java90
info/magnolia/jcr/decoration/ContentDecoratorPropertyWrapper.java59
    }

    @Override
    public Session getSession() throws RepositoryException {
        return wrapSession(super.getSession());
    }

    @Override
    public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
        Item item = super.getAncestor(depth);
        if (item.isNode()) {
            return wrapNode((Node) item);
        } else {
            return wrapProperty((Property) item);
        }
    }

    @Override
    public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
        return wrapNode(super.getParent());
    }

    @Override
    public Node getNode() throws ItemNotFoundException, ValueFormatException, RepositoryException {