com.evolveum.midpoint.common.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.evolveum.midpoint.common.Utils.java

Source

/*
 * Copyright (c) 2010-2013 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.evolveum.midpoint.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.evolveum.midpoint.prism.parser.XPathHolder;
import com.evolveum.midpoint.prism.parser.XPathSegment;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.PropertyReferenceListType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

/**
 * 
 * @author Igor Farinic
 * @author Radovan Semancik
 * 
 */
public class Utils {

    private static final Trace LOGGER = TraceManager.getTrace(Utils.class);

    public static String getPropertyName(String name) {
        if (null == name) {
            return "";
        }
        return StringUtils.lowerCase(name);
    }

    public static Element fillPropertyReference(String resolve) {
        com.evolveum.midpoint.prism.parser.XPathHolder xpath = new com.evolveum.midpoint.prism.parser.XPathHolder(
                Utils.getPropertyName(resolve));
        return xpath.toElement(SchemaConstants.NS_C, "property");
    }

    /**
     * Removing non-printable UTF characters from the string.
     * 
     * This is not really used now. It was done as a kind of prototype for
     * filters. But may come handy and it in fact tests that the pattern is
     * doing what expected, so it may be useful.
     * 
     * @param bad
     *            string with bad chars
     * @return string without bad chars
     */
    public static String cleanupUtf(String bad) {

        StringBuilder sb = new StringBuilder(bad.length());

        for (int cp, i = 0; i < bad.length(); i += Character.charCount(cp)) {
            cp = bad.codePointAt(i);
            if (isValidXmlCodepoint(cp)) {
                sb.append(Character.toChars(cp));
            }
        }

        return sb.toString();
    }

    /**
     * According to XML specification, section 2.2:
     * http://www.w3.org/TR/REC-xml/
     * 
     * @param c
     * @return
     */
    public static boolean isValidXmlCodepoint(int cp) {
        return (cp == 0x0009 || cp == 0x000a || cp == 0x000d || (cp >= 0x0020 && cp <= 0xd7ff)
                || (cp >= 0xe000 && cp <= 0xfffd) || (cp >= 0x10000 && cp <= 0x10FFFF));
    }
}