Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

In this page you can find the example usage for java.util Stack push.

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

From source file:HtmlEncoder.java

/**
 *  Do "smart" encodging on a string. This means that valid HTML entities and tags,
 *  Helma macros and HTML comments are passed through unescaped, while
 *  other occurrences of '<', '>' and '&' are encoded to HTML entities.
 *
 *  @param str the string to encode/*from w  w w. j av a  2 s.c  o m*/
 *  @param ret the string buffer to encode to
 *  @param paragraphs if true use p tags for paragraphs, otherwise just use br's
 *  @param allowedTags a set containing the names of allowed tags as strings. All other
 *                     tags will be escaped
 */
public final static void encode(String str, StringBuffer ret, boolean paragraphs, Set allowedTags) {
    if (str == null) {
        return;
    }

    int l = str.length();

    // where to insert the <p> tag in case we want to create a paragraph later on
    int paragraphStart = ret.length();

    // what kind of element/text are we leaving and entering?
    // this is one of TEXT|SEMIBLOCK|BLOCK|INTERNAL
    // depending on this information, we decide whether and how to insert
    // paragraphs and line breaks. "entering" a tag means we're at the '<'
    // and exiting means we're at the '>', not that it's a start or close tag.
    byte entering = TEXT;
    byte exiting = TEXT;

    Stack openTags = new Stack();

    // are we currently within a < and a > that consitute some kind of tag?
    // we use tag balancing to know whether we are inside a tag (and should
    // pass things through unchanged) or outside (and should encode stuff).
    boolean insideTag = false;

    // are we inside an HTML tag?
    boolean insideHtmlTag = false;
    boolean insideCloseTag = false;
    byte htmlTagMode = TAG_NAME;

    // if we are inside a <code> tag, we encode everything to make
    // documentation work easier
    boolean insideCodeTag = false;
    boolean insidePreTag = false;

    // are we within a Helma <% macro %> tag? We treat macro tags and
    // comments specially, since we can't rely on tag balancing
    // to know when we leave a macro tag or comment.
    boolean insideMacroTag = false;

    // are we inside an HTML comment?
    boolean insideComment = false;

    // the quotation mark we are in within an HTML or Macro tag, if any
    char htmlQuoteChar = '\u0000';
    char macroQuoteChar = '\u0000';

    // number of newlines met since the last non-whitespace character
    int linebreaks = 0;

    // did we meet a backslash escape?
    boolean escape = false;

    boolean triggerBreak = false;

    for (int i = 0; i < l; i++) {
        char c = str.charAt(i);

        // step one: check if this is the beginning of an HTML tag, comment or
        // Helma macro.
        if (c == '<') {
            if (i < (l - 2)) {
                if (!insideMacroTag && ('%' == str.charAt(i + 1))) {
                    // this is the beginning of a Helma macro tag
                    if (!insideCodeTag) {
                        insideMacroTag = insideTag = true;
                        macroQuoteChar = '\u0000';
                    }
                } else if (('!' == str.charAt(i + 1)) && ('-' == str.charAt(i + 2))) {
                    // the beginning of an HTML comment?
                    if (!insideCodeTag) {
                        insideComment = insideTag = ((i < (l - 3)) && ('-' == str.charAt(i + 3)));
                    }
                } else if (!insideTag) {
                    // check if this is a HTML tag.
                    insideCloseTag = ('/' == str.charAt(i + 1));
                    int tagStart = insideCloseTag ? (i + 2) : (i + 1);
                    int j = tagStart;

                    while ((j < l) && Character.isLetterOrDigit(str.charAt(j)))
                        j++;

                    if ((j > tagStart) && (j < l)) {
                        String tagName = str.substring(tagStart, j).toLowerCase();

                        if ("code".equals(tagName) && insideCloseTag && insideCodeTag) {
                            insideCodeTag = false;
                        }

                        if (((allowedTags == null) || allowedTags.contains(tagName))
                                && allTags.contains(tagName) && !insideCodeTag) {
                            insideHtmlTag = insideTag = true;
                            htmlQuoteChar = '\u0000';
                            htmlTagMode = TAG_NAME;

                            exiting = entering;
                            entering = TEXT;

                            if (internalTags.contains(tagName)) {
                                entering = INTERNAL;
                            } else if (blockTags.contains(tagName)) {
                                entering = BLOCK;
                            } else if (semiBlockTags.contains(tagName)) {
                                entering = paragraphs ? BLOCK : SEMIBLOCK;
                            }

                            if (entering > 0) {
                                triggerBreak = !insidePreTag;
                            }

                            if (insideCloseTag) {
                                int t = openTags.search(tagName);

                                if (t == -1) {
                                    i = j;
                                    insideHtmlTag = insideTag = false;

                                    continue;
                                } else if (t > 1) {
                                    for (int k = 1; k < t; k++) {
                                        Object tag = openTags.pop();
                                        if (!emptyTags.contains(tag)) {
                                            ret.append("</");
                                            ret.append(tag);
                                            ret.append(">");
                                        }
                                    }
                                }

                                openTags.pop();
                            } else {
                                openTags.push(tagName);
                            }

                            if ("code".equals(tagName) && !insideCloseTag) {
                                insideCodeTag = true;
                            }

                            if ("pre".equals(tagName)) {
                                insidePreTag = !insideCloseTag;
                            }
                        }
                    }
                }
            } // if (i < l-2)
        }

        if ((triggerBreak || linebreaks > 0) && !Character.isWhitespace(c)) {

            if (!insideTag) {
                exiting = entering;
                entering = TEXT;
                if (exiting >= SEMIBLOCK) {
                    paragraphStart = ret.length();
                }
            }

            if (entering != INTERNAL && exiting != INTERNAL) {
                int swallowBreaks = 0;
                if (paragraphs && (entering != BLOCK || exiting != BLOCK) && (exiting < BLOCK)
                        && (linebreaks > 1) && paragraphStart < ret.length()) {
                    ret.insert(paragraphStart, "<p>");
                    ret.append("</p>");
                    swallowBreaks = 2;
                }

                // treat entering a SEMIBLOCK as entering a TEXT 
                int _entering = entering == SEMIBLOCK ? TEXT : entering;
                for (int k = linebreaks - 1; k >= 0; k--) {
                    if (k >= swallowBreaks && k >= _entering && k >= exiting) {
                        ret.append("<br />");
                    }
                    ret.append(newLine);
                }
                if (exiting >= SEMIBLOCK || linebreaks > 1) {
                    paragraphStart = ret.length();
                }

            }

            linebreaks = 0;
            triggerBreak = false;
        }

        switch (c) {
        case '<':

            if (insideTag) {
                ret.append('<');
            } else {
                ret.append("&lt;");
            }

            break;

        case '&':

            // check if this is an HTML entity already,
            // in which case we pass it though unchanged
            if ((i < (l - 3)) && !insideCodeTag) {
                // is this a numeric entity?
                if (str.charAt(i + 1) == '#') {
                    int j = i + 2;

                    while ((j < l) && Character.isDigit(str.charAt(j)))
                        j++;

                    if ((j < l) && (str.charAt(j) == ';')) {
                        ret.append("&");

                        break;
                    }
                } else {
                    int j = i + 1;

                    while ((j < l) && Character.isLetterOrDigit(str.charAt(j)))
                        j++;

                    if ((j < l) && (str.charAt(j) == ';')) {
                        ret.append("&");

                        break;
                    }
                }
            }

            // we didn't reach a break, so encode the ampersand as HTML entity
            ret.append("&amp;");

            break;

        case '\\':
            ret.append(c);

            if (insideTag && !insideComment) {
                escape = !escape;
            }

            break;

        case '"':
        case '\'':
            ret.append(c);

            if (!insideComment) {
                // check if the quote is escaped
                if (insideMacroTag) {
                    if (escape) {
                        escape = false;
                    } else if (macroQuoteChar == c) {
                        macroQuoteChar = '\u0000';
                    } else if (macroQuoteChar == '\u0000') {
                        macroQuoteChar = c;
                    }
                } else if (insideHtmlTag) {
                    if (escape) {
                        escape = false;
                    } else if (htmlQuoteChar == c) {
                        htmlQuoteChar = '\u0000';
                        htmlTagMode = TAG_SPACE;
                    } else if (htmlQuoteChar == '\u0000') {
                        htmlQuoteChar = c;
                    }
                }
            }

            break;

        case '\n':
            if (insideTag || insidePreTag) {
                ret.append('\n');
            } else {
                linebreaks++;
            }

            break;
        case '\r':
            if (insideTag || insidePreTag) {
                ret.append('\r');
            }
            break;

        case '>':

            // For Helma macro tags and comments, we overrule tag balancing,
            // i.e. we don't require that '<' and '>' be balanced within
            // macros and comments. Rather, we check for the matching closing tag.
            if (insideComment) {
                ret.append('>');
                insideComment = !((str.charAt(i - 2) == '-') && (str.charAt(i - 1) == '-'));
            } else if (insideMacroTag) {
                ret.append('>');
                insideMacroTag = !((str.charAt(i - 1) == '%') && (macroQuoteChar == '\u0000'));
            } else if (insideHtmlTag) {
                ret.append('>');

                // only leave HTML tag if quotation marks are balanced
                // within that tag.
                insideHtmlTag = htmlQuoteChar != '\u0000';

                // Check if this is an empty tag so we don't generate an
                // additional </close> tag.
                if (str.charAt(i - 1) == '/') {
                    // this is to avoid misinterpreting tags like
                    // <a href=http://foo/> as empty
                    if (htmlTagMode != TAG_ATT_VAL && htmlTagMode != TAG_ATT_NAME) {
                        openTags.pop();
                    }
                }

                exiting = entering;
                if (exiting > 0) {
                    triggerBreak = !insidePreTag;
                }

            } else {
                ret.append("&gt;");
            }

            // check if we still are inside any kind of tag
            insideTag = insideComment || insideMacroTag || insideHtmlTag;
            insideCloseTag = insideTag;

            break;

        default:

            if (insideHtmlTag && !insideCloseTag) {
                switch (htmlTagMode) {
                case TAG_NAME:
                    if (!Character.isLetterOrDigit(c)) {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                case TAG_SPACE:
                    if (Character.isLetterOrDigit(c)) {
                        htmlTagMode = TAG_ATT_NAME;
                    }
                    break;
                case TAG_ATT_NAME:
                    if (c == '=') {
                        htmlTagMode = TAG_ATT_VAL;
                    } else if (c == ' ') {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                case TAG_ATT_VAL:
                    if (Character.isWhitespace(c) && htmlQuoteChar == '\u0000') {
                        htmlTagMode = TAG_SPACE;
                    }
                    break;
                }
            }
            if (c < 128) {
                ret.append(c);
            } else if ((c >= 128) && (c < 256)) {
                ret.append(transform[c - 128]);
            } else {
                ret.append("&#");
                ret.append((int) c);
                ret.append(";");
            }

            escape = false;
        }
    }

    // if tags were opened but not closed, close them.
    int o = openTags.size();

    if (o > 0) {
        for (int k = 0; k < o; k++) {
            Object tag = openTags.pop();
            if (!emptyTags.contains(tag)) {
                ret.append("</");
                ret.append(tag);
                ret.append(">");
            }
        }
    }

    // add remaining newlines we may have collected
    int swallowBreaks = 0;
    if (paragraphs && entering < BLOCK) {
        ret.insert(paragraphStart, "<p>");
        ret.append("</p>");
        swallowBreaks = 2;
    }

    if (linebreaks > 0) {
        for (int i = linebreaks - 1; i >= 0; i--) {
            if (i >= swallowBreaks && i > exiting) {
                ret.append("<br />");
            }
            ret.append(newLine);
        }
    }
}

From source file:org.hellojavaer.testcase.generator.TestCaseGenerator.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> T produceBean(Class<T> clazz, ControlParam countrolParam, Stack<Class> parseClassList) {
    try {//from w w w.  j a  v  a  2  s . c  o m
        T item = clazz.newInstance();
        for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(clazz)) {
            Method writeMethod = pd.getWriteMethod();
            if (writeMethod == null || pd.getReadMethod() == null || //
                    countrolParam.getExcludeFieldList() != null
                            && countrolParam.getExcludeFieldList().contains(pd.getName())//
            ) {//
                continue;
            }
            Class fieldClazz = pd.getPropertyType();
            Long numIndex = countrolParam.getNumIndex();
            // int enumIndex = countrolParam.getEnumIndex();
            Random random = countrolParam.getRandom();
            long strIndex = countrolParam.getStrIndex();
            int charIndex = countrolParam.getCharIndex();
            Calendar time = countrolParam.getTime();
            if (TypeUtil.isBaseType(fieldClazz)) {
                if (TypeUtil.isNumberType(fieldClazz)) {
                    if (fieldClazz == Byte.class) {
                        writeMethod.invoke(item, Byte.valueOf((byte) (numIndex & 0x7F)));
                    } else if (fieldClazz == Short.class) {
                        writeMethod.invoke(item, Short.valueOf((short) (numIndex & 0x7FFF)));
                    } else if (fieldClazz == Integer.class) {
                        writeMethod.invoke(item, Integer.valueOf((int) (numIndex & 0x7FFFFFFF)));
                    } else if (fieldClazz == Long.class) {
                        writeMethod.invoke(item, Long.valueOf((long) numIndex));
                    } else if (fieldClazz == Float.class) {
                        writeMethod.invoke(item, Float.valueOf((float) numIndex));
                    } else if (fieldClazz == Double.class) {
                        writeMethod.invoke(item, Double.valueOf((double) numIndex));
                    } else if (fieldClazz == byte.class) {//
                        writeMethod.invoke(item, (byte) (numIndex & 0x7F));
                    } else if (fieldClazz == short.class) {
                        writeMethod.invoke(item, (short) (numIndex & 0x7FFF));
                    } else if (fieldClazz == int.class) {
                        writeMethod.invoke(item, (int) (numIndex & 0x7FFFFFFF));
                    } else if (fieldClazz == long.class) {
                        writeMethod.invoke(item, (long) numIndex);
                    } else if (fieldClazz == float.class) {
                        writeMethod.invoke(item, (float) numIndex);
                    } else if (fieldClazz == double.class) {
                        writeMethod.invoke(item, (double) numIndex);
                    }
                    numIndex++;
                    if (numIndex < 0) {
                        numIndex &= 0x7FFFFFFFFFFFFFFFL;
                    }
                    countrolParam.setNumIndex(numIndex);
                } else if (fieldClazz == boolean.class) {
                    writeMethod.invoke(item, random.nextBoolean());
                } else if (fieldClazz == Boolean.class) {
                    writeMethod.invoke(item, Boolean.valueOf(random.nextBoolean()));
                } else if (fieldClazz == char.class) {
                    writeMethod.invoke(item, CHAR_RANGE[charIndex]);
                    charIndex++;
                    if (charIndex >= CHAR_RANGE.length) {
                        charIndex = 0;
                    }
                    countrolParam.setCharIndex(charIndex);
                } else if (fieldClazz == Character.class) {
                    writeMethod.invoke(item, Character.valueOf(CHAR_RANGE[charIndex]));
                    charIndex++;
                    if (charIndex >= CHAR_RANGE.length) {
                        charIndex = 0;
                    }
                    countrolParam.setCharIndex(charIndex);
                } else if (fieldClazz == String.class) {
                    if (countrolParam.getUniqueFieldList() != null
                            && countrolParam.getUniqueFieldList().contains(pd.getName())) {
                        StringBuilder sb = new StringBuilder();
                        convertNum(strIndex, STRING_RANGE, countrolParam.getRandom(), sb);
                        writeMethod.invoke(item, sb.toString());
                        strIndex += countrolParam.getStrStep();
                        if (strIndex < 0) {
                            strIndex &= 0x7FFFFFFFFFFFFFFFL;
                        }
                        countrolParam.setStrIndex(strIndex);
                    } else {
                        writeMethod.invoke(item, String.valueOf(CHAR_RANGE[charIndex]));
                        charIndex++;
                        if (charIndex >= CHAR_RANGE.length) {
                            charIndex = 0;
                        }
                        countrolParam.setCharIndex(charIndex);
                    }

                } else if (fieldClazz == Date.class) {
                    writeMethod.invoke(item, time.getTime());
                    time.add(Calendar.DAY_OF_YEAR, 1);
                } else if (fieldClazz.isEnum()) {
                    int index = random.nextInt(fieldClazz.getEnumConstants().length);
                    writeMethod.invoke(item, fieldClazz.getEnumConstants()[index]);
                } else {
                    //
                    throw new RuntimeException("out of countrol Class " + fieldClazz.getName());
                }
            } else {
                parseClassList.push(fieldClazz);
                // TODO ?
                Set<Class> set = new HashSet<Class>(parseClassList);
                if (parseClassList.size() - set.size() <= countrolParam.getRecursiveCycleLimit()) {
                    Object bean = produceBean(fieldClazz, countrolParam, parseClassList);
                    writeMethod.invoke(item, bean);
                }
                parseClassList.pop();
            }
        }
        return item;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.alfresco.repo.transfer.AlienProcessorImpl.java

public void pruneNode(NodeRef nodeToPrune, String fromRepositoryId) {
    Stack<NodeRef> nodesToPrune = new Stack<NodeRef>();
    nodesToPrune.add(nodeToPrune);//from w w  w .  j a  v  a2s . c  om

    ChildAssociationRef startingParent = nodeService.getPrimaryParent(nodeToPrune);

    Stack<NodeRef> foldersToRecalculate = new Stack<NodeRef>();

    /**
     * Now go and do the pruning.        
     */
    while (!nodesToPrune.isEmpty()) {
        /**
         *  for all alien children
         * 
         *  if from the repo with no (other) aliens - delete
         *  
         *  if from the repo with multiple alien invasions - leave alone but process children
         */
        NodeRef currentNodeRef = nodesToPrune.pop();

        Map<QName, Serializable> properties = null;
        if (log.isDebugEnabled()) {
            properties = nodeService.getProperties(currentNodeRef);
        }

        if (log.isDebugEnabled()) {
            log.debug("Current nodeRef (name: \"" + properties.get(ContentModel.PROP_NAME)
                    + "\", fromRepositoryId: \"" + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                    + "\", manifestId: \"" + fromRepositoryId + "\")" + currentNodeRef);
        }

        log.debug("pruneNode:" + currentNodeRef);

        if (getNodeService().hasAspect(currentNodeRef, TransferModel.ASPECT_ALIEN)) {
            if (log.isDebugEnabled()) {
                log.debug("Current nodeRef has ASPECT_ALIEN (name: \"" + properties.get(ContentModel.PROP_NAME)
                        + "\", fromRepositoryId: \"" + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                        + "\", manifestId: \"" + fromRepositoryId + "\")");
            }

            // Yes this is an alien node
            List<String> invadedBy = (List<String>) getNodeService().getProperty(currentNodeRef,
                    TransferModel.PROP_INVADED_BY);
            String initialRepoId = (String) getNodeService().getProperty(currentNodeRef,
                    TransferModel.PROP_FROM_REPOSITORY_ID);

            if (log.isDebugEnabled()) {
                log.debug("Current nodeRef has PROP_INVADED_BY (name: \""
                        + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                        + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID) + "\", manifestId: \""
                        + fromRepositoryId + "\"): " + invadedBy);
            }

            if ((null != invadedBy) && invadedBy.contains(fromRepositoryId)) {
                if (log.isDebugEnabled()) {
                    log.debug("Current nodeRef's PROP_INVADED_BY contains current manifestId (name: \""
                            + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                            + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID) + "\", manifestId: \""
                            + fromRepositoryId + "\")");
                }

                // Yes we are invaded by fromRepositoryId
                if ((1 == invadedBy.size()) && fromRepositoryId.equalsIgnoreCase(initialRepoId)) {
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "Current nodeRef has only 1 element in PROP_INVADED_BY. Also MANIFEST_ID and INITIAL_REPOSITORY_ID are the same. Deleting the node... (name: \""
                                        + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                                        + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                        + "\", manifestId: \"" + fromRepositoryId + "\")");
                    }

                    // we are invaded by a single repository which must be fromRepositoryId
                    getNodeService().deleteNode(currentNodeRef);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "Current 'nodeRef' has more than 1 element in PROP_INVADED_BY. Adding its children to 'nodesToPrune' list... (name: \""
                                        + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                                        + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                        + "\", manifestId: \"" + fromRepositoryId + "\")");
                    }

                    // multiple invasion - so it must be a folder
                    List<ChildAssociationRef> refs = nodeService.getChildAssocsByPropertyValue(currentNodeRef,
                            TransferModel.PROP_INVADED_BY, fromRepositoryId);
                    for (ChildAssociationRef ref : refs) {
                        if (log.isDebugEnabled()) {
                            log.debug("will need to check child:" + ref);
                        }
                        nodesToPrune.push(ref.getChildRef());
                    }

                    /**
                     * Yes we might do something to the children of this node.
                     */
                    if (!foldersToRecalculate.contains(currentNodeRef)) {
                        if (log.isDebugEnabled()) {
                            log.debug(
                                    "Current 'nodeRef' is not in 'foldersToRecalculate' list. Adding it to the list... (name: \""
                                            + properties.get(ContentModel.PROP_NAME)
                                            + "\", fromRepositoryId: \""
                                            + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                            + "\", manifestId: \"" + fromRepositoryId + "\")");
                        }

                        foldersToRecalculate.push(currentNodeRef);
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "Current \"nodeRef\"'s PROP_INVADED_BY does not contain current 'manifestId' (name: \""
                                    + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                                    + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                    + "\", manifestId: \"" + fromRepositoryId + "\")");
                }

                /**
                 * Current node has been invaded by another repository  
                 *
                 * Need to check fromRepositoryId since its children may need to be pruned
                 */
                getNodeService().hasAspect(currentNodeRef, TransferModel.ASPECT_TRANSFERRED);
                {
                    if (fromRepositoryId.equalsIgnoreCase(initialRepoId)) {
                        if (log.isDebugEnabled()) {
                            log.debug("folder is from the transferring repository");
                            log.debug(
                                    "Current nodeRef has more than 1 element in PROP_INVADED_BY. Adding its children to 'nodesToPrune' list... (name: \""
                                            + properties.get(ContentModel.PROP_NAME)
                                            + "\", fromRepositoryId: \""
                                            + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                            + "\", manifestId: \"" + fromRepositoryId + "\")");
                        }
                        // invaded from somewhere else - so it must be a folder
                        List<ChildAssociationRef> refs = getNodeService().getChildAssocs(currentNodeRef);
                        for (ChildAssociationRef ref : refs) {
                            if (log.isDebugEnabled()) {
                                log.debug("will need to check child:" + ref);
                            }
                            nodesToPrune.push(ref.getChildRef());

                            /**
                             * This folder can't be deleted so its invaded flag needs to be re-calculated 
                             */
                            if (!foldersToRecalculate.contains(currentNodeRef)) {
                                if (log.isDebugEnabled()) {
                                    log.debug(
                                            "Current 'nodeRef' is not in 'foldersToRecalculate' list. Adding it to the list... (name: \""
                                                    + properties.get(ContentModel.PROP_NAME)
                                                    + "\", fromRepositoryId: \""
                                                    + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                                    + "\", manifestId: \"" + fromRepositoryId + "\")");
                                }

                                foldersToRecalculate.push(currentNodeRef);
                            }
                        }
                    }
                }
            }
        } else {
            // Current node does not contain alien nodes so it can be deleted.
            getNodeService().hasAspect(currentNodeRef, TransferModel.ASPECT_TRANSFERRED);
            {
                if (log.isDebugEnabled()) {
                    log.debug("Current 'nodeRef' does not have ASPECT_ALIEN (name: \""
                            + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                            + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID) + "\", manifestId: \""
                            + fromRepositoryId + "\")");
                }

                String initialRepoId = (String) getNodeService().getProperty(currentNodeRef,
                        TransferModel.PROP_REPOSITORY_ID);
                if (fromRepositoryId.equalsIgnoreCase(initialRepoId)) {
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "Current \"nodeRef\"'s has PROP_FROM_REPOSITORY_ID equal to current 'manifestId'. Deleting the node... (name: \""
                                        + properties.get(ContentModel.PROP_NAME) + "\", fromRepositoryId: \""
                                        + properties.get(TransferModel.PROP_FROM_REPOSITORY_ID)
                                        + "\", manifestId: \"" + fromRepositoryId + "\")");
                        // we are invaded by a single repository
                        log.debug("pruned - deleted non alien node:" + currentNodeRef);
                    }

                    getNodeService().deleteNode(currentNodeRef);
                }
            }
        }
    }

    /**
     * Now recalculate the "invadedBy" flag for those folders we could not delete.
     */
    while (!foldersToRecalculate.isEmpty()) {
        NodeRef folderNodeRef = foldersToRecalculate.pop();

        log.debug("recalculate invadedBy :" + folderNodeRef);

        recalcInvasion(folderNodeRef, fromRepositoryId);
    }

    /**
     * Now ripple up the invaded flag - may be a alien retreat.
     */
    log.debug("now ripple upwards");

    ChildAssociationRef ripple = startingParent;
    while (ripple != null) {
        if (log.isDebugEnabled()) {
            log.debug("Checking parent:" + ripple);
        }

        if (nodeService.hasAspect(ripple.getParentRef(), TransferModel.ASPECT_ALIEN)) {
            if (recalcInvasion(ripple.getParentRef(), fromRepositoryId)) {
                log.debug("parent is still invaded");
                ripple = null;
            } else {
                log.debug("parent is no longer invaded");
                ripple = nodeService.getPrimaryParent(ripple.getParentRef());
            }
        } else {
            ripple = null;
        }
    }

    log.debug("pruneNode: end");
}

From source file:com.marlonjones.voidlauncher.CellLayout.java

/**
 * Find a vacant area that will fit the given bounds nearest the requested
 * cell location. Uses Euclidean distance to score multiple vacant areas.
 *
 * @param pixelX The X location at which you want to search for a vacant area.
 * @param pixelY The Y location at which you want to search for a vacant area.
 * @param minSpanX The minimum horizontal span required
 * @param minSpanY The minimum vertical span required
 * @param spanX Horizontal span of the object.
 * @param spanY Vertical span of the object.
 * @param ignoreOccupied If true, the result can be an occupied cell
 * @param result Array in which to place the result, or null (in which case a new array will
 *        be allocated)/* w  ww  .j a v a2s. c  o  m*/
 * @return The X, Y cell of a vacant area that can contain this object,
 *         nearest the requested location.
 */
private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
        boolean ignoreOccupied, int[] result, int[] resultSpan) {
    lazyInitTempRectStack();

    // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
    // to the center of the item, but we are searching based on the top-left cell, so
    // we translate the point over to correspond to the top-left.
    pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
    pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;

    // Keep track of best-scoring drop area
    final int[] bestXY = result != null ? result : new int[2];
    double bestDistance = Double.MAX_VALUE;
    final Rect bestRect = new Rect(-1, -1, -1, -1);
    final Stack<Rect> validRegions = new Stack<Rect>();

    final int countX = mCountX;
    final int countY = mCountY;

    if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) {
        return bestXY;
    }

    for (int y = 0; y < countY - (minSpanY - 1); y++) {
        inner: for (int x = 0; x < countX - (minSpanX - 1); x++) {
            int ySize = -1;
            int xSize = -1;
            if (ignoreOccupied) {
                // First, let's see if this thing fits anywhere
                for (int i = 0; i < minSpanX; i++) {
                    for (int j = 0; j < minSpanY; j++) {
                        if (mOccupied.cells[x + i][y + j]) {
                            continue inner;
                        }
                    }
                }
                xSize = minSpanX;
                ySize = minSpanY;

                // We know that the item will fit at _some_ acceptable size, now let's see
                // how big we can make it. We'll alternate between incrementing x and y spans
                // until we hit a limit.
                boolean incX = true;
                boolean hitMaxX = xSize >= spanX;
                boolean hitMaxY = ySize >= spanY;
                while (!(hitMaxX && hitMaxY)) {
                    if (incX && !hitMaxX) {
                        for (int j = 0; j < ySize; j++) {
                            if (x + xSize > countX - 1 || mOccupied.cells[x + xSize][y + j]) {
                                // We can't move out horizontally
                                hitMaxX = true;
                            }
                        }
                        if (!hitMaxX) {
                            xSize++;
                        }
                    } else if (!hitMaxY) {
                        for (int i = 0; i < xSize; i++) {
                            if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
                                // We can't move out vertically
                                hitMaxY = true;
                            }
                        }
                        if (!hitMaxY) {
                            ySize++;
                        }
                    }
                    hitMaxX |= xSize >= spanX;
                    hitMaxY |= ySize >= spanY;
                    incX = !incX;
                }
                incX = true;
                hitMaxX = xSize >= spanX;
                hitMaxY = ySize >= spanY;
            }
            final int[] cellXY = mTmpPoint;
            cellToCenterPoint(x, y, cellXY);

            // We verify that the current rect is not a sub-rect of any of our previous
            // candidates. In this case, the current rect is disqualified in favour of the
            // containing rect.
            Rect currentRect = mTempRectStack.pop();
            currentRect.set(x, y, x + xSize, y + ySize);
            boolean contained = false;
            for (Rect r : validRegions) {
                if (r.contains(currentRect)) {
                    contained = true;
                    break;
                }
            }
            validRegions.push(currentRect);
            double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);

            if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) {
                bestDistance = distance;
                bestXY[0] = x;
                bestXY[1] = y;
                if (resultSpan != null) {
                    resultSpan[0] = xSize;
                    resultSpan[1] = ySize;
                }
                bestRect.set(currentRect);
            }
        }
    }

    // Return -1, -1 if no suitable location found
    if (bestDistance == Double.MAX_VALUE) {
        bestXY[0] = -1;
        bestXY[1] = -1;
    }
    recycleTempRects(validRegions);
    return bestXY;
}

From source file:org.apache.ojb.otm.core.ConcreteEditingContext.java

private ContextEntry insertInternal(Identity oid, Object userObject, int lock, boolean canCreate,
        Identity insertBeforeThis, Stack stack) throws LockingException {
    ContextEntry entry;/*from www  .  j  a  va 2 s.c  om*/
    LockManager lockManager;
    Swizzling swizzlingStrategy;
    IndirectionHandler handler = null;
    OTMKit kit = _tx.getKit();
    // Are we building object's relations for the userObject in the transaction?
    // Otherwise we just get data from the "userObject" and put it into
    // the previously loaded/created object in the transaction
    boolean buildingObject = false;
    boolean lazySwizzle = false;

    if (lock == LockType.NO_LOCK) {
        return null;
    }

    entry = (ContextEntry) _objects.get(oid);

    if (userObject == null) {
        // invalidating object...
        _original.remove(oid);
        _checkpointed.remove(oid);
        if (entry != null) {
            entry.userObject = null;
            entry.cacheObject = null;
        }
        return entry;
    }

    lockManager = LockManager.getInstance();
    swizzlingStrategy = kit.getSwizzlingStrategy();

    handler = ProxyHelper.getIndirectionHandler(userObject);
    if ((handler != null) && handler.alreadyMaterialized()) {
        userObject = handler.getRealSubject();
        handler = null;
    }

    if ((entry == null) || (entry.userObject == null)) {
        // first insertion of the userObject into editing context
        Object swizzledObject = swizzlingStrategy.swizzle(userObject, null, _pb, this);
        entry = new ContextEntry(swizzledObject);
        if (entry.handler != null) {
            ObjectCopyStrategy copyStrategy = _tx.getKit().getCopyStrategy(oid);
            entry.cacheObject = copyStrategy.copy(userObject, _pb);
            // Assume that object exists, otherwise were the proxy came from?
            _objects.put(oid, entry);
            lockManager.ensureLock(oid, _tx, lock, _pb); // lock after _objects.put to avoid hanged locks
            entry.handler.addListener(this);
        } else {
            Object origCacheObj = _pb.getObjectByIdentity(oid);

            if ((origCacheObj == null) && !canCreate) {
                // we don't create the objects by reachability
                throw new IllegalStateException(
                        "Related object is neither persistent, nor otm-depentent: " + oid);
            }
            if (origCacheObj != null) {
                entry.cacheObject = origCacheObj;
            }
            buildingObject = true;
            _objects.put(oid, entry);
            lockManager.ensureLock(oid, _tx, lock, _pb); // lock after _objects.put to avoid hanged locks

            if (userObject != null) {
                if ((origCacheObj == null) && canCreate) {
                    ObjectCopyStrategy copyStrategy = _tx.getKit().getCopyStrategy(oid);
                    entry.cacheObject = copyStrategy.copy(userObject, _pb);
                    entry.state = State.PERSISTENT_NEW;
                    if (kit.isEagerInsert(userObject) || hasBidirectionalAssociation(userObject.getClass())) {
                        _pb.store(entry.cacheObject, entry.state);
                        entry.state = State.PERSISTENT_CLEAN;
                        origCacheObj = entry.cacheObject;
                    }
                }

                if (origCacheObj != null) {
                    _original.put(oid, getFields(userObject, false, true));
                }
            }
        }
        if (insertBeforeThis != null) {
            int insertIndex = _order.indexOf(insertBeforeThis);
            _order.add(insertIndex, oid);
        } else {
            _order.add(oid);
        }
    } else {
        // The object in context is the same object attempted an insert on
        // Ensure we have the correct lock level
        lockManager.ensureLock(oid, _tx, lock, _pb);

        if (handler == null) {
            if (!swizzlingStrategy.isSameInstance(entry.userObject, userObject)) {
                // the new object contains data to deal with
                if (entry.handler != null) {
                    // materialize old object even if it is not
                    // materialized yet, because we need a place
                    // to copy the data from the new object
                    entry.userObject = entry.handler.getRealSubject();
                    entry.handler = null;
                }
                // swizzle after lockReachableObjects(), when all related objects
                // will be in the editing context
                lazySwizzle = true;
            }
        }
    }

    // perform automatic read lock for all reachable objects
    // if the inserted object is materialized
    if ((handler == null) && !stack.contains(userObject)) {
        stack.push(userObject);
        lockReachableObjects(oid, userObject, entry.cacheObject, lock, stack, buildingObject);
        stack.pop();
        if (lazySwizzle) {
            entry.userObject = swizzlingStrategy.swizzle(userObject, entry.userObject, _pb, this);
        }
    }

    return entry;
}

From source file:com.android.launcher3.CellLayout.java

/**
 * Find a vacant area that will fit the given bounds nearest the requested
 * cell location. Uses Euclidean distance to score multiple vacant areas.
 *
 * @param pixelX The X location at which you want to search for a vacant area.
 * @param pixelY The Y location at which you want to search for a vacant area.
 * @param minSpanX The minimum horizontal span required
 * @param minSpanY The minimum vertical span required
 * @param spanX Horizontal span of the object.
 * @param spanY Vertical span of the object.
 * @param ignoreOccupied If true, the result can be an occupied cell
 * @param result Array in which to place the result, or null (in which case a new array will
 *        be allocated)//from  w  w  w.  j  a  v a2s. co  m
 * @return The X, Y cell of a vacant area that can contain this object,
 *         nearest the requested location.
 */
private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
        boolean ignoreOccupied, int[] result, int[] resultSpan) {
    lazyInitTempRectStack();

    // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
    // to the center of the item, but we are searching based on the top-left cell, so
    // we translate the point over to correspond to the top-left.
    pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
    pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;

    // Keep track of best-scoring drop area
    final int[] bestXY = result != null ? result : new int[2];
    double bestDistance = Double.MAX_VALUE;
    final Rect bestRect = new Rect(-1, -1, -1, -1);
    final Stack<Rect> validRegions = new Stack<Rect>();

    final int countX = mCountX;
    final int countY = mCountY;

    if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) {
        return bestXY;
    }

    for (int y = 0; y < countY - (minSpanY - 1); y++) {
        inner: for (int x = 0; x < countX - (minSpanX - 1); x++) {
            int ySize = -1;
            int xSize = -1;
            if (ignoreOccupied) {
                // First, let's see if this thing fits anywhere
                for (int i = 0; i < minSpanX; i++) {
                    for (int j = 0; j < minSpanY; j++) {
                        if (mOccupied[x + i][y + j]) {
                            continue inner;
                        }
                    }
                }
                xSize = minSpanX;
                ySize = minSpanY;

                // We know that the item will fit at _some_ acceptable size, now let's see
                // how big we can make it. We'll alternate between incrementing x and y spans
                // until we hit a limit.
                boolean incX = true;
                boolean hitMaxX = xSize >= spanX;
                boolean hitMaxY = ySize >= spanY;
                while (!(hitMaxX && hitMaxY)) {
                    if (incX && !hitMaxX) {
                        for (int j = 0; j < ySize; j++) {
                            if (x + xSize > countX - 1 || mOccupied[x + xSize][y + j]) {
                                // We can't move out horizontally
                                hitMaxX = true;
                            }
                        }
                        if (!hitMaxX) {
                            xSize++;
                        }
                    } else if (!hitMaxY) {
                        for (int i = 0; i < xSize; i++) {
                            if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
                                // We can't move out vertically
                                hitMaxY = true;
                            }
                        }
                        if (!hitMaxY) {
                            ySize++;
                        }
                    }
                    hitMaxX |= xSize >= spanX;
                    hitMaxY |= ySize >= spanY;
                    incX = !incX;
                }
                incX = true;
                hitMaxX = xSize >= spanX;
                hitMaxY = ySize >= spanY;
            }
            final int[] cellXY = mTmpPoint;
            cellToCenterPoint(x, y, cellXY);

            // We verify that the current rect is not a sub-rect of any of our previous
            // candidates. In this case, the current rect is disqualified in favour of the
            // containing rect.
            Rect currentRect = mTempRectStack.pop();
            currentRect.set(x, y, x + xSize, y + ySize);
            boolean contained = false;
            for (Rect r : validRegions) {
                if (r.contains(currentRect)) {
                    contained = true;
                    break;
                }
            }
            validRegions.push(currentRect);
            double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);

            if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) {
                bestDistance = distance;
                bestXY[0] = x;
                bestXY[1] = y;
                if (resultSpan != null) {
                    resultSpan[0] = xSize;
                    resultSpan[1] = ySize;
                }
                bestRect.set(currentRect);
            }
        }
    }

    // Return -1, -1 if no suitable location found
    if (bestDistance == Double.MAX_VALUE) {
        bestXY[0] = -1;
        bestXY[1] = -1;
    }
    recycleTempRects(validRegions);
    return bestXY;
}

From source file:com.android.leanlauncher.CellLayout.java

/**
 * Find a vacant area that will fit the given bounds nearest the requested
 * cell location. Uses Euclidean distance to score multiple vacant areas.
 *
 * @param pixelX The X location at which you want to search for a vacant area.
 * @param pixelY The Y location at which you want to search for a vacant area.
 * @param minSpanX The minimum horizontal span required
 * @param minSpanY The minimum vertical span required
 * @param spanX Horizontal span of the object.
 * @param spanY Vertical span of the object.
 * @param ignoreOccupied If true, the result can be an occupied cell
 * @param result Array in which to place the result, or null (in which case a new array will
 *        be allocated)/*from  w w w  .j  ava 2 s  .c om*/
 * @return The X, Y cell of a vacant area that can contain this object,
 *         nearest the requested location.
 */
int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, View ignoreView,
        boolean ignoreOccupied, int[] result, int[] resultSpan, boolean[][] occupied) {
    lazyInitTempRectStack();
    // mark space take by ignoreView as available (method checks if ignoreView is null)
    markCellsAsUnoccupiedForView(ignoreView, occupied);

    // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
    // to the center of the item, but we are searching based on the top-left cell, so
    // we translate the point over to correspond to the top-left.
    pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
    pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;

    // Keep track of best-scoring drop area
    final int[] bestXY = result != null ? result : new int[2];
    double bestDistance = Double.MAX_VALUE;
    final Rect bestRect = new Rect(-1, -1, -1, -1);
    final Stack<Rect> validRegions = new Stack<Rect>();

    final int countX = mCountX;
    final int countY = mCountY;

    if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) {
        return bestXY;
    }

    for (int y = 0; y < countY - (minSpanY - 1); y++) {
        inner: for (int x = 0; x < countX - (minSpanX - 1); x++) {
            int ySize = -1;
            int xSize = -1;
            if (ignoreOccupied) {
                // First, let's see if this thing fits anywhere
                for (int i = 0; i < minSpanX; i++) {
                    for (int j = 0; j < minSpanY; j++) {
                        if (occupied[x + i][y + j]) {
                            continue inner;
                        }
                    }
                }
                xSize = minSpanX;
                ySize = minSpanY;

                // We know that the item will fit at _some_ acceptable size, now let's see
                // how big we can make it. We'll alternate between incrementing x and y spans
                // until we hit a limit.
                boolean incX = true;
                boolean hitMaxX = xSize >= spanX;
                boolean hitMaxY = ySize >= spanY;
                while (!(hitMaxX && hitMaxY)) {
                    if (incX && !hitMaxX) {
                        for (int j = 0; j < ySize; j++) {
                            if (x + xSize > countX - 1 || occupied[x + xSize][y + j]) {
                                // We can't move out horizontally
                                hitMaxX = true;
                            }
                        }
                        if (!hitMaxX) {
                            xSize++;
                        }
                    } else if (!hitMaxY) {
                        for (int i = 0; i < xSize; i++) {
                            if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
                                // We can't move out vertically
                                hitMaxY = true;
                            }
                        }
                        if (!hitMaxY) {
                            ySize++;
                        }
                    }
                    hitMaxX |= xSize >= spanX;
                    hitMaxY |= ySize >= spanY;
                    incX = !incX;
                }
                incX = true;
                hitMaxX = xSize >= spanX;
                hitMaxY = ySize >= spanY;
            }
            final int[] cellXY = mTmpXY;
            cellToCenterPoint(x, y, cellXY);

            // We verify that the current rect is not a sub-rect of any of our previous
            // candidates. In this case, the current rect is disqualified in favour of the
            // containing rect.
            Rect currentRect = mTempRectStack.pop();
            currentRect.set(x, y, x + xSize, y + ySize);
            boolean contained = false;
            for (Rect r : validRegions) {
                if (r.contains(currentRect)) {
                    contained = true;
                    break;
                }
            }
            validRegions.push(currentRect);
            double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) + Math.pow(cellXY[1] - pixelY, 2));

            if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) {
                bestDistance = distance;
                bestXY[0] = x;
                bestXY[1] = y;
                if (resultSpan != null) {
                    resultSpan[0] = xSize;
                    resultSpan[1] = ySize;
                }
                bestRect.set(currentRect);
            }
        }
    }
    // re-mark space taken by ignoreView as occupied
    markCellsAsOccupiedForView(ignoreView, occupied);

    // Return -1, -1 if no suitable location found
    if (bestDistance == Double.MAX_VALUE) {
        bestXY[0] = -1;
        bestXY[1] = -1;
    }
    recycleTempRects(validRegions);
    return bestXY;
}

From source file:de.uni_koblenz.jgralab.utilities.rsa.Rsa2Tg.java

private boolean isValidBinaryEdgeClassCandidate(EdgeClass ec) {
    LocalBooleanGraphMarker alreadySeenMarker = new LocalBooleanGraphMarker(sg);
    Stack<EdgeClass> workingList = new Stack<EdgeClass>();
    workingList.add(ec);/*  w  ww  .  ja v a2s  .co m*/
    alreadySeenMarker.mark(ec);
    while (!workingList.isEmpty()) {
        EdgeClass current = workingList.pop();
        alreadySeenMarker.mark(current);
        if (BinaryEdgeClass.class.isInstance(current)) {
            continue;
        }
        // a BinaryEdgeClass must have exactly two IncidenceClasses
        Set<IncidenceClass> allIncidenceClasses = getAllIncidenceClasses(current);
        if (allIncidenceClasses.size() != 2) {
            return false;
        }

        IncidenceClass firstIC = null, lastIC = null;
        for (IncidenceClass ic : allIncidenceClasses) {
            if (firstIC == null) {
                firstIC = ic;
            } else {
                lastIC = ic;
            }
        }
        if (isNestingIncidenceOrEdgeClass.isMarked(firstIC) || isNestingIncidenceOrEdgeClass.isMarked(lastIC)) {
            return false;
        }
        // both incidences have to have different directions and both
        // incidences are not abstract and the multiplicities must fit
        if (firstIC.get_direction() == lastIC.get_direction() && (firstIC.is_abstract() || lastIC.is_abstract())
                && (firstIC.get_minVerticesAtEdge() == 1 && firstIC.get_maxVerticesAtEdge() == 1
                        && lastIC.get_minVerticesAtEdge() == 1 && lastIC.get_maxVerticesAtEdge() == 1)) {
            return false;
        }

        // every subclass and superclass of a BinaryEdgeClass must be a
        // BinaryEdgeClass candidate, too
        for (SpecializesEdgeClass sec : current.getIncidentEdges(SpecializesEdgeClass.class)) {
            EdgeClass genEC = (EdgeClass) (sec.getAlpha() == current ? sec.getOmega() : sec.getAlpha());
            if (alreadySeenMarker.isMarked(genEC)) {
                continue;
            } else if (!workingList.contains(genEC) && !BinaryEdgeClass.class.isInstance(genEC)) {
                workingList.push(genEC);
            }
        }
    }
    return true;
}

From source file:org.apache.roller.weblogger.util.HTMLSanitizer.java

public static SanitizeResult sanitizer(String html, Pattern allowedTags, Pattern forbiddenTags) {
    SanitizeResult ret = new SanitizeResult();
    Stack<String> openTags = new Stack();

    List<String> tokens = tokenize(html);

    // -------------------   LOOP for every token --------------------------
    for (String token : tokens) {
        boolean isAcceptedToken = false;

        Matcher startMatcher = tagStartPattern.matcher(token);
        Matcher endMatcher = tagClosePattern.matcher(token);

        //--------------------------------------------------------------------------------  COMMENT    <!-- ......... -->
        if (commentPattern.matcher(token).find()) {
            ret.val = ret.val + token + (token.endsWith("-->") ? "" : "-->");
            ret.invalidTags.add(token + (token.endsWith("-->") ? "" : "-->"));
            continue;

            //--------------------------------------------------------------------------------  OPEN TAG    <tag .........>
        } else if (startMatcher.find()) {

            //tag name extraction
            String tag = startMatcher.group(1).toLowerCase();

            //-----------------------------------------------------  FORBIDDEN TAG   <script .........>
            if (forbiddenTags.matcher(tag).find()) {
                ret.invalidTags.add("<" + tag + ">");
                continue;

                // --------------------------------------------------  WELL KNOWN TAG
            } else if (allowedTags.matcher(tag).find()) {

                String cleanToken = "<" + tag;
                String tokenBody = startMatcher.group(2);

                //first test table consistency
                //table tbody tfoot thead th tr td
                if ("thead".equals(tag) || "tbody".equals(tag) || "tfoot".equals(tag) || "tr".equals(tag)) {
                    if (openTags.search("table") < 1) {
                        ret.invalidTags.add("<" + tag + ">");
                        continue;
                    }//from ww  w .ja  va2 s .  c o  m
                } else if ("td".equals(tag) || "th".equals(tag)) {
                    if (openTags.search("tr") < 1) {
                        ret.invalidTags.add("<" + tag + ">");
                        continue;
                    }
                }

                // then test properties
                Matcher attributes = attributesPattern.matcher(tokenBody);

                boolean foundURL = false; // URL flag
                while (attributes.find()) {

                    String attr = attributes.group(1).toLowerCase();
                    String val = attributes.group(2);

                    // we will accept href in case of <A>
                    if ("a".equals(tag) && "href".equals(attr)) { // <a href="......">
                        String[] customSchemes = { "http", "https" };
                        if (new UrlValidator(customSchemes).isValid(val)) {
                            foundURL = true;
                        } else {
                            // may be it is a mailto?
                            //  case <a href="mailto:pippo@pippo.com?subject=...."
                            if (val.toLowerCase().startsWith("mailto:") && val.indexOf('@') >= 0) {
                                String val1 = "http://www." + val.substring(val.indexOf('@') + 1);
                                if (new UrlValidator(customSchemes).isValid(val1)) {
                                    foundURL = true;
                                } else {
                                    ret.invalidTags.add(attr + " " + val);
                                    val = "";
                                }
                            } else {
                                ret.invalidTags.add(attr + " " + val);
                                val = "";
                            }
                        }

                    } else if (tag.matches("img|embed") && "src".equals(attr)) { // <img src="......">
                        String[] customSchemes = { "http", "https" };
                        if (new UrlValidator(customSchemes).isValid(val)) {
                            foundURL = true;
                        } else {
                            ret.invalidTags.add(attr + " " + val);
                            val = "";
                        }

                    } else if ("href".equals(attr) || "src".equals(attr)) { // <tag src/href="......">   skipped
                        ret.invalidTags.add(tag + " " + attr + " " + val);
                        continue;

                    } else if (attr.matches("width|height")) { // <tag width/height="......">
                        if (!val.toLowerCase().matches("\\d+%|\\d+$")) { // test numeric values
                            ret.invalidTags.add(tag + " " + attr + " " + val);
                            continue;
                        }

                    } else if ("style".equals(attr)) { // <tag style="......">

                        // then test properties
                        Matcher styles = stylePattern.matcher(val);
                        String cleanStyle = "";

                        while (styles.find()) {
                            String styleName = styles.group(1).toLowerCase();
                            String styleValue = styles.group(2);

                            // suppress invalid styles values
                            if (forbiddenStylePattern.matcher(styleValue).find()) {
                                ret.invalidTags.add(tag + " " + attr + " " + styleValue);
                                continue;
                            }

                            // check if valid url
                            Matcher urlStyleMatcher = urlStylePattern.matcher(styleValue);
                            if (urlStyleMatcher.find()) {
                                String[] customSchemes = { "http", "https" };
                                String url = urlStyleMatcher.group(1);
                                if (!new UrlValidator(customSchemes).isValid(url)) {
                                    ret.invalidTags.add(tag + " " + attr + " " + styleValue);
                                    continue;
                                }
                            }

                            cleanStyle = cleanStyle + styleName + ":" + encode(styleValue) + ";";

                        }
                        val = cleanStyle;

                    } else if (attr.startsWith("on")) { // skip all javascript events
                        ret.invalidTags.add(tag + " " + attr + " " + val);
                        continue;

                    } else { // by default encode all properies
                        val = encode(val);
                    }

                    cleanToken = cleanToken + " " + attr + "=\"" + val + "\"";
                }
                cleanToken = cleanToken + ">";

                isAcceptedToken = true;

                // for <img> and <a>
                if (tag.matches("a|img|embed") && !foundURL) {
                    isAcceptedToken = false;
                    cleanToken = "";
                }

                token = cleanToken;

                // push the tag if require closure and it is accepted (otherwirse is encoded)
                if (isAcceptedToken
                        && !(standAloneTags.matcher(tag).find() || selfClosed.matcher(tag).find())) {
                    openTags.push(tag);
                }

                // --------------------------------------------------------------------------------  UNKNOWN TAG
            } else {
                ret.invalidTags.add(token);
                ret.val = ret.val + token;
                continue;

            }

            // --------------------------------------------------------------------------------  CLOSE TAG </tag>
        } else if (endMatcher.find()) {
            String tag = endMatcher.group(1).toLowerCase();

            //is self closing
            if (selfClosed.matcher(tag).find()) {
                ret.invalidTags.add(token);
                continue;
            }
            if (forbiddenTags.matcher(tag).find()) {
                ret.invalidTags.add("/" + tag);
                continue;
            }
            if (!allowedTags.matcher(tag).find()) {
                ret.invalidTags.add(token);
                ret.val = ret.val + token;
                continue;
            } else {

                String cleanToken = "";

                // check tag position in the stack
                int pos = openTags.search(tag);
                // if found on top ok
                for (int i = 1; i <= pos; i++) {
                    //pop all elements before tag and close it
                    String poppedTag = openTags.pop();
                    cleanToken = cleanToken + "</" + poppedTag + ">";
                    isAcceptedToken = true;
                }

                token = cleanToken;
            }

        }

        ret.val = ret.val + token;

        if (isAcceptedToken) {
            ret.html = ret.html + token;
            //ret.text = ret.text + " ";
        } else {
            String sanToken = htmlEncodeApexesAndTags(token);
            ret.html = ret.html + sanToken;
            ret.text = ret.text + htmlEncodeApexesAndTags(removeLineFeed(token));
        }

    }

    // must close remaining tags
    while (openTags.size() > 0) {
        //pop all elements before tag and close it
        String poppedTag = openTags.pop();
        ret.html = ret.html + "</" + poppedTag + ">";
        ret.val = ret.val + "</" + poppedTag + ">";
    }

    //set boolean value
    ret.isValid = ret.invalidTags.size() == 0;

    return ret;
}

From source file:com.tmh.web.filter.xss.HtmlSanitizer.java

public static SanitizeResult sanitizer(String html, Pattern allowedTags, Pattern forbiddenTags) {
    SanitizeResult ret = new SanitizeResult();
    Stack<String> openTags = new Stack();

    List<String> tokens = tokenize(html);

    // -------------------   LOOP for every token --------------------------
    for (String token : tokens) {
        boolean isAcceptedToken = false;
        Matcher startMatcher = tagStartPattern.matcher(token);
        Matcher endMatcher = tagClosePattern.matcher(token);

        //  COMMENT    <!-- ......... -->
        if (commentPattern.matcher(token).find()) {
            ret.val = ret.val + token + (token.endsWith("-->") ? "" : "-->");
            ret.invalidTags.add(token + (token.endsWith("-->") ? "" : "-->"));
            continue;

        } //  STYLE SCRIPT   style=xss:expression
        else if (styleScriptPattern.matcher(token).find()) {
            ret.val = ret.val + token;
            ret.invalidTags.add(token);//  w w w.j a  v a2 s .c o m
            continue;

            //  OPEN TAG    <tag .........>
        } else if (startMatcher.find()) {

            //tag name extraction
            String tag = startMatcher.group(1).toLowerCase();

            //-----------------------------------------------------  FORBIDDEN TAG   <script .........>
            if (forbiddenTags.matcher(tag).find()) {
                ret.invalidTags.add("<" + tag + ">");
                continue;

                // --------------------------------------------------  WELL KNOWN TAG
            } else if (allowedTags.matcher(tag).find()) {

                String cleanToken = "<" + tag;
                String tokenBody = startMatcher.group(2);

                //first test table consistency
                //table tbody tfoot thead th tr td
                if ("thead".equals(tag) || "tbody".equals(tag) || "tfoot".equals(tag) || "tr".equals(tag)) {
                    if (openTags.search("table") < 1) {
                        ret.invalidTags.add("<" + tag + ">");
                        continue;
                    }
                } else if ("td".equals(tag) || "th".equals(tag)) {
                    if (openTags.search("tr") < 1) {
                        ret.invalidTags.add("<" + tag + ">");
                        continue;
                    }
                }

                // then test properties
                Matcher attributes = attributesPattern.matcher(tokenBody);

                boolean foundURL = false; // URL flag
                while (attributes.find()) {

                    String attr = attributes.group(1).toLowerCase();
                    String val = attributes.group(2);

                    // we will accept href in case of <A>
                    if ("a".equals(tag) && "href".equals(attr)) { // <a href="......">
                        String[] customSchemes = { "http", "https" };
                        if (new UrlValidator(customSchemes).isValid(val)) {
                            foundURL = true;
                        } else {
                            // may be it is a mailto?
                            //  case <a href="mailto:pippo@pippo.com?subject=...."
                            if (val.toLowerCase().startsWith("mailto:") && val.indexOf("@") >= 0) {
                                String val1 = "http://www." + val.substring(val.indexOf("@") + 1);
                                if (new UrlValidator(customSchemes).isValid(val1)) {
                                    foundURL = true;
                                } else {
                                    ret.invalidTags.add(attr + " " + val);
                                    val = "";
                                }
                            } else {
                                ret.invalidTags.add(attr + " " + val);
                                val = "";
                            }
                        }

                    } else if (tag.matches("img|embed") && "src".equals(attr)) { // <img src="......">
                        String[] customSchemes = { "http", "https" };
                        if (new UrlValidator(customSchemes).isValid(val)) {
                            foundURL = true;
                        } else {
                            ret.invalidTags.add(attr + " " + val);
                            val = "";
                        }

                    } else if ("href".equals(attr) || "src".equals(attr)) { // <tag src/href="......">   skipped
                        ret.invalidTags.add(tag + " " + attr + " " + val);
                        continue;

                    } else if (attr.matches("width|height")) { // <tag width/height="......">
                        if (!val.toLowerCase().matches("\\d+%|\\d+$")) { // test numeric values
                            ret.invalidTags.add(tag + " " + attr + " " + val);
                            continue;
                        }

                    } else if ("style".equals(attr)) { // <tag style="......">

                        // then test properties
                        Matcher styles = stylePattern.matcher(val);
                        String cleanStyle = "";

                        while (styles.find()) {
                            String styleName = styles.group(1).toLowerCase();
                            String styleValue = styles.group(2);

                            // suppress invalid styles values
                            if (forbiddenStylePattern.matcher(styleValue).find()) {
                                ret.invalidTags.add(tag + " " + attr + " " + styleValue);
                                continue;
                            }

                            // check if valid url
                            Matcher urlStyleMatcher = urlStylePattern.matcher(styleValue);
                            if (urlStyleMatcher.find()) {
                                String[] customSchemes = { "http", "https" };
                                String url = urlStyleMatcher.group(1);
                                if (!new UrlValidator(customSchemes).isValid(url)) {
                                    ret.invalidTags.add(tag + " " + attr + " " + styleValue);
                                    continue;
                                }
                            }

                            cleanStyle = cleanStyle + styleName + ":" + encode(styleValue) + ";";

                        }
                        val = cleanStyle;

                    } else if (attr.startsWith("on")) { // skip all javascript events
                        ret.invalidTags.add(tag + " " + attr + " " + val);
                        continue;

                    } else { // by default encode all properies
                        val = encode(val);
                    }

                    cleanToken = cleanToken + " " + attr + "=\"" + val + "\"";
                }
                cleanToken = cleanToken + ">";

                isAcceptedToken = true;

                // for <img> and <a>
                if (tag.matches("a|img|embed") && !foundURL) {
                    isAcceptedToken = false;
                    cleanToken = "";
                }

                token = cleanToken;

                // push the tag if require closure and it is accepted (otherwirse is encoded)
                if (isAcceptedToken && !(standAloneTags.matcher(tag).find() || selfClosed.matcher(tag).find()))
                    openTags.push(tag);

                //   UNKNOWN TAG
            } else {
                ret.invalidTags.add(token);
                ret.val = ret.val + token;
                continue;

            }

            //   CLOSE TAG </tag>
        } else if (endMatcher.find()) {
            String tag = endMatcher.group(1).toLowerCase();

            //is self closing
            if (selfClosed.matcher(tag).find()) {
                ret.invalidTags.add(token);
                continue;
            }
            if (forbiddenTags.matcher(tag).find()) {
                ret.invalidTags.add("/" + tag);
                continue;
            }
            if (!allowedTags.matcher(tag).find()) {
                ret.invalidTags.add(token);
                ret.val = ret.val + token;
                continue;
            } else {

                String cleanToken = "";

                // check tag position in the stack
                int pos = openTags.search(tag);
                // if found on top ok
                for (int i = 1; i <= pos; i++) {
                    //pop all elements before tag and close it
                    String poppedTag = openTags.pop();
                    cleanToken = cleanToken + "</" + poppedTag + ">";
                    isAcceptedToken = true;
                }

                token = cleanToken;
            }

        }

        ret.val = ret.val + token;

        if (isAcceptedToken) {
            ret.html = ret.html + token;
            //ret.text = ret.text + " ";
        } else {
            String sanToken = htmlEncodeApexesAndTags(token);
            ret.html = ret.html + sanToken;
            ret.text = ret.text + htmlEncodeApexesAndTags(removeLineFeed(token));
        }

    }

    // must close remaining tags
    while (openTags.size() > 0) {
        //pop all elements before tag and close it
        String poppedTag = openTags.pop();
        ret.html = ret.html + "</" + poppedTag + ">";
        ret.val = ret.val + "</" + poppedTag + ">";
    }

    //set boolean value
    ret.isValid = ret.invalidTags.size() == 0;

    return ret;
}