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:ecar.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   w ww  .  j  a v  a2 s  .c  om*/
                } 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.pironet.tda.SunJDKParser.java

/**
 * parse the next thread dump from the stream passed with the constructor.
 *
 * @return null if no more thread dumps were found.
 *///  ww  w .  j av  a2s .  c  om
public MutableTreeNode parseNext() {
    if (nextDump != null) {
        MutableTreeNode tmpDump = nextDump;
        nextDump = null;
        return (tmpDump);
    }
    boolean retry = false;
    String line = null;

    do {

        try {
            Map<String, String> threads = new HashMap<>();
            ThreadDumpInfo overallTDI = new ThreadDumpInfo("Dump No. " + counter++, 0);
            if (withCurrentTimeStamp) {
                overallTDI.setStartTime((new Date(System.currentTimeMillis())).toString());
            }
            DefaultMutableTreeNode threadDump = new DefaultMutableTreeNode(overallTDI);

            DefaultMutableTreeNode catThreads = new DefaultMutableTreeNode(
                    new TableCategory("Threads", IconFactory.THREADS));
            threadDump.add(catThreads);

            DefaultMutableTreeNode catWaiting = new DefaultMutableTreeNode(
                    new TableCategory("Threads waiting for Monitors", IconFactory.THREADS_WAITING));

            DefaultMutableTreeNode catSleeping = new DefaultMutableTreeNode(
                    new TableCategory("Threads sleeping on Monitors", IconFactory.THREADS_SLEEPING));

            DefaultMutableTreeNode catLocking = new DefaultMutableTreeNode(
                    new TableCategory("Threads locking Monitors", IconFactory.THREADS_LOCKING));

            // create category for monitors with disabled filtering.
            // NOTE:  These strings are "magic" in that the methods
            // TDA#displayCategory and TreeCategory#getCatComponent both
            // checks these literal strings and the behavior differs.
            DefaultMutableTreeNode catMonitors = new DefaultMutableTreeNode(
                    new TreeCategory("Monitors", IconFactory.MONITORS, false));
            DefaultMutableTreeNode catMonitorsLocks = new DefaultMutableTreeNode(
                    new TreeCategory("Monitors without locking thread", IconFactory.MONITORS_NO_LOCKS, false));
            DefaultMutableTreeNode catBlockingMonitors = new DefaultMutableTreeNode(
                    new TreeCategory("Threads blocked by Monitors", IconFactory.THREADS_LOCKING, false));

            String title = null;
            String dumpKey = null;
            StringBuffer content = null;
            boolean inLocking = false;
            boolean inSleeping = false;
            boolean inWaiting = false;
            int threadCount = 0;
            int waiting = 0;
            int locking = 0;
            int sleeping = 0;
            boolean locked = true;
            boolean finished = false;
            final MonitorMap mmap = new MonitorMap();
            final Stack<String> monitorStack = new Stack<>();
            long startTime = 0;
            int singleLineCounter = 0;
            boolean concurrentSyncsFlag = false;
            Matcher matched = getDm().getLastMatch();

            while (getBis().ready() && !finished) {
                line = getNextLine();
                lineCounter++;
                singleLineCounter++;
                if (locked) {
                    if (line.contains("Full thread dump")) {
                        locked = false;
                        if (!withCurrentTimeStamp) {
                            overallTDI.setLogLine(lineCounter);

                            if (startTime != 0) {
                                startTime = 0;
                            } else if (matched != null && matched.matches()) {

                                String parsedStartTime = matched.group(1);
                                if (!getDm().isDefaultMatches() && isMillisTimeStamp()) {
                                    try {
                                        // the factor is a hack for a bug in oc4j timestamp printing (pattern timeStamp=2342342340)
                                        if (parsedStartTime.length() < 13) {
                                            startTime = Long.parseLong(parsedStartTime)
                                                    * (long) Math.pow(10, 13 - parsedStartTime.length());
                                        } else {
                                            startTime = Long.parseLong(parsedStartTime);
                                        }
                                    } catch (NumberFormatException nfe) {
                                        startTime = 0;
                                    }
                                    if (startTime > 0) {
                                        overallTDI.setStartTime((new Date(startTime)).toString());
                                    }
                                } else {
                                    overallTDI.setStartTime(parsedStartTime);
                                }
                                matched = null;
                                getDm().resetLastMatch();
                            }
                        }
                        dumpKey = overallTDI.getName();
                    } else if (!getDm().isPatternError() && (getDm().getRegexPattern() != null)) {
                        Matcher m = getDm().checkForDateMatch(line);
                        if (m != null) {
                            matched = m;
                        }
                    }
                } else {
                    if (line.startsWith("\"")) {
                        // We are starting a group of lines for a different thread
                        // First, flush state for the previous thread (if any)
                        concurrentSyncsFlag = false;
                        String stringContent = content != null ? content.toString() : null;
                        if (title != null) {
                            threads.put(title, content.toString());
                            content.append("</pre></pre>");
                            addToCategory(catThreads, title, null, stringContent, singleLineCounter, true);
                            threadCount++;
                        }
                        if (inWaiting) {
                            addToCategory(catWaiting, title, null, stringContent, singleLineCounter, true);
                            inWaiting = false;
                            waiting++;
                        }
                        if (inSleeping) {
                            addToCategory(catSleeping, title, null, stringContent, singleLineCounter, true);
                            inSleeping = false;
                            sleeping++;
                        }
                        if (inLocking) {
                            addToCategory(catLocking, title, null, stringContent, singleLineCounter, true);
                            inLocking = false;
                            locking++;
                        }
                        singleLineCounter = 0;
                        while (!monitorStack.empty()) {
                            mmap.parseAndAddThread(monitorStack.pop(), title, content.toString());
                        }

                        // Second, initialize state for this new thread
                        title = line;
                        content = new StringBuffer("<body bgcolor=\"ffffff\"><pre><font size="
                                + TDA.getFontSizeModifier(-1) + '>');
                        content.append(line);
                        content.append('\n');
                    } else if (line.contains("at ")) {
                        content.append(line);
                        content.append('\n');
                    } else if (line.contains("java.lang.Thread.State")) {
                        content.append(line);
                        content.append('\n');
                        if (title.indexOf("t@") > 0) {
                            // in this case the title line is missing state informations
                            String state = line.substring(line.indexOf(':') + 1).trim();
                            if (state.indexOf(' ') > 0) {
                                title += " state=" + state.substring(0, state.indexOf(' '));
                            } else {
                                title += " state=" + state;
                            }
                        }
                    } else if (line.contains("Locked ownable synchronizers:")) {
                        concurrentSyncsFlag = true;
                        content.append(line);
                        content.append('\n');
                    } else if (line.contains("- waiting on")) {
                        content.append(linkifyMonitor(line));
                        monitorStack.push(line);
                        inSleeping = true;
                        content.append('\n');
                    } else if (line.contains("- parking to wait")) {
                        content.append(linkifyMonitor(line));
                        monitorStack.push(line);
                        inSleeping = true;
                        content.append('\n');
                    } else if (line.contains("- waiting to")) {
                        content.append(linkifyMonitor(line));
                        monitorStack.push(line);
                        inWaiting = true;
                        content.append('\n');
                    } else if (line.contains("- locked")) {
                        content.append(linkifyMonitor(line));
                        inLocking = true;
                        monitorStack.push(line);
                        content.append('\n');
                    } else if (line.contains("- ")) {
                        if (concurrentSyncsFlag) {
                            content.append(linkifyMonitor(line));
                            monitorStack.push(line);
                        } else {
                            content.append(line);
                        }
                        content.append('\n');
                    }

                    // last thread reached?
                    if ((line.contains("\"Suspend Checker Thread\""))
                            || (line.contains("\"VM Periodic Task Thread\""))
                            || (line.contains("<EndOfDump>"))) {
                        finished = true;
                        getBis().mark(getMarkSize());
                        if ((checkForDeadlocks(threadDump)) == 0) {
                            // no deadlocks found, set back original position.
                            getBis().reset();
                        }

                        if (!checkThreadDumpStatData(overallTDI)) {
                            // no statistical data found, set back original position.
                            getBis().reset();
                        }

                        getBis().mark(getMarkSize());
                        if (!(foundClassHistograms = checkForClassHistogram(threadDump))) {
                            getBis().reset();
                        }
                    }
                }
            }
            // last thread
            String stringContent = content != null ? content.toString() : null;
            if (title != null) {
                threads.put(title, content.toString());
                content.append("</pre></pre>");
                addToCategory(catThreads, title, null, stringContent, singleLineCounter, true);
                threadCount++;
            }
            if (inWaiting) {
                addToCategory(catWaiting, title, null, stringContent, singleLineCounter, true);
                waiting++;
            }
            if (inSleeping) {
                addToCategory(catSleeping, title, null, stringContent, singleLineCounter, true);
                sleeping++;
            }
            if (inLocking) {
                addToCategory(catLocking, title, null, stringContent, singleLineCounter, true);
                locking++;
            }
            while (!monitorStack.empty()) {
                mmap.parseAndAddThread(monitorStack.pop(), title, content.toString());
            }

            int monitorCount = mmap.size();

            int monitorsWithoutLocksCount = 0;
            int contendedMonitors = 0;
            int blockedThreads = 0;
            // dump monitors 
            if (mmap.size() > 0) {
                int[] result = dumpMonitors(catMonitors, catMonitorsLocks, mmap);
                monitorsWithoutLocksCount = result[0];
                overallTDI.setOverallThreadsWaitingWithoutLocksCount(result[1]);

                result = dumpBlockingMonitors(catBlockingMonitors, mmap);
                contendedMonitors = result[0];
                blockedThreads = result[1];
            }

            // display nodes with stuff to display
            if (waiting > 0) {
                overallTDI.setWaitingThreads((Category) catWaiting.getUserObject());
                threadDump.add(catWaiting);
            }

            if (sleeping > 0) {
                overallTDI.setSleepingThreads((Category) catSleeping.getUserObject());
                threadDump.add(catSleeping);
            }

            if (locking > 0) {
                overallTDI.setLockingThreads((Category) catLocking.getUserObject());
                threadDump.add(catLocking);
            }

            if (monitorCount > 0) {
                overallTDI.setMonitors((Category) catMonitors.getUserObject());
                threadDump.add(catMonitors);
            }

            if (contendedMonitors > 0) {
                overallTDI.setBlockingMonitors((Category) catBlockingMonitors.getUserObject());
                threadDump.add(catBlockingMonitors);
            }

            if (monitorsWithoutLocksCount > 0) {
                overallTDI.setMonitorsWithoutLocks((Category) catMonitorsLocks.getUserObject());
                threadDump.add(catMonitorsLocks);
            }
            overallTDI.setThreads((Category) catThreads.getUserObject());

            ((Category) catThreads.getUserObject())
                    .setName(catThreads.getUserObject() + " (" + threadCount + " Threads overall)");
            ((Category) catWaiting.getUserObject())
                    .setName(catWaiting.getUserObject() + " (" + waiting + " Threads waiting)");
            ((Category) catSleeping.getUserObject())
                    .setName(catSleeping.getUserObject() + " (" + sleeping + " Threads sleeping)");
            ((Category) catLocking.getUserObject())
                    .setName(catLocking.getUserObject() + " (" + locking + " Threads locking)");
            ((Category) catMonitors.getUserObject())
                    .setName(catMonitors.getUserObject() + " (" + monitorCount + " Monitors)");
            ((Category) catBlockingMonitors.getUserObject()).setName(catBlockingMonitors.getUserObject() + " ("
                    + blockedThreads + " Threads blocked by " + contendedMonitors + " Monitors)");
            ((Category) catMonitorsLocks.getUserObject()).setName(
                    catMonitorsLocks.getUserObject() + " (" + monitorsWithoutLocksCount + " Monitors)");
            // add thread dump to passed dump store.
            if ((threadCount > 0) && (dumpKey != null)) {
                threadStore.put(dumpKey.trim(), threads);
            }

            // check custom categories
            addCustomCategories(threadDump);

            return (threadCount > 0 ? threadDump : null);
        } catch (StringIndexOutOfBoundsException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                    "Error during parsing of a found thread dump, skipping to next one!\n"
                            + "Check for possible broken dumps, sometimes, stream flushing mixes the logged data.\n"
                            + "Error Message is \"" + e.getLocalizedMessage() + "\". \n"
                            + (line != null ? "Last line read was \"" + line + "\". \n" : ""),
                    "Error during Parsing Thread Dump", JOptionPane.ERROR_MESSAGE);
            retry = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } while (retry);

    return (null);
}

From source file:it.doqui.index.ecmengine.business.personalization.splitting.SplittingDbNodeServiceImpl.java

/**
 * Recursive method used to build up paths from a given node to the root.
 * <p>/*from  w  w  w. j  av a2  s  .co  m*/
 * Whilst walking up the hierarchy to the root, some nodes may have a <b>root</b> aspect.
 * Everytime one of these is encountered, a new path is farmed off, but the method
 * continues to walk up the hierarchy.
 *
 * @param currentNode the node to start from, i.e. the child node to work upwards from
 * @param currentPath the path from the current node to the descendent that we started from
 * @param completedPaths paths that have reached the root are added to this collection
 * @param assocStack the parent-child relationships traversed whilst building the path.
 *      Used to detected cyclic relationships.
 * @param primaryOnly true if only the primary parent association must be traversed.
 *      If this is true, then the only root is the top level node having no parents.
 * @throws CyclicChildRelationshipException
 */
private void prependPaths(final Node currentNode, final Path currentPath, Collection<Path> completedPaths,
        Stack<ChildAssoc> assocStack, boolean primaryOnly) throws CyclicChildRelationshipException {
    logger.debug("[SplittingDbNodeServiceImpl::prependPaths] BEGIN");

    try {
        Assert.isTrue(!isPart(currentNode), "BUG: prependPaths() on part node!");
        NodeRef currentNodeRef = currentNode.getNodeRef();

        // get the parent associations of the given node
        Collection<ChildAssoc> parentAssocs = nodeDaoService.getParentAssocs(currentNode);

        // does the node have parents
        boolean hasParents = !parentAssocs.isEmpty();

        // does the current node have a root aspect?
        boolean isRoot = currentNode.getAspects().contains(ContentModel.ASPECT_ROOT);
        boolean isStoreRoot = currentNode.getTypeQName().equals(ContentModel.TYPE_STOREROOT);

        // look for a root.  If we only want the primary root, then ignore all but the top-level root.
        if (isRoot && !(primaryOnly && hasParents)) { // exclude primary search with parents present

            // create a one-sided association reference for the root node and prepend to the stack
            // this effectively spoofs the fact that the current node is not below the root
            // - we put this association in as the first association in the path must be a one-sided
            //   reference pointing to the root node
            ChildAssociationRef assocRef = new ChildAssociationRef(null, null, null, // Type, parent and QName
                    getRootNode(currentNode.getNodeRef().getStoreRef()));

            // create a path to save and add the 'root' association
            Path pathToSave = new Path();
            Path.ChildAssocElement first = null;

            for (Path.Element element : currentPath) {
                if (first == null) {
                    first = (Path.ChildAssocElement) element;
                } else {
                    pathToSave.append(element);
                }
            }

            if (first != null) {

                // mimic an association that would appear if the current node was below
                // the root node
                // or if first beneath the root node it will make the real thing
                ChildAssociationRef updateAssocRef = new ChildAssociationRef(
                        isStoreRoot ? ContentModel.ASSOC_CHILDREN : first.getRef().getTypeQName(),
                        getRootNode(currentNode.getNodeRef().getStoreRef()), first.getRef().getQName(),
                        first.getRef().getChildRef());
                Path.Element newFirst = new Path.ChildAssocElement(updateAssocRef);
                pathToSave.prepend(newFirst);
            }

            Path.Element element = new Path.ChildAssocElement(assocRef);
            pathToSave.prepend(element);

            // store the path just built
            completedPaths.add(pathToSave);
        }

        if (!hasParents && !isRoot) {
            throw new RuntimeException("Node without parents does not have root aspect: " + currentNodeRef);
        }

        // walk up each parent association
        for (ChildAssoc assoc : parentAssocs) {

            // does the association already exist in the stack
            if (assocStack.contains(assoc)) {
                // the association was present already
                throw new CyclicChildRelationshipException(
                        "Cyclic parent-child relationship detected: \n" + "   current node: " + currentNode
                                + "\n" + "   current path: " + currentPath + "\n" + "   next assoc: " + assoc,
                        assoc);
            }

            // do we consider only primary associations?
            if (primaryOnly && !assoc.getIsPrimary()) {
                continue;
            }

            Node realParent = getPartsContainerNode(assoc.getParent());

            // build a real association reference
            ChildAssociationRef assocRef = new ChildAssociationRef(assoc.getTypeQName(),
                    tenantService.getBaseName(realParent.getNodeRef()), assoc.getQname(),
                    tenantService.getBaseName(assoc.getChild().getNodeRef()), assoc.getIsPrimary(), -1);

            // Ordering is not important here: We are building distinct paths upwards
            Path.Element element = new Path.ChildAssocElement(assocRef);

            // create a new path that builds on the current path
            Path path = new Path();
            path.append(currentPath);

            // prepend element
            path.prepend(element);

            // push the associations stack, recurse and pop
            assocStack.push(assoc);
            prependPaths(realParent, path, completedPaths, assocStack, primaryOnly);
            assocStack.pop();
        }
    } finally {
        logger.debug("[SplittingDbNodeServiceImpl::prependPaths] END");
    }
}

From source file:org.alfresco.repo.domain.node.AbstractNodeDAOImpl.java

/**
 * Build the paths for a node/*ww  w.j  a va2  s .  co  m*/
 * 
 * @param currentNodePair       the leave or child node to start with
 * @param currentRootNodePair   pass in <tt>null</tt> only 
 * @param currentPath           an empty {@link Path}
 * @param completedPaths        completed paths i.e. the result
 * @param assocIdStack          a stack to detected cyclic relationships
 * @param primaryOnly           <tt>true</tt> to follow only primary parent associations
 * @throws CyclicChildRelationshipException
 */
private void prependPaths(Pair<Long, NodeRef> currentNodePair, Pair<StoreRef, NodeRef> currentRootNodePair,
        Path currentPath, Collection<Path> completedPaths, Stack<Long> assocIdStack, boolean primaryOnly)
        throws CyclicChildRelationshipException {
    if (isDebugEnabled) {
        logger.debug("\n" + "Prepending paths: \n" + "   Current node: " + currentNodePair + "\n"
                + "   Current root: " + currentRootNodePair + "\n" + "   Current path: " + currentPath);
    }
    Long currentNodeId = currentNodePair.getFirst();
    NodeRef currentNodeRef = currentNodePair.getSecond();

    // Check if we have changed root nodes
    StoreRef currentStoreRef = currentNodeRef.getStoreRef();
    if (currentRootNodePair == null || !currentStoreRef.equals(currentRootNodePair.getFirst())) {
        // We've changed stores
        Pair<Long, NodeRef> rootNodePair = getRootNode(currentStoreRef);
        currentRootNodePair = new Pair<StoreRef, NodeRef>(currentStoreRef, rootNodePair.getSecond());
    }

    // get the parent associations of the given node
    ParentAssocsInfo parentAssocInfo = getParentAssocsCached(currentNodeId); // note: currently may throw NotLiveNodeException
    // bulk load parents as we are certain to hit them in the next call
    ArrayList<Long> toLoad = new ArrayList<Long>(parentAssocInfo.getParentAssocs().size());
    for (Map.Entry<Long, ChildAssocEntity> entry : parentAssocInfo.getParentAssocs().entrySet()) {
        toLoad.add(entry.getValue().getParentNode().getId());
    }
    cacheNodesById(toLoad);

    // does the node have parents
    boolean hasParents = parentAssocInfo.getParentAssocs().size() > 0;
    // does the current node have a root aspect?

    // look for a root. If we only want the primary root, then ignore all but the top-level root.
    if (!(primaryOnly && hasParents) && parentAssocInfo.isRoot()) // exclude primary search with parents present
    {
        // create a one-sided assoc ref for the root node and prepend to the stack
        // this effectively spoofs the fact that the current node is not below the root
        // - we put this assoc in as the first assoc in the path must be a one-sided
        // reference pointing to the root node
        ChildAssociationRef assocRef = new ChildAssociationRef(null, null, null,
                currentRootNodePair.getSecond());
        // create a path to save and add the 'root' assoc
        Path pathToSave = new Path();
        Path.ChildAssocElement first = null;
        for (Path.Element element : currentPath) {
            if (first == null) {
                first = (Path.ChildAssocElement) element;
            } else {
                pathToSave.append(element);
            }
        }
        if (first != null) {
            // mimic an association that would appear if the current node was below the root node
            // or if first beneath the root node it will make the real thing
            ChildAssociationRef updateAssocRef = new ChildAssociationRef(
                    parentAssocInfo.isStoreRoot() ? ContentModel.ASSOC_CHILDREN : first.getRef().getTypeQName(),
                    currentRootNodePair.getSecond(), first.getRef().getQName(), first.getRef().getChildRef());
            Path.Element newFirst = new Path.ChildAssocElement(updateAssocRef);
            pathToSave.prepend(newFirst);
        }

        Path.Element element = new Path.ChildAssocElement(assocRef);
        pathToSave.prepend(element);

        // store the path just built
        completedPaths.add(pathToSave);
    }

    // walk up each parent association
    for (Map.Entry<Long, ChildAssocEntity> entry : parentAssocInfo.getParentAssocs().entrySet()) {
        Long assocId = entry.getKey();
        ChildAssocEntity assoc = entry.getValue();
        ChildAssociationRef assocRef = assoc.getRef(qnameDAO);
        // do we consider only primary assocs?
        if (primaryOnly && !assocRef.isPrimary()) {
            continue;
        }
        // Ordering is meaningless here as we are constructing a path upwards
        // and have no idea where the node comes in the sibling order or even
        // if there are like-pathed siblings.
        assocRef.setNthSibling(-1);
        // build a path element
        Path.Element element = new Path.ChildAssocElement(assocRef);
        // create a new path that builds on the current path
        Path path = new Path();
        path.append(currentPath);
        // prepend element
        path.prepend(element);
        // get parent node pair
        Pair<Long, NodeRef> parentNodePair = new Pair<Long, NodeRef>(assoc.getParentNode().getId(),
                assocRef.getParentRef());

        // does the association already exist in the stack
        if (assocIdStack.contains(assocId)) {
            // the association was present already
            logger.error("Cyclic parent-child relationship detected: \n" + "   current node: " + currentNodeId
                    + "\n" + "   current path: " + currentPath + "\n" + "   next assoc: " + assocId);
            throw new CyclicChildRelationshipException("Node has been pasted into its own tree.", assocRef);
        }

        if (isDebugEnabled) {
            logger.debug("\n" + "   Prepending path parent: \n" + "      Parent node: " + parentNodePair);
        }

        // push the assoc stack, recurse and pop
        assocIdStack.push(assocId);

        prependPaths(parentNodePair, currentRootNodePair, path, completedPaths, assocIdStack, primaryOnly);

        assocIdStack.pop();
    }
    // done
}

From source file:com.zimbra.cs.account.ldap.LdapProvisioning.java

private List<DistributionList> getContainingDistributionLists(Entry entry, boolean directOnly,
        Map<String, String> via) throws ServiceException {
    List<DistributionList> directDLs = getAllDirectDLs(this, entry);
    HashSet<String> directDLSet = new HashSet<String>();
    HashSet<String> checked = new HashSet<String>();
    List<DistributionList> result = new ArrayList<DistributionList>();

    Stack<DistributionList> dlsToCheck = new Stack<DistributionList>();

    for (DistributionList dl : directDLs) {
        dlsToCheck.push(dl);
        directDLSet.add(dl.getName());//from  www  . j av  a  2 s .c o m
    }

    while (!dlsToCheck.isEmpty()) {
        DistributionList dl = dlsToCheck.pop();
        if (checked.contains(dl.getId()))
            continue;
        result.add(dl);
        checked.add(dl.getId());
        if (directOnly)
            continue;

        List<DistributionList> newLists = getAllDirectDLs(this, dl);

        for (DistributionList newDl : newLists) {
            if (!directDLSet.contains(newDl.getName())) {
                if (via != null) {
                    via.put(newDl.getName(), dl.getName());
                }
                dlsToCheck.push(newDl);
            }
        }
    }
    Collections.sort(result);
    return result;
}

From source file:com.angkorteam.framework.swagger.factory.SwaggerFactory.java

@Override
public void afterPropertiesSet() throws Exception {
    Swagger swagger = new Swagger();
    io.swagger.models.Info info = new io.swagger.models.Info();
    swagger.setInfo(info);/*  w  w w  .  j a  va 2s .c  o  m*/
    info.setTitle(title);
    info.setLicense(license);
    info.setContact(contact);
    info.setTermsOfService(termsOfService);
    info.setVersion(version);
    info.setDescription(description);

    swagger.setConsumes(consumes);
    swagger.setProduces(produces);
    swagger.setSchemes(schemes);
    swagger.setBasePath(basePath);
    swagger.setHost(host);
    swagger.setExternalDocs(externalDocs);

    ConfigurationBuilder config = new ConfigurationBuilder();
    Set<String> acceptablePackages = new HashSet<>();

    boolean allowAllPackages = false;

    if (resourcePackages != null && resourcePackages.length > 0) {
        for (String resourcePackage : resourcePackages) {
            if (resourcePackage != null && !"".equals(resourcePackage)) {
                acceptablePackages.add(resourcePackage);
                config.addUrls(ClasspathHelper.forPackage(resourcePackage));
            }
        }
    }

    config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());

    Reflections reflections = new Reflections(config);
    Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class);

    Set<Class<?>> output = new HashSet<Class<?>>();
    for (Class<?> cls : controllers) {
        if (allowAllPackages) {
            output.add(cls);
        } else {
            for (String pkg : acceptablePackages) {
                if (cls.getPackage().getName().startsWith(pkg)) {
                    output.add(cls);
                }
            }
        }
    }

    Map<String, Path> paths = new HashMap<>();
    swagger.setPaths(paths);
    Map<String, Model> definitions = new HashMap<>();
    swagger.setDefinitions(definitions);
    Stack<Class<?>> modelStack = new Stack<>();
    for (Class<?> controller : controllers) {
        List<String> clazzPaths = new ArrayList<>();
        RequestMapping clazzRequestMapping = controller.getDeclaredAnnotation(RequestMapping.class);
        Api api = controller.getDeclaredAnnotation(Api.class);
        if (clazzRequestMapping != null) {
            clazzPaths = lookPaths(clazzRequestMapping);
        }
        if (clazzPaths.isEmpty()) {
            clazzPaths.add("");
        }
        if (api != null) {
            if (!"".equals(api.description())) {
                for (String name : api.tags()) {
                    if (!"".equals(name)) {
                        io.swagger.models.Tag tag = new io.swagger.models.Tag();
                        tag.setDescription(api.description());
                        tag.setName(name);
                        swagger.addTag(tag);
                    }
                }
            }
        } else {
            io.swagger.models.Tag tag = new io.swagger.models.Tag();
            tag.setDescription("Unknown");
            tag.setName("Unknown");
            swagger.addTag(tag);
        }
        Method[] methods = null;
        try {
            methods = controller.getDeclaredMethods();
        } catch (NoClassDefFoundError e) {
        }
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
                RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
                ApiResponses apiResponses = method.getAnnotation(ApiResponses.class);
                ApiHeaders apiHeaders = method.getAnnotation(ApiHeaders.class);
                List<String> methodPaths = new ArrayList<>();
                if (requestMapping != null && apiOperation != null && apiResponses != null) {
                    methodPaths = lookPaths(requestMapping);
                }
                if (methodPaths.isEmpty()) {
                    methodPaths.add("");
                }
                if (requestMapping != null && apiOperation != null && apiResponses != null) {
                    for (String classPath : clazzPaths) {
                        for (String methodPath : methodPaths) {
                            RequestMethod[] requestMethods = requestMapping.method();
                            if (requestMethods == null || requestMethods.length == 0) {
                                requestMethods = RequestMethod.values();
                            }
                            Path path = new Path();
                            paths.put(classPath + methodPath, path);
                            for (RequestMethod requestMethod : requestMethods) {
                                Operation operation = new Operation();
                                operation.setDescription(apiOperation.description());
                                for (String consume : requestMapping.consumes()) {
                                    operation.addConsumes(consume);
                                }
                                for (String produce : requestMapping.produces()) {
                                    operation.addProduces(produce);
                                }
                                if (api != null) {
                                    if (!"".equals(api.description())) {
                                        for (String name : api.tags()) {
                                            if (!"".equals(name)) {
                                                operation.addTag(name);
                                            }
                                        }
                                    }
                                } else {
                                    io.swagger.models.Tag tag = new io.swagger.models.Tag();
                                    operation.addTag("Unknown");
                                }

                                if (requestMethod == RequestMethod.DELETE) {
                                    path.delete(operation);
                                } else if (requestMethod == RequestMethod.GET) {
                                    path.get(operation);
                                } else if (requestMethod == RequestMethod.HEAD) {
                                    path.head(operation);
                                } else if (requestMethod == RequestMethod.OPTIONS) {
                                    path.options(operation);
                                } else if (requestMethod == RequestMethod.PATCH) {
                                    path.patch(operation);
                                } else if (requestMethod == RequestMethod.POST) {
                                    path.post(operation);
                                } else if (requestMethod == RequestMethod.PUT) {
                                    path.put(operation);
                                }

                                if (apiHeaders != null && apiHeaders.value() != null
                                        && apiHeaders.value().length > 0) {
                                    for (ApiHeader header : apiHeaders.value()) {
                                        HeaderParameter parameter = new HeaderParameter();
                                        parameter.setName(header.name());
                                        parameter.setType("string");
                                        parameter.setDescription(header.description());
                                        parameter.setRequired(header.required());
                                        operation.addParameter(parameter);
                                    }
                                }

                                for (Parameter parameter : method.getParameters()) {
                                    PathVariable pathVariable = parameter.getAnnotation(PathVariable.class);
                                    RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
                                    RequestBody requestBody = parameter.getAnnotation(RequestBody.class);
                                    RequestPart requestPart = parameter.getAnnotation(RequestPart.class);
                                    ApiParam apiParam = parameter.getAnnotation(ApiParam.class);
                                    if (apiParam != null && pathVariable != null
                                            && isSimpleScalar(parameter.getType())) {
                                        PathParameter pathParameter = new PathParameter();
                                        pathParameter.setRequired(true);
                                        pathParameter.setDescription(apiParam.description());
                                        pathParameter.setType(lookupType(parameter.getType()));
                                        pathParameter.setFormat(lookupFormat(parameter.getType(), apiParam));
                                        pathParameter.setName(pathVariable.value());
                                        operation.addParameter(pathParameter);
                                        continue;
                                    }

                                    if (requestMethod == RequestMethod.DELETE
                                            || requestMethod == RequestMethod.GET
                                            || requestMethod == RequestMethod.HEAD
                                            || requestMethod == RequestMethod.OPTIONS
                                            || requestMethod == RequestMethod.PATCH
                                            || requestMethod == RequestMethod.PUT) {
                                        if (apiParam != null && requestParam != null
                                                && isSimpleArray(parameter.getType())) {
                                            QueryParameter param = new QueryParameter();
                                            param.setRequired(requestParam.required());
                                            param.setDescription(apiParam.description());
                                            param.setType("array");
                                            if (!"".equals(requestParam.value())) {
                                                param.setName(requestParam.value());
                                            }
                                            if (!"".equals(requestParam.name())) {
                                                param.setName(requestParam.name());
                                            }
                                            param.setItems(lookupProperty(parameter.getType(), requestParam,
                                                    apiParam));
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestParam != null
                                                && isSimpleScalar(parameter.getType())) {
                                            QueryParameter param = new QueryParameter();
                                            param.setRequired(requestParam.required());
                                            param.setDescription(apiParam.description());
                                            param.setType(lookupType(parameter.getType()));
                                            param.setFormat(lookupFormat(parameter.getType(), apiParam));
                                            if (!"".equals(requestParam.value())) {
                                                param.setName(requestParam.value());
                                            }
                                            if (!"".equals(requestParam.name())) {
                                                param.setName(requestParam.name());
                                            }
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestBody != null
                                                && parameter.getType() == MultipartFile.class) {
                                            FormParameter param = new FormParameter();
                                            param.setRequired(true);
                                            param.setIn("body");
                                            param.setName("body");
                                            param.setType("file");
                                            param.setDescription(apiParam.description());
                                            operation.addConsumes("application/octet-stream");
                                            //                                                BodyParameter param = new BodyParameter();
                                            //                                                param.setRequired(requestBody.required());
                                            //                                                param.setDescription(apiParam.description());
                                            //                                                param.setName("body");
                                            //                                                ModelImpl model = new ModelImpl();
                                            //                                                model.setType("file");
                                            //                                                param.setSchema(model);
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestBody != null
                                                && isSimpleArray(parameter.getType())) {
                                            BodyParameter param = new BodyParameter();
                                            param.setRequired(requestBody.required());
                                            param.setDescription(apiParam.description());
                                            param.setName("body");
                                            ArrayModel model = new ArrayModel();
                                            StringProperty property = new StringProperty();
                                            property.setType(lookupType(parameter.getType()));
                                            property.setFormat(lookupFormat(parameter.getType(), apiParam));
                                            model.setItems(property);
                                            param.setSchema(model);
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestBody != null
                                                && isSimpleScalar(parameter.getType())) {
                                            BodyParameter param = new BodyParameter();
                                            param.setRequired(requestBody.required());
                                            param.setDescription(apiParam.description());
                                            param.setName("body");
                                            ModelImpl model = new ModelImpl();
                                            model.setType(lookupType(parameter.getType()));
                                            model.setFormat(lookupFormat(parameter.getType(), apiParam));
                                            param.setSchema(model);
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestBody != null
                                                && isModelArray(parameter.getType())) {
                                            BodyParameter param = new BodyParameter();
                                            param.setRequired(requestBody.required());
                                            param.setDescription(apiParam.description());
                                            param.setName("body");
                                            ArrayModel model = new ArrayModel();
                                            RefProperty property = new RefProperty();
                                            property.setType(lookupType(parameter.getType()));
                                            property.set$ref("#/definitions/"
                                                    + parameter.getType().getComponentType().getSimpleName());
                                            if (!modelStack.contains(parameter.getType().getComponentType())) {
                                                modelStack.push(parameter.getType().getComponentType());
                                            }
                                            model.setItems(property);
                                            param.setSchema(model);
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestBody != null
                                                && isModelScalar(parameter.getType())) {
                                            BodyParameter param = new BodyParameter();
                                            param.setRequired(requestBody.required());
                                            param.setDescription(apiParam.description());
                                            param.setName("body");
                                            RefModel model = new RefModel();
                                            model.set$ref(
                                                    "#/definitions/" + parameter.getType().getSimpleName());
                                            if (!modelStack.contains(parameter.getType())) {
                                                modelStack.push(parameter.getType());
                                            }
                                            param.setSchema(model);
                                            operation.addParameter(param);
                                            continue;
                                        }
                                    } else if (requestMethod == RequestMethod.POST) {
                                        if (apiParam != null && requestParam != null
                                                && isSimpleArray(parameter.getType())) {
                                            FormParameter param = new FormParameter();
                                            param.setRequired(requestParam.required());
                                            param.setDescription(apiParam.description());
                                            param.setType("array");
                                            if (!"".equals(requestParam.value())) {
                                                param.setName(requestParam.value());
                                            }
                                            if (!"".equals(requestParam.name())) {
                                                param.setName(requestParam.name());
                                            }
                                            param.setItems(lookupProperty(parameter.getType(), requestParam,
                                                    apiParam));
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestParam != null
                                                && isSimpleScalar(parameter.getType())) {
                                            FormParameter param = new FormParameter();
                                            param.setRequired(requestParam.required());
                                            param.setDescription(apiParam.description());
                                            param.setType(lookupType(parameter.getType()));
                                            param.setFormat(lookupFormat(parameter.getType(), apiParam));
                                            if (!"".equals(requestParam.value())) {
                                                param.setName(requestParam.value());
                                            }
                                            if (!"".equals(requestParam.name())) {
                                                param.setName(requestParam.name());
                                            }
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestPart != null
                                                && isSimpleArray(parameter.getType())) {
                                            FormParameter param = new FormParameter();
                                            param.setRequired(requestPart.required());
                                            param.setDescription(apiParam.description());
                                            param.setType("array");
                                            if (!"".equals(requestPart.value())) {
                                                param.setName(requestPart.value());
                                            }
                                            if (!"".equals(requestPart.name())) {
                                                param.setName(requestPart.name());
                                            }
                                            param.setItems(lookupProperty(parameter.getType(), requestParam,
                                                    apiParam));
                                            operation.addParameter(param);
                                            continue;
                                        }
                                        if (apiParam != null && requestPart != null
                                                && isSimpleScalar(parameter.getType())) {
                                            FormParameter param = new FormParameter();
                                            param.setRequired(requestPart.required());
                                            param.setDescription(apiParam.description());
                                            param.setType(lookupType(parameter.getType()));
                                            param.setFormat(lookupFormat(parameter.getType(), apiParam));
                                            if (!"".equals(requestPart.value())) {
                                                param.setName(requestPart.value());
                                            }
                                            if (!"".equals(requestPart.name())) {
                                                param.setName(requestPart.name());
                                            }
                                            operation.addParameter(param);
                                            continue;
                                        }
                                    }
                                }

                                for (ApiResponse apiResponse : apiResponses.value()) {
                                    if (isSimpleScalar(apiResponse.response())) {
                                        if (apiResponse.array()) {
                                            Response response = new Response();
                                            if (!"".equals(apiResponse.description())) {
                                                response.setDescription(
                                                        apiResponse.httpStatus().getReasonPhrase());
                                            } else {
                                                response.setDescription(apiResponse.description());
                                            }
                                            ArrayProperty property = new ArrayProperty();
                                            property.setItems(
                                                    lookupProperty(apiResponse.response(), apiResponse));
                                            response.setSchema(property);
                                            operation.addResponse(
                                                    String.valueOf(apiResponse.httpStatus().value()), response);
                                        } else {
                                            Response response = new Response();
                                            if ("".equals(apiResponse.description())) {
                                                response.setDescription(
                                                        apiResponse.httpStatus().getReasonPhrase());
                                            } else {
                                                response.setDescription(apiResponse.description());
                                            }
                                            response.setSchema(
                                                    lookupProperty(apiResponse.response(), apiResponse));
                                            operation.addResponse(
                                                    String.valueOf(apiResponse.httpStatus().value()), response);
                                        }
                                    } else if (isModelScalar(apiResponse.response())) {
                                        if (apiResponse.array()) {
                                            Response response = new Response();
                                            if (!"".equals(apiResponse.description())) {
                                                response.setDescription(
                                                        apiResponse.httpStatus().getReasonPhrase());
                                            } else {
                                                response.setDescription(apiResponse.description());
                                            }
                                            RefProperty property = new RefProperty();
                                            property.set$ref(
                                                    "#/definitions/" + apiResponse.response().getSimpleName());
                                            if (!modelStack.contains(apiResponse.response())) {
                                                modelStack.push(apiResponse.response());
                                            }
                                            ArrayProperty array = new ArrayProperty();
                                            array.setItems(property);
                                            response.setSchema(array);
                                            operation.addResponse(
                                                    String.valueOf(apiResponse.httpStatus().value()), response);
                                        } else {
                                            Response response = new Response();
                                            if (!"".equals(apiResponse.description())) {
                                                response.setDescription(
                                                        apiResponse.httpStatus().getReasonPhrase());
                                            } else {
                                                response.setDescription(apiResponse.description());
                                            }
                                            RefProperty property = new RefProperty();
                                            property.set$ref(
                                                    "#/definitions/" + apiResponse.response().getSimpleName());
                                            if (!modelStack.contains(apiResponse.response())) {
                                                modelStack.push(apiResponse.response());
                                            }
                                            response.setSchema(property);
                                            operation.addResponse(
                                                    String.valueOf(apiResponse.httpStatus().value()), response);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    while (!modelStack.isEmpty()) {
        Class<?> scheme = modelStack.pop();
        if (definitions.containsKey(scheme.getSimpleName())) {
            continue;
        }
        java.lang.reflect.Field[] fields = scheme.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            ModelImpl model = new ModelImpl();
            model.setType("object");
            for (Field field : fields) {
                ApiProperty apiProperty = field.getDeclaredAnnotation(ApiProperty.class);
                if (apiProperty != null) {
                    if (apiProperty.array()) {
                        Class<?> type = apiProperty.model();
                        ArrayProperty property = new ArrayProperty();
                        if (isSimpleScalar(type)) {
                            property.setItems(lookupProperty(type, apiProperty));
                        } else if (isModelScalar(type)) {
                            if (!definitions.containsKey(type.getSimpleName())) {
                                modelStack.push(type);
                            }
                            RefProperty ref = new RefProperty();
                            ref.set$ref("#/definitions/" + type.getSimpleName());
                            property.setItems(ref);
                        }
                        model.addProperty(field.getName(), property);
                    } else {
                        Class<?> type = field.getType();
                        if (isSimpleScalar(type)) {
                            model.addProperty(field.getName(), lookupProperty(type, apiProperty));
                        } else if (isModelScalar(type)) {
                            if (!definitions.containsKey(type.getSimpleName())) {
                                modelStack.push(type);
                            }
                            RefProperty ref = new RefProperty();
                            ref.set$ref("#/definitions/" + type.getSimpleName());
                            model.addProperty(field.getName(), ref);
                        }
                    }
                }
            }
            definitions.put(scheme.getSimpleName(), model);
        }
    }

    this.swagger = swagger;
}

From source file:net.rptools.maptool.client.MapToolLineParser.java

public String parseLine(MapToolVariableResolver res, Token tokenInContext, String line,
        MapToolMacroContext context) throws ParserException {
    if (line == null) {
        return "";
    }/*from   ww  w . j ava  2  s.  c  o  m*/
    line = line.trim();
    if (line.length() == 0) {
        return "";
    }
    Stack<Token> contextTokenStack = new Stack<Token>();
    enterContext(context);
    MapToolVariableResolver resolver = null;
    boolean resolverInitialized = false;
    String opts = null;
    String roll = null;
    try {
        // Keep the same variable context for this line
        resolver = (res == null) ? new MapToolVariableResolver(tokenInContext) : res;
        resolverInitialized = resolver.initialize();
        StringBuilder builder = new StringBuilder();
        int start = 0;
        List<InlineRollMatch> matches = this.locateInlineRolls(line);

        for (InlineRollMatch match : matches) {
            builder.append(line.substring(start, match.getStart())); // add everything before the roll

            start = match.getEnd() + 1;
            // These variables will hold data extracted from the roll options.
            Output output;
            if (MapTool.useToolTipsForUnformatedRolls()) {
                output = Output.TOOLTIP;
            } else {
                output = Output.EXPANDED;
            }
            String text = null; // used by the T option
            HashSet<String> outputOpts = new HashSet<String>();
            OutputLoc outputTo = OutputLoc.CHAT;

            LoopType loopType = LoopType.NO_LOOP;
            int loopStart = 0, loopEnd = 0, loopStep = 1;
            int loopCount = 0;
            String loopSep = null;
            String loopVar = null, loopCondition = null;
            List<String> foreachList = new ArrayList<String>();

            BranchType branchType = BranchType.NO_BRANCH;
            Object branchCondition = null;

            CodeType codeType = CodeType.NO_CODE;
            String macroName = null;

            String frameName = null;
            String frameOpts = null;

            if (match.getMatch().startsWith("[")) {
                opts = match.getOpt();
                roll = match.getRoll();
                if (opts != null) {
                    // Turn the opts string into a list of OptionInfo objects.
                    List<OptionInfo> optionList = null;
                    try {
                        optionList = getRollOptionList(opts);
                    } catch (RollOptionException roe) {
                        throw doError(roe.msg, opts, roll);
                    }

                    // Scan the roll options and prepare variables for later use
                    for (OptionInfo option : optionList) {
                        String error = null;
                        /*
                         * TODO: If you're adding a new option, add a new
                         * case here to collect info from the parameters. If
                         * your option uses parameters, use the
                         * option.getXxxParam() methods to get the text or
                         * parsed values of the parameters.
                         */
                        switch (option.optionType) {

                        ///////////////////////////////////////////////////
                        // OUTPUT FORMAT OPTIONS
                        ///////////////////////////////////////////////////
                        case HIDDEN:
                            output = Output.NONE;
                            break;
                        case RESULT:
                            output = Output.RESULT;
                            break;
                        case EXPANDED:
                            output = Output.EXPANDED;
                            break;
                        case UNFORMATTED:
                            output = Output.UNFORMATTED;
                            outputOpts.add("u");
                            break;
                        case TOOLTIP:
                            // T(display_text)
                            output = Output.TOOLTIP;
                            text = option.getStringParam(0);
                            break;

                        ///////////////////////////////////////////////////
                        // VISIBILITY OPTIONS
                        ///////////////////////////////////////////////////
                        case GM:
                            outputOpts.add("g");
                            break;
                        case SELF:
                            outputOpts.add("s");
                            break;
                        case WHISPER:
                            outputOpts.add("w");
                            for (int i = 0; i < option.getParamCount(); i++) {
                                String arg = parseExpression(resolver, tokenInContext, option.getStringParam(i))
                                        .getValue().toString();
                                if (arg.trim().startsWith("[")) {
                                    Object json = JSONMacroFunctions.convertToJSON(arg);
                                    if (json instanceof JSONArray) {
                                        for (Object name : (JSONArray) json) {
                                            outputOpts.add("w:" + name.toString().toLowerCase());
                                        }
                                    }
                                } else
                                    outputOpts.add("w:" + arg.toLowerCase());
                            }
                            break;

                        ///////////////////////////////////////////////////
                        // TOOLTIP VISIBILITY OPTIONS
                        ///////////////////////////////////////////////////
                        case GMTT:
                            outputOpts.add("gt");
                            break;
                        case SELFTT:
                            outputOpts.add("st");
                            break;

                        ///////////////////////////////////////////////////
                        // LOOP OPTIONS
                        ///////////////////////////////////////////////////
                        case COUNT:
                            // COUNT(num [, sep])
                            loopType = LoopType.COUNT;
                            error = null;
                            try {
                                loopCount = option.getParsedIntParam(0, resolver, tokenInContext);
                                if (loopCount < 0)
                                    error = I18N.getText("lineParser.countNonNeg", loopCount);
                            } catch (ParserException pe) {
                                error = I18N.getText("lineParser.errorProcessingOpt", "COUNT", pe.getMessage());
                            }
                            loopSep = option.getStringParam(1);

                            if (error != null)
                                throw doError(error, opts, roll);
                            break;

                        case FOR:
                            // FOR(var, start, end [, step [, sep]])
                            loopType = LoopType.FOR;
                            error = null;
                            try {
                                loopVar = option.getIdentifierParam(0);
                                loopStart = option.getParsedIntParam(1, resolver, tokenInContext);
                                loopEnd = option.getParsedIntParam(2, resolver, tokenInContext);
                                try {
                                    loopStep = option.getParsedIntParam(3, resolver, tokenInContext);
                                } catch (ParserException pe) {
                                    // Build a more informative error message for this common mistake
                                    String msg = pe.getMessage();
                                    msg = msg + " " + I18N.getText("lineParser.nonDefLoopSep");
                                    throw new ParserException(msg);
                                }
                                loopSep = option.getStringParam(4);
                                if (loopStep != 0)
                                    loopCount = Math.max(1, (int) Math.ceil(
                                            Math.abs((double) (loopEnd - loopStart) / (double) loopStep)));

                                if (loopVar.equalsIgnoreCase(""))
                                    error = I18N.getText("lineParser.forVarMissing");
                                if (loopStep == 0)
                                    error = I18N.getText("lineParser.forNoZeroStep");
                                if ((loopEnd <= loopStart && loopStep > 0)
                                        || (loopEnd >= loopStart && loopStep < 0))
                                    loopCount = 0;
                            } catch (ParserException pe) {
                                error = I18N.getText("lineParser.errorProcessingOpt", "FOR", pe.getMessage());
                            }

                            if (error != null)
                                throw doError(error, opts, roll);
                            break;

                        case FOREACH:
                            // FOREACH(var, list [, outputDelim [, inputDelim]])
                            loopType = LoopType.FOREACH;
                            error = null;
                            try {
                                loopVar = option.getIdentifierParam(0);
                                String listString = option.getParsedParam(1, resolver, tokenInContext)
                                        .toString();
                                loopSep = option.getStringParam(2);
                                String listDelim = option.getStringParam(3);
                                if (listDelim.trim().startsWith("\"")) {
                                    listDelim = parseExpression(resolver, tokenInContext, listDelim).getValue()
                                            .toString();
                                }

                                foreachList = null;
                                if (listString.trim().startsWith("{") || listString.trim().startsWith("[")) {
                                    // if String starts with [ or { it is JSON -- try to treat it as a JSON String
                                    Object obj = JSONMacroFunctions.convertToJSON(listString);
                                    if (obj != null) {
                                        foreachList = new ArrayList<String>();
                                        if (obj instanceof JSONArray) {
                                            for (Object o : ((JSONArray) obj).toArray()) {
                                                foreachList.add(o.toString());
                                            }
                                        } else {
                                            @SuppressWarnings("unchecked")
                                            Set<String> keySet = ((JSONObject) obj).keySet();
                                            foreachList.addAll(keySet);
                                        }
                                    }
                                }

                                // If we still dont have a list treat it list a string list
                                if (foreachList == null) {
                                    foreachList = new ArrayList<String>();
                                    StrListFunctions.parse(listString, foreachList, listDelim);
                                }
                                loopCount = foreachList.size();

                                if (loopVar.equalsIgnoreCase(""))
                                    error = I18N.getText("lineParser.foreachVarMissing");
                            } catch (ParserException pe) {
                                error = I18N.getText("lineParser.errorProcessingOpt", "FOREACH",
                                        pe.getMessage());
                            }

                            if (error != null)
                                throw doError(error, opts, roll);
                            break;

                        case WHILE:
                            // WHILE(cond [, sep])
                            loopType = LoopType.WHILE;
                            loopCondition = option.getStringParam(0);
                            loopSep = option.getStringParam(1);
                            break;

                        ///////////////////////////////////////////////////
                        // BRANCH OPTIONS
                        ///////////////////////////////////////////////////
                        case IF:
                            // IF(condition)
                            branchType = BranchType.IF;
                            branchCondition = option.getStringParam(0);
                            break;
                        case SWITCH:
                            // SWITCH(condition)
                            branchType = BranchType.SWITCH;
                            branchCondition = option.getObjectParam(0);
                            break;

                        ///////////////////////////////////////////////////
                        // DIALOG AND FRAME OPTIONS
                        ///////////////////////////////////////////////////
                        case FRAME:
                            codeType = CodeType.CODEBLOCK;
                            frameName = option.getParsedParam(0, resolver, tokenInContext).toString();
                            frameOpts = option.getParsedParam(1, resolver, tokenInContext).toString();
                            outputTo = OutputLoc.FRAME;
                            break;
                        case DIALOG:
                            codeType = CodeType.CODEBLOCK;
                            frameName = option.getParsedParam(0, resolver, tokenInContext).toString();
                            frameOpts = option.getParsedParam(1, resolver, tokenInContext).toString();
                            outputTo = OutputLoc.DIALOG;
                            break;
                        ///////////////////////////////////////////////////
                        // CODE OPTIONS
                        ///////////////////////////////////////////////////
                        case MACRO:
                            // MACRO("macroName@location")
                            codeType = CodeType.MACRO;
                            macroName = option.getStringParam(0);
                            break;
                        case CODE:
                            codeType = CodeType.CODEBLOCK;
                            break;
                        ///////////////////////////////////////////////////
                        // MISC OPTIONS
                        ///////////////////////////////////////////////////
                        case TOKEN:
                            if (!isMacroTrusted()) {
                                throw new ParserException(I18N.getText("macro.function.roll.noPerm"));
                            }
                            Token newToken = MapTool.getFrame().getCurrentZoneRenderer().getZone().resolveToken(
                                    option.getParsedParam(0, resolver, tokenInContext).toString());
                            if (newToken != null) {
                                contextTokenStack.push(resolver.getTokenInContext());
                                resolver.setTokenIncontext(newToken);
                            }
                            break;
                        default:
                            // should never happen
                            log.error(I18N.getText("lineParser.badOptionFound", opts, roll));
                            throw doError("lineParser.badOptionFound", opts, roll);
                        }
                    }
                }

                // Now that the options have been dealt with, process the body of the roll.
                // We deal with looping first, then branching, then deliver the output.
                StringBuilder expressionBuilder = new StringBuilder();
                int iteration = 0;
                boolean doLoop = true;
                while (doLoop) {
                    int loopConditionValue;
                    Integer branchConditionValue = null;
                    Object branchConditionParsed = null;

                    // Process loop settings
                    if (iteration > maxLoopIterations) {
                        throw doError("lineParser.tooManyLoops", opts, roll);
                    }

                    if (loopType != LoopType.NO_LOOP) {
                        // We only update roll.count in a loop statement.  This allows simple nested 
                        // statements to inherit roll.count from the outer statement.
                        resolver.setVariable("roll.count", iteration);
                    }

                    switch (loopType) {
                    /*
                     * TODO: If you're adding a new looping option, add a
                     * new case to handle the iteration
                     */
                    case NO_LOOP:
                        if (iteration > 0) { // stop after first iteration
                            doLoop = false;
                        }
                        break;
                    case COUNT:
                        if (iteration == loopCount) {
                            doLoop = false;
                        }
                        break;
                    case FOR:
                        if (iteration != loopCount) {
                            resolver.setVariable(loopVar, new BigDecimal(loopStart + loopStep * iteration));
                        } else {
                            doLoop = false;
                            resolver.setVariable(loopVar, null);
                        }
                        break;
                    case FOREACH:
                        if (iteration != loopCount) {
                            String item = foreachList.get(iteration);
                            resolver.setVariable(loopVar, item);
                        } else {
                            doLoop = false;
                            resolver.setVariable(loopVar, null);
                        }
                        break;
                    case WHILE:
                        // This is a hack to get around a bug with the parser's comparison operators.
                        // The InlineTreeFormatter class in the parser chokes on comparison operators, because they're
                        // not listed in the operator precedence table.
                        //
                        // The workaround is that "non-deterministic" functions fully evaluate their arguments,
                        // so the comparison operators are reduced to a number by the time the buggy code is reached.
                        // The if() function defined in dicelib is such a function, so we use it here to eat
                        // any comparison operators.
                        String hackCondition = (loopCondition == null) ? null
                                : String.format("if(%s, 1, 0)", loopCondition);
                        // Stop loop if the while condition is false
                        try {
                            Result result = parseExpression(resolver, tokenInContext, hackCondition);
                            loopConditionValue = ((Number) result.getValue()).intValue();
                            if (loopConditionValue == 0) {
                                doLoop = false;
                            }
                        } catch (Exception e) {
                            throw doError(I18N.getText("lineParser.invalidWhile", loopCondition), opts, roll);
                        }
                        break;
                    }

                    // Output the loop separator
                    if (doLoop && iteration != 0 && output != Output.NONE) {
                        expressionBuilder.append(parseExpression(resolver, tokenInContext, loopSep).getValue());
                    }

                    if (!doLoop) {
                        break;
                    }

                    iteration++;

                    // Extract the appropriate branch to evaluate.

                    // Evaluate the branch condition/expression
                    if (branchCondition != null) {
                        // This is a similar hack to the one used for the loopCondition above.
                        String hackCondition = (branchCondition == null) ? null : branchCondition.toString();
                        if (branchType == BranchType.IF) {
                            hackCondition = (hackCondition == null) ? null
                                    : String.format("if(%s, 1, 0)", hackCondition);
                        }
                        Result result = null;
                        try {
                            result = parseExpression(resolver, tokenInContext, hackCondition);
                        } catch (Exception e) {
                            throw doError(I18N.getText("lineParser.invalidCondition", branchType.toString(),
                                    branchCondition.toString()), opts, roll);
                        }
                        branchConditionParsed = result.getValue();
                        if (branchConditionParsed instanceof Number) {
                            branchConditionValue = ((Number) branchConditionParsed).intValue();
                        }
                    }

                    // Set up regexes for scanning through the branches.
                    // branchRegex then defines one matcher group for the parseable content of the branch.
                    String rollBranch = roll;
                    String branchRegex, branchSepRegex, branchLastSepRegex;
                    if (codeType != CodeType.CODEBLOCK) {
                        // matches any text not containing a ";" (skipping over strings) 
                        String noCodeRegex = "((?:[^\";]|\"[^\"]*\"|'[^']*')*)";
                        branchRegex = noCodeRegex;
                        branchSepRegex = ";";
                        branchLastSepRegex = ";?"; // The last clause doesn't have to end with a separator
                    } else {
                        // matches text inside braces "{...}", skipping over strings (one level of {} nesting allowed)
                        String codeRegex = "\\{((?:[^{}\"]|\"[^\"]*\"|'[^']*'|\\{(?:[^}\"]|\"[^\"]*\"|'[^']*')*})*)}";
                        branchRegex = codeRegex;
                        branchSepRegex = ";";
                        branchLastSepRegex = ";?"; // The last clause doesn't have to end with a separator
                    }

                    // Extract the branch to use
                    switch (branchType) {
                    /*
                     * TODO: If you're adding a new branching option, add a
                     * new case to extract the branch text
                     */
                    case NO_BRANCH: {
                        // There's only one branch, so our regex is very simple
                        String testRegex = String.format("^\\s*%s\\s*$", branchRegex);
                        Matcher testMatcher = Pattern.compile(testRegex).matcher(roll);
                        if (testMatcher.find()) {
                            rollBranch = testMatcher.group(1);
                        } else {
                            throw doError("lineParser.errorBodyRoll", opts, roll);
                        }
                        break;
                    }
                    case IF: {
                        // IF can have one or two branches.
                        // When there's only one branch and the condition is false, there's no output.
                        if (branchConditionValue == null) {
                            throw doError(I18N.getText("lineParser.invalidIfCond", branchCondition,
                                    branchConditionParsed.toString()), opts, roll);
                        }
                        int whichBranch = (branchConditionValue != 0) ? 0 : 1;
                        String testRegex = String.format("^\\s*%s\\s*(?:%s\\s*%s\\s*%s)?\\s*$", branchRegex,
                                branchSepRegex, branchRegex, branchLastSepRegex);
                        Matcher testMatcher = Pattern.compile(testRegex).matcher(roll);
                        if (testMatcher.find()) { // verifies that roll body is well-formed
                            rollBranch = testMatcher.group(1 + whichBranch);
                            if (rollBranch == null)
                                rollBranch = "''"; // quick-and-dirty way to get no output
                            rollBranch = rollBranch.trim();
                        } else {
                            throw doError("lineParser.ifError", opts, roll);
                        }
                        break;
                    }
                    case SWITCH: {
                        // We augment the branch regex to detect the "case xxx:" or "default:" prefixes,
                        // and search for a match.  An error is thrown if no case match is found.

                        // Regex matches 'default', 'case 123:', 'case "123":', 'case "abc":', but not 'case abc:'
                        branchRegex = "(?:case\\s*\"?((?<!\")(?:\\+|-)?[\\d]+(?!\")|(?<=\")[^\"]*(?=\"))\"?|(default))\\s*:\\s*"
                                + branchRegex;
                        String caseTarget = branchConditionParsed.toString();
                        String testRegex = String.format("^(?:\\s*%s\\s*%s\\s*)*\\s*%s\\s*%s\\s*$", branchRegex,
                                branchSepRegex, branchRegex, branchLastSepRegex);
                        Matcher testMatcher = Pattern.compile(testRegex).matcher(roll);
                        if (testMatcher.find()) { // verifies that roll body is well-formed
                            String scanRegex = String.format("\\s*%s\\s*(?:%s)?", branchRegex, branchSepRegex);
                            Matcher scanMatcher = Pattern.compile(scanRegex).matcher(roll);
                            boolean foundMatch = false;
                            while (!foundMatch && scanMatcher.find()) {
                                String caseLabel = scanMatcher.group(1); // "case (xxx):"
                                String def = scanMatcher.group(2); // "(default):"
                                String branch = scanMatcher.group(3);
                                if (def != null) {
                                    rollBranch = branch.trim();
                                    foundMatch = true;
                                    ;
                                }
                                if (caseLabel != null && caseLabel.matches(caseTarget)) {
                                    rollBranch = branch.trim();
                                    foundMatch = true;
                                }
                            }
                            if (!foundMatch) {
                                doError(I18N.getText("lineParser.switchNoMatch", caseTarget), opts, roll);
                            }
                        } else {
                            doError("lineParser.switchError", opts, roll);
                        }

                        break;
                    }
                    } // end of switch(branchType) statement

                    // Construct the output.  
                    // If a MACRO or CODE block is being used, we default to bare output as in the RESULT style.
                    // The output style NONE is also allowed in these cases.
                    Result result;
                    String output_text;
                    switch (codeType) {
                    case NO_CODE:
                        // If none of the code options are active, any of the formatting options can be used.
                        switch (output) {
                        /*
                         * TODO: If you're adding a new formatting option,
                         * add a new case to build the output
                         */
                        case NONE:
                            parseExpression(resolver, tokenInContext, rollBranch);
                            break;
                        case RESULT:
                            result = parseExpression(resolver, tokenInContext, rollBranch);
                            output_text = result != null ? result.getValue().toString() : "";
                            if (!this.isMacroTrusted()) {
                                output_text = output_text.replaceAll(
                                        "\u00AB|\u00BB|&#171;|&#187;|&laquo;|&raquo;|\036|\037", "");
                            }
                            if (outputOpts.isEmpty()) {
                                expressionBuilder.append(output_text);
                            } else {
                                outputOpts.add("r");
                                expressionBuilder.append(rollString(outputOpts, output_text));
                            }

                            break;
                        case TOOLTIP:
                            String tooltip = rollBranch + " = ";
                            output_text = null;
                            result = parseExpression(resolver, tokenInContext, rollBranch);
                            tooltip += result.getDetailExpression();
                            if (text == null) {
                                output_text = result.getValue().toString();
                            } else {
                                if (!result.getDetailExpression().equals(result.getValue().toString())) {
                                    tooltip += " = " + result.getValue();
                                }
                                resolver.setVariable("roll.result", result.getValue());
                                output_text = parseExpression(resolver, tokenInContext, text).getValue()
                                        .toString();
                            }
                            tooltip = tooltip.replaceAll("'", "&#39;");
                            expressionBuilder.append(
                                    output_text != null ? rollString(outputOpts, tooltip, output_text) : "");
                            break;
                        case EXPANDED:
                            expressionBuilder.append(rollString(outputOpts,
                                    rollBranch + " = " + expandRoll(resolver, tokenInContext, rollBranch)));
                            break;
                        case UNFORMATTED:
                            output_text = rollBranch + " = " + expandRoll(resolver, tokenInContext, rollBranch);

                            // Escape quotes so that the result can be used in a title attribute
                            output_text = output_text.replaceAll("'", "&#39;");
                            output_text = output_text.replaceAll("\"", "&#34;");

                            expressionBuilder.append(rollString(outputOpts, output_text));
                        } // end of switch(output) statement
                        break; // end of case NO_CODE in switch(codeType) statement
                    /*
                     * TODO: If you're adding a new code option, add a new
                     * case to execute the code
                     */
                    case MACRO:
                        // [MACRO("macroName@location"): args]
                        result = parseExpression(resolver, tokenInContext, macroName);
                        String callName = result.getValue().toString();
                        result = parseExpression(resolver, tokenInContext, rollBranch);
                        String macroArgs = result.getValue().toString();
                        output_text = runMacro(resolver, tokenInContext, callName, macroArgs);
                        if (output != Output.NONE) {
                            expressionBuilder.append(output_text);
                        }
                        resolver.setVariable("roll.count", iteration); // reset this because called code might change it
                        break;

                    case CODEBLOCK:
                        output_text = runMacroBlock(resolver, tokenInContext, rollBranch);
                        resolver.setVariable("roll.count", iteration); // reset this because called code might change it
                        if (output != Output.NONE) {
                            expressionBuilder.append(output_text);
                        }
                        break;
                    }
                }
                switch (outputTo) {
                case FRAME:
                    HTMLFrameFactory.show(frameName, true, frameOpts, expressionBuilder.toString());
                    break;
                case DIALOG:
                    HTMLFrameFactory.show(frameName, false, frameOpts, expressionBuilder.toString());
                    break;
                case CHAT:
                    builder.append(expressionBuilder);
                    break;
                }

                // Revert to our previous token if [token(): ] was used
                if (contextTokenStack.size() > 0) {
                    resolver.setTokenIncontext(contextTokenStack.pop());
                }
            } else if (match.getMatch().startsWith("{")) {
                roll = match.getRoll();
                Result result = parseExpression(resolver, tokenInContext, roll);
                if (isMacroTrusted()) {
                    builder.append(result != null ? result.getValue().toString() : "");
                } else {
                    builder.append(result != null ? result.getValue().toString()
                            .replaceAll("\u00AB|\u00BB|&#171;|&#187;|&laquo;|&raquo;|\036|\037", "") : "");
                }
            }
        }
        builder.append(line.substring(start));
        return builder.toString();
    } catch (AbortFunctionException e) {
        // do nothing; this exception will never generate any output
        // throw doError("macroExecutionAbort", opts == null ? "" : opts, roll == null ? line : roll);
        throw e;
    } catch (AssertFunctionException e) {
        // do nothing; this exception will never generate any output
        // throw doError("macroExecutionAssert", opts == null ? "" : opts, roll == null ? line : roll);
        throw e;
    } catch (ParserException e) {
        // do nothing, jut pass message back up
        throw e;
    } catch (Exception e) {
        log.info(line, e);
        throw doError("lineParser.errorBodyRoll", opts == null ? "" : opts, roll == null ? line : roll);
    } finally {
        exitContext();
        if (resolverInitialized) {
            // This is the top level call, time to clean up
            resolver.flush();
        }
        // If we have exited the last context let the html frame we have (potentially)
        // updated a token.
        if (contextStackEmpty()) {
            HTMLFrameFactory.tokenChanged(tokenInContext);
        }
        MapTool.getFrame().refresh(); // Repaint incase macros changed anything. 
    }
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

/**
 * Get the workItems list and validate if all the fields of the excel sheet
 * are correct//from  w  w  w .  ja v  a2  s  .co  m
 * 
 * @param workbook
 * @param selectedSheet
 * @param personID
 * @param locale
 * @param columnIndexToFieldIDMap
 * @param fieldIDToColumnIndexMap
 * @param lastSavedIdentifierFieldIDIsSet
 * @param defaultValuesMap
 * @param invalidValueHandlingMap
 * @param gridErrorsMap
 * @param rowErrorsMap
 * @return
 */
static SortedMap<Integer, TWorkItemBean> getAndValidateGridData(Workbook workbook, Integer selectedSheet,
        Integer personID, Locale locale, Map<Integer, Integer> columnIndexToFieldIDMap,
        Map<Integer, Integer> fieldIDToColumnIndexMap, Set<Integer> lastSavedIdentifierFieldIDIsSet,
        Map<Integer, Integer> defaultValuesMap, Map<Integer, Integer> invalidValueHandlingMap,
        Map<Integer, Map<Integer, List<Integer>>> rowNoToPseudoFieldsOriginal,
        Map<Integer, Map<Integer, List<Integer>>> rowNoToPseudoFieldsExcel,
        Map<Integer, SortedMap<Integer, SortedMap<String, ErrorData>>> gridErrorsMap,
        Map<Integer, SortedSet<Integer>> rowErrorsMap, Map<Integer, SortedSet<Integer>> requiredFieldErrorsMap,
        Map<Integer, Integer> rowToParentRow) {
    SortedMap<Integer, TWorkItemBean> workItemBeansMap = new TreeMap<Integer, TWorkItemBean>();
    Sheet sheet = workbook.getSheetAt(selectedSheet.intValue());
    // get the column indexes for project and issueType
    Integer projectColumn = fieldIDToColumnIndexMap.get(SystemFields.INTEGER_PROJECT);
    Integer issueTypeColumn = fieldIDToColumnIndexMap.get(SystemFields.INTEGER_ISSUETYPE);
    // Maps to spare additional database accesses for default values
    Map<Integer, String> defaultShowValuesMap = new HashMap<Integer, String>();
    Map<Integer, String> defaultLocalizedFieldLabels = new HashMap<Integer, String>();
    Integer originalProject = null;
    Integer originalIssueType = null;
    Set<Integer> mandatoryIdentifierFields = ExcelFieldMatchBL.getMandatoryIdentifierFields();
    Map<Integer, Map<String, ILabelBean>> systemLookups = loadBaseLookups(personID, locale);
    Map<String, ILabelBean> projectLookups = systemLookups.get(SystemFields.INTEGER_PROJECT);
    Map<Integer, Map<Integer, Map<String, ILabelBean>>> projectSpecificLookups = null;
    if (projectLookups != null) {
        projectSpecificLookups = loadProjectLookups(GeneralUtils
                .createIntegerListFromBeanList(GeneralUtils.createListFromCollection(projectLookups.values())));
    }
    boolean projectSpecificIDsActive = ApplicationBean.getInstance().getSiteBean().getProjectSpecificIDsOn();
    Map<Integer, TProjectBean> projectBeansMap = new HashMap<Integer, TProjectBean>();
    if (projectSpecificIDsActive) {
        List<TProjectBean> projectBeans = ProjectBL.loadUsedProjectsFlat(personID);
        if (projectBeans != null) {
            for (TProjectBean projectBean : projectBeans) {
                Integer projectID = projectBean.getObjectID();
                projectBeansMap.put(projectID, projectBean);
                String label = projectBean.getLabel();
                String projectPrefix = projectBean.getPrefix();
                if (projectPrefix == null || "".equals(projectPrefix)) {
                    LOGGER.info("The project " + label + " with ID " + projectID
                            + " has no prefix, consquently project specific item numbers might not be recognized");
                }
            }
        }
    }
    /**
     * Process the rows only to gather the projects to issueTypes to get the
     * roles and restrictions once for all issues
     */
    Map<Integer, Set<Integer>> projectToIssueTypesMap = new HashMap<Integer, Set<Integer>>();
    for (Row row : sheet) {
        int rowNum = row.getRowNum();
        if (rowNum == 0) {
            // only the data rows are processed (the header row is not
            // important now)
            continue;
        }
        SerializableBeanAllowedContext serializableBeanAllowedContext = new SerializableBeanAllowedContext();
        serializableBeanAllowedContext.setPersonID(personID);
        serializableBeanAllowedContext.setNew(true);
        // get the project and issueType first because the other fields
        // could depend on these issueTypes
        // process the project column
        Integer projectID = null;
        if (projectColumn != null) {
            try {
                projectID = (Integer) getAttributeValue(row.getCell(projectColumn),
                        SystemFields.INTEGER_PROJECT, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
            } catch (Exception e) {
            }
        }
        if (projectID == null) {
            // no project column exists on the sheet: take the default value
            // which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            projectID = defaultValuesMap.get(SystemFields.INTEGER_PROJECT);
        }
        if (projectID != null) {
            serializableBeanAllowedContext.setProjectID(projectID);
        }
        // process the issueType column
        Integer issueTypeID = null;
        if (issueTypeColumn != null) {
            try {
                issueTypeID = (Integer) getAttributeValue(row.getCell(issueTypeColumn),
                        SystemFields.INTEGER_ISSUETYPE, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
            } catch (Exception e) {
            }
        }
        if (issueTypeID == null) {
            // no issue type column exists on the sheet: take the default
            // value which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            issueTypeID = defaultValuesMap.get(SystemFields.INTEGER_ISSUETYPE);
        }
        if (projectID != null) {
            Set<Integer> issueTypes = projectToIssueTypesMap.get(projectID);
            if (issueTypes == null) {
                issueTypes = new HashSet<Integer>();
                projectToIssueTypesMap.put(projectID, issueTypes);
            }
            if (issueTypeID != null) {
                issueTypes.add(issueTypeID);
            }
        }
    }
    Map<Integer, Map<Integer, Map<Integer, TFieldConfigBean>>> projectsToIssueTypesToFieldConfigsMapForBottomUpFields = null;
    Map<Integer, Map<Integer, Map<String, Object>>> projectsIssueTypesFieldSettingsMapForBottomUpFields = null;
    Map<Integer, Map<Integer, Map<Integer, Integer>>> fieldRestrictions = AccessBeans
            .getFieldRestrictions(personID, projectToIssueTypesMap, null, true);
    Set<Integer> possibleBottomUpFields = FieldRuntimeBL.getPossibleBottomUpFields();
    for (Iterator<Integer> iterator = possibleBottomUpFields.iterator(); iterator.hasNext();) {
        if (!fieldIDToColumnIndexMap.containsKey(iterator.next())) {
            // remove possible bottom up field if not mapped
            iterator.remove();
        }
        if (!possibleBottomUpFields.isEmpty()) {
            // at least one bottom up date was mapped
            projectsToIssueTypesToFieldConfigsMapForBottomUpFields = FieldRuntimeBL
                    .loadFieldConfigsInContextsAndTargetProjectAndIssueType(projectToIssueTypesMap,
                            possibleBottomUpFields, locale, null, null);
            projectsIssueTypesFieldSettingsMapForBottomUpFields = FieldRuntimeBL
                    .getFieldSettingsForFieldConfigs(projectsToIssueTypesToFieldConfigsMapForBottomUpFields);
        }
    }

    /**
     * now process the rows in detail one by one
     */
    Stack<Integer> parentStack = new Stack<Integer>();
    Map<Integer, Integer> rowToIndent = new HashMap<Integer, Integer>();
    for (Row row : sheet) {
        int rowNum = row.getRowNum();
        if (rowNum == 0) {
            // only the data rows are processed (the header row is not
            // important now)
            continue;
        }
        boolean excelValueFound = false;
        // whether the project column is mapped and excel value if found for
        // project
        boolean mappedProject = false;
        SerializableBeanAllowedContext serializableBeanAllowedContext = new SerializableBeanAllowedContext();
        serializableBeanAllowedContext.setPersonID(personID);
        serializableBeanAllowedContext.setNew(true);
        // get the project and issueType first because the other fields
        // could depend on these issueTypes
        // process the project column
        Integer projectID = null;
        if (projectColumn != null) {
            try {
                projectID = (Integer) getAttributeValue(row.getCell(projectColumn),
                        SystemFields.INTEGER_PROJECT, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                if (projectID != null) {
                    mappedProject = true;
                    excelValueFound = true;
                }
            } catch (ExcelImportNotExistingCellValueException e) {
                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            } catch (ExcelImportNotAllowedCellValueException e) {
                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            } catch (ExcelImportInvalidCellValueException e) {
                addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            }
        }
        if (projectID == null) {
            // no project column exists on the sheet: take the default value
            // which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            projectID = defaultValuesMap.get(SystemFields.INTEGER_PROJECT);
        }
        if (projectID != null) {
            serializableBeanAllowedContext.setProjectID(projectID);
        }
        // process the issueType column
        Integer issueTypeID = null;
        if (issueTypeColumn != null) {
            try {
                issueTypeID = (Integer) getAttributeValue(row.getCell(issueTypeColumn),
                        SystemFields.INTEGER_ISSUETYPE, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                if (issueTypeID != null) {
                    excelValueFound = true;
                }
            } catch (ExcelImportNotExistingCellValueException e) {
                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            } catch (ExcelImportNotAllowedCellValueException e) {
                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            } catch (ExcelImportInvalidCellValueException e) {
                addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            }
        }
        if (issueTypeID == null) {
            // no issue type column exists on the sheet: take the default
            // value which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            issueTypeID = defaultValuesMap.get(SystemFields.INTEGER_ISSUETYPE);
        }
        if (issueTypeID != null) {
            serializableBeanAllowedContext.setIssueTypeID(issueTypeID);
        }
        /*
         * gather the values for the identifier fields and try to get an
         * existing workItem by these fields
         */
        Map<Integer, Object> identifierFieldValues = new HashMap<Integer, Object>();
        if (lastSavedIdentifierFieldIDIsSet != null && !lastSavedIdentifierFieldIDIsSet.isEmpty()) {
            for (Integer fieldID : lastSavedIdentifierFieldIDIsSet) {
                Integer attributeFieldID = fieldID;
                if (SystemFields.INTEGER_ISSUENO.equals(fieldID) && projectSpecificIDsActive) {
                    attributeFieldID = SystemFields.INTEGER_PROJECT_SPECIFIC_ISSUENO;
                }
                Object attributeValue = null;
                Integer columnIndex = null;
                try {
                    columnIndex = fieldIDToColumnIndexMap.get(fieldID);
                    attributeValue = getAttributeValue(row.getCell(columnIndex), attributeFieldID, null,
                            serializableBeanAllowedContext, locale, invalidValueHandlingMap, systemLookups,
                            projectSpecificLookups);
                    if (attributeValue != null) {
                        identifierFieldValues.put(fieldID, attributeValue);
                        excelValueFound = true;
                    }
                } catch (ExcelImportNotExistingCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                } catch (ExcelImportNotAllowedCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                } catch (ExcelImportInvalidCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                }
            }
        }
        // always initialize the next workItem to null
        TWorkItemBean workItemBean = null;
        boolean itemIsNew = false;
        if (!identifierFieldValues.isEmpty()) {
            if (identifierFieldValues.get(SystemFields.INTEGER_ISSUENO) != null) {
                // is issueNo field mapped?
                if (projectSpecificIDsActive) {
                    // get by project specific itemID
                    String projectSpecificID = null;
                    try {
                        projectSpecificID = (String) identifierFieldValues.get(SystemFields.INTEGER_ISSUENO);
                    } catch (Exception e) {
                    }
                    if (projectSpecificID != null) {
                        // it should be trimmed because in excel the child
                        // issues are indented
                        workItemBean = ItemBL.loadWorkItemByProjectSpecificID(projectID, mappedProject,
                                projectBeansMap, projectSpecificID.trim());
                        if (workItemBean != null && LOGGER.isDebugEnabled()) {
                            LOGGER.debug("WorkItem " + projectSpecificID + " from row " + rowNum
                                    + " found by projectSpecificID");
                        }
                    }
                } else {
                    // get by "global" workItemID
                    Integer workItemID = null;
                    try {
                        workItemID = (Integer) identifierFieldValues.get(SystemFields.INTEGER_ISSUENO);
                    } catch (Exception e) {
                    }
                    if (workItemID != null) {
                        workItemBean = ItemBL.loadWorkItemSystemAttributes(workItemID);
                    }
                    if (workItemBean != null && LOGGER.isDebugEnabled()) {
                        LOGGER.debug("WorkItem " + workItemID + " from row " + rowNum + " found by workItemID");
                    }
                }
                if (workItemBean == null) {
                    // the issueNo field is set as identifier and the
                    // corresponding issue does't exist, report as error
                    addGridError(gridErrorsMap, WORKITEM_NOTEXIST_ERRORS, rowNum,
                            ExcelFieldMatchBL.colNumericToLetter(
                                    fieldIDToColumnIndexMap.get(SystemFields.INTEGER_ISSUENO)),
                            SystemFields.INTEGER_ISSUENO,
                            identifierFieldValues.get(SystemFields.INTEGER_ISSUENO).toString());
                    continue;
                }
            }
            if (workItemBean == null) {
                // workItem was not found by issueNo
                // (issueNo field was not mapped or issueNo value is missing
                // from excel or
                // the issue's project is not accessible if
                // projectSpecificIDsActive)
                // try with user defined identifier fields
                try {
                    workItemBean = ItemBL.loadWorkItemSystemAttributes(identifierFieldValues);
                } catch (ExcelImportNotUniqueIdentifiersException e) {
                    addRowError(rowErrorsMap, WORKITEM_MORE_THAN_ONE_EXIST, rowNum);
                    continue;
                }
                if (workItemBean != null && LOGGER.isDebugEnabled()) {
                    LOGGER.debug("WorkItem from row " + rowNum + " found by user defined identifier fields");
                }
            }
            if (workItemBean != null) {
                // existing workItem
                originalProject = workItemBean.getProjectID();
                originalIssueType = workItemBean.getListTypeID();
                // is it editable by the current person?
                if (!AccessBeans.isAllowedToChange(workItemBean, personID)) {
                    addRowError(rowErrorsMap, WORKITEM_NO_EDIT_RIGHT, rowNum);
                    continue;
                }
                // load also the custom attributes because when the workItem
                // will be updated
                // the custom attributes will also be compared to the
                // original value
                ItemBL.loadWorkItemCustomAttributes(workItemBean);
                serializableBeanAllowedContext.setWorkItemBeanOriginal(workItemBean);
                serializableBeanAllowedContext.setNew(false);
                // LOGGER.debug("WorkItem " + workItemBean.getObjectID() +
                // " from row " + rowNum + " found");
            }
        }
        boolean missingRequiredFound = false;
        if (workItemBean == null) {
            // not existing found by identifier fields, create a new one
            workItemBean = new TWorkItemBean();
            if (identifierFieldValues != null) {
                // preset the new workItem with the processed identifier
                // values
                for (Map.Entry<Integer, Object> identifierEntry : identifierFieldValues.entrySet()) {
                    workItemBean.setAttribute(identifierEntry.getKey(), identifierEntry.getValue());
                }
            }
            itemIsNew = true;
            LOGGER.debug("WorkItem from row " + rowNum + " not found. A new one will be created.");
        }
        if (projectID != null) {
            workItemBean.setAttribute(SystemFields.INTEGER_PROJECT, null, projectID);
        } else {
            if (itemIsNew) {
                // project column not mapped
                addRowError(requiredFieldErrorsMap, SystemFields.INTEGER_PROJECT, rowNum);
                missingRequiredFound = true;
            }
        }
        if (issueTypeID != null) {
            workItemBean.setAttribute(SystemFields.INTEGER_ISSUETYPE, null, issueTypeID);
        } else {
            if (itemIsNew) {
                // project column not mapped
                addRowError(requiredFieldErrorsMap, SystemFields.INTEGER_ISSUETYPE, rowNum);
                missingRequiredFound = true;
            }
        }
        if (missingRequiredFound) {
            continue;
        }
        Map<Integer, Integer> restrictedFields = null;
        projectID = workItemBean.getProjectID();
        issueTypeID = workItemBean.getListTypeID();
        if (projectID != null && issueTypeID != null) {
            Map<Integer, Map<Integer, Integer>> issueTypeRestrictions = fieldRestrictions.get(projectID);
            if (issueTypeRestrictions != null) {
                restrictedFields = issueTypeRestrictions.get(issueTypeID);
            }
            if (restrictedFields == null) {
                // no project or issue type mapped get the restriction now
                restrictedFields = AccessBeans.getFieldRestrictions(personID, projectID, issueTypeID, true);
                issueTypeRestrictions = new HashMap<Integer, Map<Integer, Integer>>();
                issueTypeRestrictions.put(issueTypeID, restrictedFields);
                fieldRestrictions.put(projectID, issueTypeRestrictions);
            }
            // new values exist
            if (originalProject != null && originalIssueType != null) {
                // workItem existed
                if (!projectID.equals(originalProject) || !issueTypeID.equals(originalIssueType)) {
                    if (!AccessBeans.isAllowedToChange(workItemBean, personID)) {
                        // move not allowed
                        addRowError(rowErrorsMap, WORKITEM_NO_EDIT_RIGHT, rowNum);
                        continue;
                    }
                }
            } else {
                // new workItem
                if (!AccessBeans.isAllowedToCreate(personID, projectID, issueTypeID)) {
                    // create not allowed
                    addRowError(rowErrorsMap, WORKITEM_NO_CREATE_RIGHT, rowNum);
                    continue;
                }
            }
        }
        // process the remaining cells
        Map<Integer, Integer> rowNoToIndentLevel = new HashMap<Integer, Integer>();
        for (Cell cell : row) {
            boolean attributeChanged = false;
            int columnIndex = cell.getColumnIndex();
            Integer fieldID = columnIndexToFieldIDMap.get(columnIndex);
            Integer fieldForRestriction = fieldID;
            if (fieldID == null) {
                // LOGGER.debug("No mapping found for column " +
                // columnIndex);
                continue;
            }
            if (fieldID.equals(SystemFields.INTEGER_PROJECT) || fieldID.equals(SystemFields.INTEGER_ISSUETYPE)
                    || identifierFieldValues.containsKey(fieldID)
                    || mandatoryIdentifierFields.contains(fieldID)) {
                // these values are already read
                continue;
            }
            if (fieldID.intValue() < 0) {
                // pseudo field: now only watchers
                if (fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.INFORMANT_LIST
                        || fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.CONSULTANT_LIST) {
                    fieldForRestriction = FieldsRestrictionsToRoleBL.PSEUDO_COLUMNS.WATCHERS;
                    String watcherValue = getStringCellValue(cell);
                    if (watcherValue == null || "".equals(watcherValue.trim())) {
                        continue;
                    }
                    Map<Integer, List<Integer>> watcherMapOriginal = rowNoToPseudoFieldsOriginal.get(rowNum);
                    if (watcherMapOriginal == null) {
                        watcherMapOriginal = new HashMap<Integer, List<Integer>>();
                        rowNoToPseudoFieldsOriginal.put(rowNum, watcherMapOriginal);
                    }
                    List<Integer> watcherListOriginal = null;
                    TWorkItemBean workItemBeanOriginal = serializableBeanAllowedContext
                            .getWorkItemBeanOriginal();
                    if (workItemBeanOriginal != null) {
                        if (fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.INFORMANT_LIST) {
                            watcherListOriginal = GeneralUtils.createIntegerListFromBeanList(
                                    PersonBL.getDirectInformants(workItemBeanOriginal.getObjectID()));
                        } else {
                            watcherListOriginal = GeneralUtils.createIntegerListFromBeanList(
                                    PersonBL.getDirectConsultants(workItemBeanOriginal.getObjectID()));
                        }
                        watcherMapOriginal.put(fieldID, watcherListOriginal);
                    }
                    List<Integer> watcherListExcel = new LinkedList<Integer>();
                    String[] watcherNames = watcherValue
                            .split("\\" + ConsultedInformedLoaderBL.WATCHER_SPLITTER_VALUES_STRING);
                    if (watcherNames != null) {
                        Map<Integer, List<Integer>> watcherMapExcel = rowNoToPseudoFieldsExcel.get(rowNum);
                        if (watcherMapExcel == null) {
                            watcherMapExcel = new HashMap<Integer, List<Integer>>();
                            rowNoToPseudoFieldsExcel.put(rowNum, watcherMapExcel);
                        }
                        watcherMapExcel.put(fieldID, watcherListExcel);
                        for (int i = 0; i < watcherNames.length; i++) {
                            String watcherName = watcherNames[i];
                            Integer objectID = null;
                            try {
                                objectID = getWatcherValue(watcherName, fieldID, systemLookups,
                                        watcherListOriginal, serializableBeanAllowedContext, locale);
                            } catch (ExcelImportNotExistingCellValueException e) {
                                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            } catch (ExcelImportNotAllowedCellValueException e) {
                                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            }
                            if (objectID != null) {
                                watcherListExcel.add(objectID);
                                excelValueFound = true;
                            }
                        }
                    }
                    attributeChanged = ConsInfBL.watcherChanged(watcherListOriginal, watcherListExcel);
                } else {
                    if (fieldID.intValue() == ExcelFieldMatchBL.LOCAL_PARENT_PSEUDO_COLUMN) {
                        // local parent - child hierarchy (for new items)
                        Integer pseudoHierarchyColumn = fieldIDToColumnIndexMap
                                .get(ExcelFieldMatchBL.LOCAL_PARENT_PSEUDO_COLUMN);
                        if (pseudoHierarchyColumn != null) {
                            String hierarchyColumn = getStringCellValue(row.getCell(pseudoHierarchyColumn));
                            if (hierarchyColumn != null && hierarchyColumn.length() > 0) {
                                int previousIndent = 0;
                                if (!parentStack.isEmpty()) {
                                    Integer previousRow = parentStack.peek();
                                    if (rowToIndent.get(previousRow) != null) {
                                        previousIndent = rowToIndent.get(previousRow).intValue();
                                    }
                                }
                                int actualIndent = hierarchyColumn.length();
                                rowToIndent.put(rowNum, actualIndent);
                                rowNoToIndentLevel.put(rowNum, actualIndent);
                                if (previousIndent == actualIndent) {
                                    // sibling: same parent as the sibling's
                                    // parent
                                    if (!parentStack.isEmpty()) {
                                        // remove the sibling from stack
                                        parentStack.pop();
                                        if (!parentStack.isEmpty()) {
                                            // if the stack is still not
                                            // empty then the peek is teh
                                            // parent
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    }
                                } else {
                                    if (actualIndent > previousIndent) {
                                        // child of the previous row
                                        if (actualIndent - previousIndent > 1) {
                                            // jump more than one in deep is
                                            // error
                                            addGridError(gridErrorsMap, INCONSISTENT_HIERARCHY_ERRORS, rowNum,
                                                    ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                    hierarchyColumn);
                                        }
                                        if (!parentStack.isEmpty()) {
                                            // add previous row as parent
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    } else {
                                        // new hierarchy: nothing to do with
                                        // the previous row
                                        int difference = previousIndent - actualIndent;
                                        for (int i = 0; i <= difference; i++) {
                                            // pop to find the parent
                                            if (!parentStack.isEmpty()) {
                                                parentStack.pop();
                                            }
                                        }
                                        if (!parentStack.isEmpty()) {
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    }
                                }
                            } else {
                                // no hierarchy string: top level item
                                while (!parentStack.isEmpty()) {
                                    // empty the stack
                                    parentStack.pop();
                                }
                            }
                            // add row to stack for possible children
                            parentStack.push(rowNum);
                        }
                    }
                }
            } else {
                IFieldTypeRT fieldTypeRT = FieldTypeManager.getFieldTypeRT(fieldID);
                Object attributeValue = null;
                if (fieldTypeRT.isComposite() || fieldTypeRT.isMultipleValues()) {
                    String compositeOrMultipleValue = getStringCellValue(cell);
                    if (compositeOrMultipleValue == null || "".equals(compositeOrMultipleValue.trim())) {
                        workItemBean.setAttribute(fieldID, null, null);
                        continue;
                    }
                    // we suppose that all composite and multiple values are
                    // lookup values
                    // TODO refactor if that is not true
                    String[] parts;
                    if (fieldTypeRT.isMultipleValues()) {
                        parts = compositeOrMultipleValue
                                .split(CustomSelectBaseRT.OPTION_SPLITTER_VALUES_STRING);
                        List<Integer> multipleValues = new ArrayList<Integer>();
                        for (int i = 0; i < parts.length; i++) {
                            String part = parts[i];
                            Integer objectID = null;
                            try {
                                objectID = getLookupValue(part, fieldTypeRT, fieldID, systemLookups,
                                        projectSpecificLookups, serializableBeanAllowedContext, null,
                                        invalidValueHandlingMap, locale);
                            } catch (ExcelImportNotExistingCellValueException e) {
                                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            } catch (ExcelImportNotAllowedCellValueException e) {
                                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            }
                            if (objectID != null) {
                                multipleValues.add(objectID);
                            }
                        }
                        if (!multipleValues.isEmpty()) {
                            attributeValue = multipleValues.toArray();
                            excelValueFound = true;
                        }
                    } else {
                        int numberOfParts = ((CustomCompositeBaseRT) fieldTypeRT).getNumberOfParts();
                        parts = compositeOrMultipleValue
                                .split("\\" + CustomCompositeBaseRT.PART_SPLITTER_VALUES_STRING);
                        if (parts != null && parts.length > numberOfParts) {
                            addGridError(gridErrorsMap, WRONG_COMPOSITE_SIZE, rowNum,
                                    ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                    compositeOrMultipleValue);
                        }
                        Map<Integer, Integer> componentPartsMap = new HashMap<Integer, Integer>();
                        attributeValue = new HashMap<Integer, Object>();
                        if (parts != null) {
                            for (int i = 0; i < parts.length; i++) {
                                String part = parts[i];
                                Integer objectID = null;
                                IFieldTypeRT componentFieldType = ((CustomCompositeBaseRT) fieldTypeRT)
                                        .getCustomFieldType(i + 1);
                                if (componentFieldType != null) {
                                    try {
                                        objectID = getLookupValue(part, componentFieldType, fieldID,
                                                systemLookups, projectSpecificLookups,
                                                serializableBeanAllowedContext, componentPartsMap,
                                                invalidValueHandlingMap, locale);
                                    } catch (ExcelImportNotExistingCellValueException e) {
                                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                e.getMessage());
                                    } catch (ExcelImportNotAllowedCellValueException e) {
                                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                e.getMessage());
                                    }
                                    if (objectID == null) {
                                        // workItemBean.setAttribute(fieldID,
                                        // Integer.valueOf(i+1), null);
                                        ((Map<Integer, Object>) attributeValue).put(Integer.valueOf(i + 1),
                                                null);
                                    } else {
                                        componentPartsMap.put(Integer.valueOf(i + 1), objectID);
                                        // workItemBean.setAttribute(fieldID,
                                        // Integer.valueOf(i+1), new
                                        // Object[] {objectID});
                                        ((Map<Integer, Object>) attributeValue).put(Integer.valueOf(i + 1),
                                                new Object[] { objectID });
                                        excelValueFound = true;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    // simple field
                    // Object attributeValue = null;
                    try {
                        attributeValue = getAttributeValue(cell, fieldID, null, serializableBeanAllowedContext,
                                locale, invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                    } catch (ExcelImportNotExistingCellValueException e) {
                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    } catch (ExcelImportNotAllowedCellValueException e) {
                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    } catch (ExcelImportInvalidCellValueException e) {
                        addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                    if (attributeValue != null) {
                        excelValueFound = true;
                        if (possibleBottomUpFields.contains(fieldID)) {
                            TFieldConfigBean fieldConfigBean = FieldRuntimeBL
                                    .getFieldConfigForProjectIssueTypeField(
                                            projectsToIssueTypesToFieldConfigsMapForBottomUpFields, projectID,
                                            issueTypeID, fieldID);
                            Object fieldSettings = FieldRuntimeBL.getFieldSettingsForProjectIssueTypeField(
                                    projectsIssueTypesFieldSettingsMapForBottomUpFields, projectID, issueTypeID,
                                    fieldID);
                            if (fieldTypeRT.getHierarchicalBehavior(fieldID, fieldConfigBean,
                                    fieldSettings) == HIERARCHICAL_BEHAVIOR_OPTIONS.COMPUTE_BOTTOM_UP
                                    && ItemBL.hasChildren(workItemBean.getObjectID())) {
                                Date trackPlusAttributeValue = (Date) workItemBean.getAttribute(fieldID);
                                if (EqualUtils.notEqual(trackPlusAttributeValue, (Date) attributeValue)) {
                                    // add read only restrictions for start
                                    // and end date for non leaf workItems
                                    LOGGER.debug("Parent change restriction for bottom up date " + fieldID);
                                    addGridError(gridErrorsMap, NOT_EDITABLE_ERRORS, rowNum,
                                            ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                            getStringCellValue(cell));
                                }
                            }
                            /*
                             * if
                             * (ApplicationBean.getInstance().getSiteBean
                             * ().getSummaryItemsBehavior() &&
                             * ItemBL2.hasChildren
                             * (workItemBean.getObjectID())) { Date
                             * trackPlusAttributeValue =
                             * (Date)workItemBean.getAttribute(fieldID); if
                             * (EqualUtils.notEqual(trackPlusAttributeValue,
                             * (Date)attributeValue)) { //add read only
                             * restrictions for start and end date for non
                             * leaf workItems LOGGER.debug(
                             * "Summary parent change restriction for date "
                             * + fieldID); addGridError(gridErrorsMap,
                             * NOT_EDITABLE_ERRORS, rowNum,
                             * ExcelFieldMatchBL
                             * .colNumericToLetter(columnIndex), fieldID,
                             * getStringCellValue(cell)); } }
                             */
                        }
                    }
                }
                attributeChanged = fieldTypeRT.valueModified(attributeValue,
                        workItemBean.getAttribute(fieldID));
                workItemBean.setAttribute(fieldID, null, attributeValue);
            }
            if (attributeChanged) {
                try {
                    verifyFieldRestrictions(fieldForRestriction, restrictedFields, cell, locale);
                } catch (ExcelImportNotModifiableCellValueException e) {
                    addGridError(gridErrorsMap, NOT_EDITABLE_ERRORS, rowNum,
                            ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                }
            }
        }
        if (!excelValueFound) {
            // not a single excel value found in any cell from the row
            // simply neglect this row.
            // expanded row count can be greater than the number of real
            // workItem rows
            // for example when the content of some rows is deleted but the
            // rows are not deleted
            // and empty rows may remain in the excel
            LOGGER.info("The row number " + (rowNum + 1) + " contains only empty cells and will be neglected");
            continue;
        }
        // add the default values for those fields which didn't have column
        // in
        // excel sheet or have column but the value is empty or not valid
        Iterator<Integer> itrDefaultValueFields = defaultValuesMap.keySet().iterator();
        while (itrDefaultValueFields.hasNext()) {
            Integer fieldID = itrDefaultValueFields.next();
            if (/*!fieldIDToColumnIndexMap.containsKey(fieldID) ||*/workItemBean.getAttribute(fieldID,
                    null) == null) {
                if (invalidValueHandlingMap.containsKey(fieldID)) {
                    if (DEFAULT_IF_NOT_EXIST_OR_EMPTY.equals(invalidValueHandlingMap.get(fieldID))) {
                        IFieldTypeRT fieldTypeRT = FieldTypeManager.getFieldTypeRT(fieldID, null);
                        ILookup lookup = (ILookup) fieldTypeRT;
                        Integer defaultObjectID = defaultValuesMap.get(fieldID);
                        if (defaultObjectID != null) {
                            boolean allowed = lookup.lookupBeanAllowed(defaultObjectID,
                                    serializableBeanAllowedContext);
                            if (allowed) {
                                workItemBean.setAttribute(fieldID, null, defaultObjectID);
                            } else {
                                // for example when no default project
                                // and/or issue type is specified the
                                // default manager and responsible
                                // lists contain the users which are manager
                                // or responsible in any of the projects
                                // (but maybe not in all)
                                LOGGER.debug("The default value is not allowed for field " + fieldID
                                        + " on row " + rowNum);
                                // cache the show values and localized
                                // labels to spare additional database
                                // accesses
                                String showValue;
                                if (defaultShowValuesMap.containsKey(fieldID)) {
                                    showValue = defaultShowValuesMap.get(fieldID);
                                } else {
                                    showValue = fieldTypeRT.getShowValue(defaultObjectID, locale);
                                    defaultShowValuesMap.put(fieldID, showValue);
                                }
                                String localizedLabel;
                                if (defaultLocalizedFieldLabels.containsKey(fieldID)) {
                                    localizedLabel = defaultLocalizedFieldLabels.get(fieldID);
                                } else {
                                    localizedLabel = FieldRuntimeBL.getLocalizedDefaultFieldLabel(fieldID,
                                            locale);
                                    defaultLocalizedFieldLabels.put(fieldID, localizedLabel);
                                }
                                addGridError(gridErrorsMap, NOT_ALLOWED_DEFAULT_VALUES_ERRORS, rowNum,
                                        localizedLabel, fieldID, showValue);
                            }
                        }
                    }
                }
            }
        }
        workItemBeansMap.put(rowNum, workItemBean);
    }
    return workItemBeansMap;
}

From source file:com.clark.func.Functions.java

/**
 * Checks a filename to see if it matches the specified wildcard matcher
 * allowing control over case-sensitivity.
 * <p>//from   w  ww.  j a  v  a  2 s . c o m
 * The wildcard matcher uses the characters '?' and '*' to represent a
 * single or multiple (zero or more) wildcard characters. N.B. the sequence
 * "*?" does not work properly at present in match strings.
 * 
 * @param filename
 *            the filename to match on
 * @param wildcardMatcher
 *            the wildcard string to match against
 * @param caseSensitivity
 *            what case sensitivity rule to use, null means case-sensitive
 * @return true if the filename matches the wilcard string
 * @since Commons IO 1.3
 */
public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity) {
    if (filename == null && wildcardMatcher == null) {
        return true;
    }
    if (filename == null || wildcardMatcher == null) {
        return false;
    }
    if (caseSensitivity == null) {
        caseSensitivity = IOCase.SENSITIVE;
    }
    String[] wcs = splitOnTokens(wildcardMatcher);
    boolean anyChars = false;
    int textIdx = 0;
    int wcsIdx = 0;
    Stack<int[]> backtrack = new Stack<int[]>();

    // loop around a backtrack stack, to handle complex * matching
    do {
        if (backtrack.size() > 0) {
            int[] array = backtrack.pop();
            wcsIdx = array[0];
            textIdx = array[1];
            anyChars = true;
        }

        // loop whilst tokens and text left to process
        while (wcsIdx < wcs.length) {

            if (wcs[wcsIdx].equals("?")) {
                // ? so move to next text char
                textIdx++;
                if (textIdx > filename.length()) {
                    break;
                }
                anyChars = false;

            } else if (wcs[wcsIdx].equals("*")) {
                // set any chars status
                anyChars = true;
                if (wcsIdx == wcs.length - 1) {
                    textIdx = filename.length();
                }

            } else {
                // matching text token
                if (anyChars) {
                    // any chars then try to locate text token
                    textIdx = caseSensitivity.checkIndexOf(filename, textIdx, wcs[wcsIdx]);
                    if (textIdx == -1) {
                        // token not found
                        break;
                    }
                    int repeat = caseSensitivity.checkIndexOf(filename, textIdx + 1, wcs[wcsIdx]);
                    if (repeat >= 0) {
                        backtrack.push(new int[] { wcsIdx, repeat });
                    }
                } else {
                    // matching from current position
                    if (!caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx])) {
                        // couldnt match token
                        break;
                    }
                }

                // matched text token, move text index to end of matched
                // token
                textIdx += wcs[wcsIdx].length();
                anyChars = false;
            }

            wcsIdx++;
        }

        // full match
        if (wcsIdx == wcs.length && textIdx == filename.length()) {
            return true;
        }

    } while (backtrack.size() > 0);

    return false;
}