Example usage for java.util LinkedList removeLast

List of usage examples for java.util LinkedList removeLast

Introduction

In this page you can find the example usage for java.util LinkedList removeLast.

Prototype

public E removeLast() 

Source Link

Document

Removes and returns the last element from this list.

Usage

From source file:org.myrian.persistence.oql.QFrame.java

private void render(LinkedList joins, List where, QFrame oroot, QFrame root, Set emitted) {
    // If the first non empty frame is outer we treat it as inner.
    if (m_outer && !joins.isEmpty()) {
        oroot = this;
    }/*w w  w .j  av  a  2 s  . c  om*/

    Code table = null;
    if (m_table != null && m_duplicate == null) {
        table = new Code(m_table).add(" ").add(alias());
    } else if (m_tableExpr != null && m_duplicate == null) {
        table = m_tableExpr.emit(m_generator).add(" ").add(alias());
    }

    if (table != null) {
        joins.addFirst(JFrame.leaf(table, this, oroot));
    }

    List children = getChildren();
    for (int i = 0; i < children.size(); i++) {
        QFrame child = (QFrame) children.get(i);
        child.render(joins, where, oroot, root, emitted);
    }

    if (m_condition != null) {
        Code c = m_condition.emit(m_generator);
        if (!c.isTrue() && !emitted.contains(c)) {
            m_used.clear();
            frames(m_condition, m_used);
            boolean join = false;
            for (Iterator it = joins.iterator(); it.hasNext();) {
                JFrame frame = (JFrame) it.next();
                boolean modified = m_used.removeAll(frame.defined);
                if (m_used.isEmpty()) {
                    // We default to putting things in the where
                    // clause here because oracle won't resolve
                    // external variable references correctly when
                    // they appear in join conditions.
                    if (oroot.equals(root)) {
                        where.add(c);
                    } else if (frame.froot != null && oroot.equals(frame.froot)) {
                        frame.join = frame.join.add(" and ").add(c);
                    } else {
                        /*
                         * XXX rhs needs to make sure this is
                         * the right thing to do
                         */
                        where.add(c);
                    }
                } else if (modified) {
                    join = true;
                    break;
                }
            }
            if (join) {
                JFrame right = (JFrame) joins.removeFirst();

                if (joins.isEmpty()) {
                    throw new IllegalStateException(
                            "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                }

                LinkedList skipped = null;
                JFrame left = (JFrame) joins.removeFirst();
                while (true) {
                    m_used.clear();
                    frames(m_condition, m_used);
                    m_used.removeAll(right.defined);
                    boolean cross = m_used.removeAll(left.defined);
                    if (m_used.isEmpty()) {
                        joins.addFirst(JFrame.join(left, right, c));
                        break;
                    } else if (joins.isEmpty()) {
                        throw new IllegalStateException(
                                "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                    } else if (cross) {
                        JFrame lefter = (JFrame) joins.removeFirst();
                        left = JFrame.cross(lefter, left);
                    } else {
                        if (skipped == null) {
                            skipped = new LinkedList();
                        }
                        skipped.addLast(left);
                        left = (JFrame) joins.removeFirst();
                    }
                }

                if (skipped != null) {
                    while (!skipped.isEmpty()) {
                        joins.addFirst(skipped.removeLast());
                    }
                }
            }

            emitted.add(c);
        }
    }
}

From source file:org.alfresco.repo.search.impl.lucene.ADMLuceneSearcherImpl.java

public List<Pair<String, Integer>> getTopTerms(String field, int count) {
    ClosingIndexSearcher searcher = null;
    try {//from  w  w  w .ja v  a 2 s  . c  o m
        LinkedList<Pair<String, Integer>> answer = new LinkedList<Pair<String, Integer>>();
        searcher = getSearcher(indexer);
        IndexReader reader = searcher.getIndexReader();
        TermEnum terms = reader.terms(new Term(field, ""));
        do {
            Term term = terms.term();
            if (term != null) {
                if (!term.field().equals(field)) {
                    break;
                }
                int freq = terms.docFreq();
                Pair<String, Integer> pair = new Pair<String, Integer>(term.text(), Integer.valueOf(freq));
                if (answer.size() < count) {
                    if (answer.size() == 0) {
                        answer.add(pair);
                    } else if (answer.get(answer.size() - 1).getSecond().compareTo(pair.getSecond()) >= 0) {
                        answer.add(pair);
                    } else {
                        for (ListIterator<Pair<String, Integer>> it = answer.listIterator(); it
                                .hasNext(); /**/) {
                            Pair<String, Integer> test = it.next();
                            if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                                it.previous();
                                it.add(pair);
                                break;
                            }
                        }
                    }
                } else if (answer.get(count - 1).getSecond().compareTo(pair.getSecond()) < 0) {
                    for (ListIterator<Pair<String, Integer>> it = answer.listIterator(); it.hasNext(); /**/) {
                        Pair<String, Integer> test = it.next();
                        if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                            it.previous();
                            it.add(pair);
                            break;
                        }
                    }
                    answer.removeLast();
                } else {
                    // off the end
                }
            }
        } while (terms.next());
        terms.close();
        return answer;

    } catch (IOException e) {
        throw new SearcherException(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                throw new SearcherException(e);
            }
        }
    }

}

From source file:org.jasig.portal.url.UrlSyntaxProviderImpl.java

@Override
public IPortalRequestInfo getPortalRequestInfo(HttpServletRequest request) {
    request = this.portalRequestUtils.getOriginalPortalRequest(request);
    final IPortalRequestInfo cachedPortalRequestInfo = (IPortalRequestInfo) request
            .getAttribute(PORTAL_REQUEST_INFO_ATTR);
    if (cachedPortalRequestInfo != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("short-circuit: found portalRequestInfo within request attributes");
        }//from   w  w w  . j  av  a2  s  .c  om
        return cachedPortalRequestInfo;
    }

    synchronized (PortalWebUtils.getRequestAttributeMutex(request)) {
        // set a flag to say this request is currently being parsed
        final Boolean inProgressAttr = (Boolean) request.getAttribute(PORTAL_REQUEST_PARSING_IN_PROGRESS_ATTR);
        if (inProgressAttr != null && inProgressAttr) {
            if (logger.isDebugEnabled()) {
                logger.warn("Portal request info parsing already in progress, returning null");
            }
            return null;
        }
        request.setAttribute(PORTAL_REQUEST_PARSING_IN_PROGRESS_ATTR, Boolean.TRUE);
    }

    try {
        //Clone the parameter map so data can be removed from it as it is parsed to help determine what to do with non-namespaced parameters
        final Map<String, String[]> parameterMap = new ParameterMap(request.getParameterMap());

        final String requestPath = this.urlPathHelper.getPathWithinApplication(request);

        if (LEGACY_URL_PATHS.contains(requestPath)) {
            return parseLegacyPortalUrl(request, parameterMap);
        }

        final IUrlNodeSyntaxHelper urlNodeSyntaxHelper = this.urlNodeSyntaxHelperRegistry
                .getCurrentUrlNodeSyntaxHelper(request);

        final PortalRequestInfoImpl portalRequestInfo = new PortalRequestInfoImpl();
        IPortletWindowId targetedPortletWindowId = null;
        PortletRequestInfoImpl targetedPortletRequestInfo = null;

        final String[] requestPathParts = SLASH_PATTERN.split(requestPath);

        UrlState requestedUrlState = null;
        ParseStep parseStep = ParseStep.FOLDER;
        for (int pathPartIndex = 0; pathPartIndex < requestPathParts.length; pathPartIndex++) {
            String pathPart = requestPathParts[pathPartIndex];
            if (StringUtils.isEmpty(pathPart)) {
                continue;
            }

            switch (parseStep) {
            case FOLDER: {
                parseStep = ParseStep.PORTLET;

                if (FOLDER_PATH_PREFIX.equals(pathPart)) {
                    //Skip adding the prefix to the folders deque
                    pathPartIndex++;

                    final LinkedList<String> folders = new LinkedList<String>();
                    for (; pathPartIndex < requestPathParts.length; pathPartIndex++) {
                        pathPart = requestPathParts[pathPartIndex];

                        //Found the portlet part of the path, step back one and finish folder parsing
                        if (PORTLET_PATH_PREFIX.equals(pathPart)) {
                            pathPartIndex--;
                            break;
                        }
                        //Found the end of the path, step back one, check for state and finish folder parsing
                        else if (pathPart.endsWith(REQUEST_TYPE_SUFFIX)) {
                            pathPartIndex--;
                            pathPart = requestPathParts[pathPartIndex];

                            //If a state was added to the folder list remove it and step back one so other code can handle it
                            if (UrlState.valueOfIngoreCase(pathPart, null) != null) {
                                folders.removeLast();
                                pathPartIndex--;
                            }
                            break;
                        }

                        folders.add(pathPart);
                    }

                    if (folders.size() > 0) {
                        final String targetedLayoutNodeId = urlNodeSyntaxHelper
                                .getLayoutNodeForFolderNames(request, folders);
                        portalRequestInfo.setTargetedLayoutNodeId(targetedLayoutNodeId);
                    }
                    break;
                }
            }
            case PORTLET: {
                parseStep = ParseStep.STATE;

                final String targetedLayoutNodeId = portalRequestInfo.getTargetedLayoutNodeId();

                if (PORTLET_PATH_PREFIX.equals(pathPart)) {
                    if (++pathPartIndex < requestPathParts.length) {
                        pathPart = requestPathParts[pathPartIndex];

                        targetedPortletWindowId = urlNodeSyntaxHelper.getPortletForFolderName(request,
                                targetedLayoutNodeId, pathPart);
                    }

                    break;
                }

                //See if a portlet was targeted by parameter  
                final String[] targetedPortletIds = parameterMap.remove(PARAM_TARGET_PORTLET);
                if (targetedPortletIds != null && targetedPortletIds.length > 0) {
                    final String targetedPortletString = targetedPortletIds[0];
                    targetedPortletWindowId = urlNodeSyntaxHelper.getPortletForFolderName(request,
                            targetedLayoutNodeId, targetedPortletString);
                }

            }
            case STATE: {
                parseStep = ParseStep.TYPE;

                //States other than the default only make sense if a portlet is being targeted
                if (targetedPortletWindowId == null) {
                    break;
                }

                requestedUrlState = UrlState.valueOfIngoreCase(pathPart, null);

                //Set the URL state
                if (requestedUrlState != null) {
                    portalRequestInfo.setUrlState(requestedUrlState);

                    //If the request is stateless
                    if (statelessUrlStates.contains(requestedUrlState)) {
                        final IPortletWindow statelessPortletWindow = this.portletWindowRegistry
                                .getOrCreateStatelessPortletWindow(request, targetedPortletWindowId);
                        targetedPortletWindowId = statelessPortletWindow.getPortletWindowId();
                    }

                    //Create the portlet request info
                    targetedPortletRequestInfo = portalRequestInfo
                            .getPortletRequestInfo(targetedPortletWindowId);
                    portalRequestInfo.setTargetedPortletWindowId(targetedPortletWindowId);

                    //Set window state based on URL State first then look for the window state parameter
                    switch (requestedUrlState) {
                    case MAX: {
                        targetedPortletRequestInfo.setWindowState(WindowState.MAXIMIZED);
                    }
                        break;

                    case DETACHED: {
                        targetedPortletRequestInfo.setWindowState(IPortletRenderer.DETACHED);
                    }
                        break;

                    case EXCLUSIVE: {
                        targetedPortletRequestInfo.setWindowState(IPortletRenderer.EXCLUSIVE);
                    }
                        break;
                    }

                    break;
                }
            }
            case TYPE: {
                parseStep = ParseStep.COMPLETE;

                if (pathPartIndex == requestPathParts.length - 1 && pathPart.endsWith(REQUEST_TYPE_SUFFIX)
                        && pathPart.length() > REQUEST_TYPE_SUFFIX.length()) {
                    final String urlTypePart = pathPart.substring(0,
                            pathPart.length() - REQUEST_TYPE_SUFFIX.length());
                    final UrlType urlType;

                    //Handle inline resourceIds, look for a . in the request type string and use the suffix as the urlType
                    final int lastPeriod = urlTypePart.lastIndexOf('.');
                    if (lastPeriod >= 0 && lastPeriod < urlTypePart.length()) {
                        final String urlTypePartSuffix = urlTypePart.substring(lastPeriod + 1);
                        urlType = UrlType.valueOfIngoreCase(urlTypePartSuffix, null);
                        if (urlType == UrlType.RESOURCE && targetedPortletRequestInfo != null) {
                            final String resourceId = urlTypePart.substring(0, lastPeriod);
                            targetedPortletRequestInfo.setResourceId(resourceId);
                        }
                    } else {
                        urlType = UrlType.valueOfIngoreCase(urlTypePart, null);
                    }

                    if (urlType != null) {
                        portalRequestInfo.setUrlType(urlType);
                        break;
                    }
                }
            }
            }
        }

        //If a targeted portlet window ID is found but no targeted portlet request info has been retrieved yet, set it up
        if (targetedPortletWindowId != null && targetedPortletRequestInfo == null) {
            targetedPortletRequestInfo = portalRequestInfo.getPortletRequestInfo(targetedPortletWindowId);
            portalRequestInfo.setTargetedPortletWindowId(targetedPortletWindowId);
        }

        //Get the set of portlet window ids that also have parameters on the url
        final String[] additionalPortletIdArray = parameterMap.remove(PARAM_ADDITIONAL_PORTLET);
        final Set<String> additionalPortletIds = Sets
                .newHashSet(additionalPortletIdArray != null ? additionalPortletIdArray : new String[0]);

        //Used if there is delegation to capture form-submit and other non-prefixed parameters
        //Map of parent id to delegate id
        final Map<IPortletWindowId, IPortletWindowId> delegateIdMappings = new LinkedHashMap<IPortletWindowId, IPortletWindowId>(
                0);

        //Parse all remaining parameters from the request
        final Set<Entry<String, String[]>> parameterEntrySet = parameterMap.entrySet();
        for (final Iterator<Entry<String, String[]>> parameterEntryItr = parameterEntrySet
                .iterator(); parameterEntryItr.hasNext();) {
            final Entry<String, String[]> parameterEntry = parameterEntryItr.next();

            final String name = parameterEntry.getKey();
            final List<String> values = Arrays.asList(parameterEntry.getValue());

            /* NOTE: continues are being used to allow fall-through behavior like a switch statement would provide */

            //Portal Parameters, just need to remove the prefix
            if (name.startsWith(PORTAL_PARAM_PREFIX)) {
                final Map<String, List<String>> portalParameters = portalRequestInfo.getPortalParameters();
                portalParameters.put(this.safeSubstringAfter(PORTAL_PARAM_PREFIX, name), values);
                parameterEntryItr.remove();
                continue;
            }

            //Generic portlet parameters, have to remove the prefix and see if there was a portlet windowId between the prefix and parameter name
            if (name.startsWith(PORTLET_PARAM_PREFIX)) {
                final Tuple<String, IPortletWindowId> portletParameterParts = this
                        .parsePortletParameterName(request, name, additionalPortletIds);
                final IPortletWindowId portletWindowId = portletParameterParts.second;
                final String paramName = portletParameterParts.first;

                //Get the portlet parameter map to add the parameter to
                final Map<String, List<String>> portletParameters;
                if (portletWindowId == null) {
                    if (targetedPortletRequestInfo == null) {
                        this.logger.warn("Parameter " + name
                                + " is for the targeted portlet but no portlet is targeted by the request. The parameter will be ignored. Value: "
                                + values);
                        parameterEntryItr.remove();
                        break;
                    }

                    portletParameters = targetedPortletRequestInfo.getPortletParameters();
                } else {
                    final PortletRequestInfoImpl portletRequestInfoImpl = portalRequestInfo
                            .getPortletRequestInfo(portletWindowId);
                    portletParameters = portletRequestInfoImpl.getPortletParameters();
                }

                portletParameters.put(paramName, values);
                parameterEntryItr.remove();
                continue;
            }

            //Portlet control parameters are either used directly or as a prefix to a windowId. Use the SuffixedPortletParameter to simplify their parsing
            for (final SuffixedPortletParameter suffixedPortletParameter : SuffixedPortletParameter.values()) {
                final String parameterPrefix = suffixedPortletParameter.getParameterPrefix();
                //Skip to the next parameter prefix if the current doesn't match
                if (!name.startsWith(parameterPrefix)) {
                    continue;
                }

                //All of these parameters require at least one value
                if (values.isEmpty()) {
                    this.logger
                            .warn("Ignoring parameter " + name + " as it must have a value. Value: " + values);
                    break;
                }

                //Verify the parameter is being used on the correct type of URL
                final Set<UrlType> validUrlTypes = suffixedPortletParameter.getValidUrlTypes();
                if (!validUrlTypes.contains(portalRequestInfo.getUrlType())) {
                    this.logger.warn("Ignoring parameter " + name + " as it is only valid for " + validUrlTypes
                            + " requests and this is a " + portalRequestInfo.getUrlType() + " request. Value: "
                            + values);
                    break;
                }

                //Determine the portlet window and request info the parameter targets
                final IPortletWindowId portletWindowId = this.parsePortletWindowIdSuffix(request,
                        parameterPrefix, additionalPortletIds, name);
                final PortletRequestInfoImpl portletRequestInfo = getTargetedPortletRequestInfo(
                        portalRequestInfo, targetedPortletRequestInfo, portletWindowId);
                if (portletRequestInfo == null) {
                    this.logger.warn("Parameter " + name
                            + " is for the targeted portlet but no portlet is targeted by the request. The parameter will be ignored. Value: "
                            + values);
                    break;
                }

                parameterEntryItr.remove();

                //Use the enum helper to store the parameter values on the request info
                suffixedPortletParameter.updateRequestInfo(request, portletWindowRegistry, portletRequestInfo,
                        values, delegateIdMappings);
                break;
            }
        }

        //Any non-namespaced parameters still need processing?
        if (!parameterMap.isEmpty()) {
            //If the parameter was not ignored by a previous parser add it to whatever was targeted (portlet or portal)
            final Map<String, List<String>> parameters;
            if (!delegateIdMappings.isEmpty()) {
                //Resolve the last portlet window in the chain of delegation
                PortletRequestInfoImpl delegatePortletRequestInfo = null;
                for (final IPortletWindowId delegatePortletWindowId : delegateIdMappings.values()) {
                    if (!delegateIdMappings.containsKey(delegatePortletWindowId)) {
                        delegatePortletRequestInfo = portalRequestInfo
                                .getPortletRequestInfo(delegatePortletWindowId);
                        break;
                    }
                }

                if (delegatePortletRequestInfo != null) {
                    parameters = delegatePortletRequestInfo.getPortletParameters();
                } else {
                    this.logger.warn(
                            "No root delegate portlet could be resolved, non-namespaced parameters will be sent to the targeted portlet. THIS SHOULD NEVER HAPPEN. Delegate parent/child mapping: "
                                    + delegateIdMappings);

                    if (targetedPortletRequestInfo != null) {
                        parameters = targetedPortletRequestInfo.getPortletParameters();
                    } else {
                        parameters = portalRequestInfo.getPortalParameters();
                    }
                }
            } else if (targetedPortletRequestInfo != null) {
                parameters = targetedPortletRequestInfo.getPortletParameters();
            } else {
                parameters = portalRequestInfo.getPortalParameters();
            }

            ParameterMap.putAllList(parameters, parameterMap);
        }

        //If a portlet is targeted but no layout node is targeted must be maximized
        if (targetedPortletRequestInfo != null && portalRequestInfo.getTargetedLayoutNodeId() == null
                && (requestedUrlState == null || requestedUrlState == UrlState.NORMAL)) {
            portalRequestInfo.setUrlState(UrlState.MAX);
            targetedPortletRequestInfo.setWindowState(WindowState.MAXIMIZED);
        }

        //Make the request info object read-only, once parsed the request info should be static
        portalRequestInfo.makeReadOnly();

        request.setAttribute(PORTAL_REQUEST_INFO_ATTR, portalRequestInfo);

        if (logger.isDebugEnabled()) {
            logger.debug("finished building requestInfo: " + portalRequestInfo);
        }

        return portalRequestInfo;
    } finally {
        request.removeAttribute(PORTAL_REQUEST_PARSING_IN_PROGRESS_ATTR);
    }
}

From source file:com.manydesigns.portofino.navigation.Navigation.java

private void buildTree() {
    int rootPageIndex = dispatch.getClosestSubtreeRootIndex();
    PageInstance[] pageInstances = dispatch.getPageInstancePath(rootPageIndex);
    if (pageInstances == null || pageInstances.length == 0) {
        return;//w  w w .  j  av a  2 s  .  co m
    }
    PageInstance rootPageInstance = pageInstances[0];
    String prefix = "";
    if (rootPageIndex > 0) {
        prefix += rootPageInstance.getParent().getPath() + "/" + rootPageInstance.getName();
    }
    boolean rootSelected = pageInstances.length == 1;
    Page rootPage = rootPageInstance.getPage();
    boolean rootGhost = rootPage.getActualNavigationRoot() == NavigationRoot.GHOST_ROOT;
    rootNavigationItem = new NavigationItem(rootPageInstance.getTitle(), rootPageInstance.getDescription(),
            prefix, true, rootSelected, rootGhost);
    LinkedList<Page> pages = new LinkedList<Page>();
    PageInstance[] allInstances = dispatch.getPageInstancePath();
    for (int i = 0; i <= rootPageIndex; i++) {
        pages.add(allInstances[i].getPage());
    }
    Permissions basePermissions = SecurityLogic.calculateActualPermissions(new Permissions(), pages);
    pages.clear();
    List<ChildPage> childPages;
    NavigationItem currentNavigationItem = rootNavigationItem;
    for (int i = 0, pageInstancesLength = pageInstances.length; i < pageInstancesLength; i++) {
        PageInstance current = pageInstances[i];
        PageInstance next;
        if (i < pageInstancesLength - 1) {
            next = pageInstances[i + 1];
        } else {
            next = null;
        }

        Layout layout = current.getLayout();
        if (layout != null) {
            childPages = layout.getChildPages();
        } else {
            childPages = new ArrayList<ChildPage>();
        }

        List<NavigationItem> currentChildNavigationItems = currentNavigationItem.getChildNavigationItems();
        prefix = currentNavigationItem.getPath() + "/";
        for (String param : current.getParameters()) {
            prefix += param + "/";
        }
        currentNavigationItem = null;
        for (ChildPage childPage : childPages) {
            File pageDir = current.getChildPageDirectory(childPage.getName());
            Page page;
            try {
                page = DispatcherLogic.getPage(pageDir);
            } catch (Exception e) {
                logger.warn("Nonexisting child page: " + pageDir, e);
                continue;
            }
            String path = prefix + childPage.getName();
            boolean inPath = false;
            boolean selected = false;
            String description = page.getDescription();
            if (next != null) {
                if (next.getName().equals(childPage.getName())) {
                    inPath = true;
                    selected = (i == pageInstancesLength - 2);
                    description = next.getDescription();
                }
            }
            pages.add(page);
            if (!skipPermissions) {
                Permissions permissions = SecurityLogic.calculateActualPermissions(basePermissions, pages);
                if (!SecurityLogic.hasPermissions(configuration, permissions, subject, AccessLevel.VIEW)) {
                    pages.removeLast();
                    continue;
                }
            }
            pages.removeLast();
            if (!childPage.isShowInNavigation() && !inPath) {
                continue;
            }
            NavigationItem childNavigationItem = new NavigationItem(page.getTitle(), description, path, inPath,
                    selected, false);
            currentChildNavigationItems.add(childNavigationItem);
            if (inPath) {
                currentNavigationItem = childNavigationItem;
            }
        }
        if (currentNavigationItem == null && next != null) {
            boolean selected = (i == pageInstancesLength - 2);
            String path = prefix + next.getName();
            currentNavigationItem = new NavigationItem(next.getTitle(), next.getDescription(), path, true,
                    selected, false);
            currentChildNavigationItems.add(currentNavigationItem);
        }

        if (next != null) {
            pages.add(next.getPage());
        }
    }
}

From source file:it.doqui.index.ecmengine.business.personalization.multirepository.index.lucene.RepositoryAwareADMLuceneSearcherImpl.java

public List<Pair<String, Integer>> getTopTerms(String field, int count) {
    RepositoryAwareClosingIndexSearcher searcher = null;
    try {/* ww w  .  jav a  2s. c om*/
        LinkedList<Pair<String, Integer>> answer = new LinkedList<Pair<String, Integer>>();
        searcher = getSearcher(indexer);
        IndexReader reader = searcher.getIndexReader();
        TermEnum terms = reader.terms(new Term(field, ""));
        do {
            Term term = terms.term();
            if (term != null) {
                if (!term.field().equals(field)) {
                    break;
                }
                int freq = terms.docFreq();
                Pair<String, Integer> pair = new Pair<String, Integer>(term.text(), Integer.valueOf(freq));
                if (answer.size() < count) {
                    if (answer.size() == 0) {
                        answer.add(pair);
                    } else if (answer.get(answer.size() - 1).getSecond().compareTo(pair.getSecond()) >= 0) {
                        answer.add(pair);
                    } else {
                        for (ListIterator<Pair<String, Integer>> it = answer.listIterator(); it
                                .hasNext(); /**/) {
                            Pair<String, Integer> test = it.next();
                            if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                                it.previous();
                                it.add(pair);
                                break;
                            }
                        }
                    }
                } else if (answer.get(count - 1).getSecond().compareTo(pair.getSecond()) < 0) {
                    for (ListIterator<Pair<String, Integer>> it = answer.listIterator(); it.hasNext(); /**/) {
                        Pair<String, Integer> test = it.next();
                        if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                            it.previous();
                            it.add(pair);
                            break;
                        }
                    }
                    answer.removeLast();
                } else {
                    // off the end
                }
            }
        } while (terms.next());
        terms.close();
        return answer;

    } catch (IOException e) {
        throw new SearcherException(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                throw new SearcherException(e);
            }
        }
    }

}

From source file:com.zimbra.cs.service.mail.ToXML.java

private static void addParts(Element root, MPartInfo mpiRoot, Set<MPartInfo> bodies, String prefix, int maxSize,
        boolean neuter, boolean excludeCalendarParts, String defaultCharset, boolean swallowContentExceptions,
        MsgContent wantContent) throws ServiceException {
    MPartInfo mpi = mpiRoot;//from  www.j ava2 s . c  o m
    LinkedList<Pair<Element, LinkedList<MPartInfo>>> queue = new LinkedList<Pair<Element, LinkedList<MPartInfo>>>();
    Pair<Element, LinkedList<MPartInfo>> level = new Pair<Element, LinkedList<MPartInfo>>(root,
            new LinkedList<MPartInfo>());
    level.getSecond().add(mpi);
    queue.add(level);

    VisitPhase phase = VisitPhase.PREVISIT;
    while (!queue.isEmpty()) {
        level = queue.getLast();
        LinkedList<MPartInfo> parts = level.getSecond();
        if (parts.isEmpty()) {
            queue.removeLast();
            phase = VisitPhase.POSTVISIT;
            continue;
        }

        mpi = parts.getFirst();
        Element child = addPart(phase, level.getFirst(), root, mpi, bodies, prefix, maxSize, neuter,
                excludeCalendarParts, defaultCharset, swallowContentExceptions, wantContent);
        if (phase == VisitPhase.PREVISIT && child != null && mpi.hasChildren()) {
            queue.addLast(new Pair<Element, LinkedList<MPartInfo>>(child,
                    new LinkedList<MPartInfo>(mpi.getChildren())));
        } else {
            parts.removeFirst();
            phase = VisitPhase.PREVISIT;
        }
    }
}

From source file:com.moviejukebox.plugin.KinopoiskPlugin.java

/**
 * Scan Kinopoisk html page for the specified movie
 *//*w w  w. j  a  va 2s  .c o  m*/
private boolean updateKinopoiskMediaInfo(Movie movie, String kinopoiskId) {
    try {
        String originalTitle = movie.getTitle();
        String newTitle = originalTitle;
        String xml = httpClient.request(FILM_URL + kinopoiskId, CHARSET);
        boolean etalonFlag = kinopoiskId.equals(etalonId);
        // Work-around for issue #649
        xml = xml.replace("&#133;", "&hellip;");
        xml = xml.replace("&#151;", "&mdash;");

        // Title
        boolean overrideTitle = OverrideTools.checkOverwriteTitle(movie, KINOPOISK_PLUGIN_ID);
        if (overrideTitle || etalonFlag) {
            newTitle = HTMLTools.extractTag(xml, "class=\"moviename-big\" itemprop=\"name\">", 0, "</");
            if (StringTools.isValidString(newTitle)) {
                if (overrideTitle) {
                    int i = newTitle.indexOf("(?");
                    if (i >= 0) {
                        newTitle = newTitle.substring(0, i);
                        movie.setMovieType(Movie.TYPE_TVSHOW);
                    }
                    newTitle = newTitle.replace('\u00A0', ' ').trim();
                    if (movie.getSeason() != -1) {
                        newTitle = newTitle + ", ? " + movie.getSeason();
                    }

                    // Original title
                    originalTitle = newTitle;
                    for (String s : HTMLTools.extractTags(xml, "class=\"moviename-big\" itemprop=\"name\">",
                            "</span>", "itemprop=\"alternativeHeadline\">", "</span>")) {
                        if (!s.isEmpty()) {
                            originalTitle = s;
                            if (joinTitles) {
                                newTitle = newTitle + Movie.SPACE_SLASH_SPACE + originalTitle;
                            }
                        }
                        break;
                    }
                } else {
                    newTitle = originalTitle;
                }
            } else {
                if (etalonFlag) {
                    LOG.error(SITE_DESIGN, "movie title");
                }
                newTitle = originalTitle;
            }
        }

        // Plot
        if (OverrideTools.checkOverwritePlot(movie, KINOPOISK_PLUGIN_ID)) {
            StringBuilder plot = new StringBuilder();
            for (String subPlot : HTMLTools.extractTags(xml, "<span class=\"_reachbanner_\"", "</span>", "",
                    "<")) {
                if (!subPlot.isEmpty()) {
                    if (plot.length() > 0) {
                        plot.append(" ");
                    }
                    plot.append(subPlot);
                }
            }

            movie.setPlot(plot.toString(), KINOPOISK_PLUGIN_ID);

            if (etalonFlag && (plot.length() == 0)) {
                LOG.error(SITE_DESIGN, "plot");
            }
        }

        boolean valueFounded = false;
        boolean genresFounded = false;
        boolean certificationFounded = false;
        boolean countryFounded = false;
        boolean yearFounded = false;
        boolean runtimeFounded = false;
        boolean taglineFounded = false;
        boolean releaseFounded = false;
        for (String item : HTMLTools.extractTags(xml, "<table class=\"info\">", "</table>", "<tr", "</tr>")) {
            item = "<tr>" + item + "</tr>";
            // Genres
            if (OverrideTools.checkOverwriteGenres(movie, KINOPOISK_PLUGIN_ID)) {
                LinkedList<String> newGenres = new LinkedList<>();
                boolean innerGenresFound = false;
                for (String genre : HTMLTools.extractTags(item, "><", "</tr>",
                        "<a href=\"/lists/m_act%5Bgenre%5D/", "</a>")) {
                    innerGenresFound = true;
                    genre = genre.substring(0, 1).toUpperCase() + genre.substring(1, genre.length());
                    if ("".equalsIgnoreCase(genre)) {
                        newGenres.addFirst(genre);
                    } else {
                        newGenres.add(genre);
                    }
                }
                if (innerGenresFound) {
                    // Limit genres count
                    int maxGenres = 9;
                    try {
                        maxGenres = PropertiesUtil.getIntProperty("genres.max", 9);
                    } catch (Exception ignore) {
                        //
                    }
                    while (newGenres.size() > maxGenres) {
                        newGenres.removeLast();
                    }

                    movie.setGenres(newGenres, KINOPOISK_PLUGIN_ID);
                    genresFounded = true;
                }
            }

            // Certification from MPAA
            if (OverrideTools.checkOverwriteCertification(movie, KINOPOISK_PLUGIN_ID)) {
                if (!certificationFounded) {
                    certificationFounded = getCertification(movie, item, "> MPAA<", "</tr>",
                            "<a href=\"/film", "</a>", "alt=\" ");
                }
                if (!certificationFounded || russianCertification) {
                    certificationFounded |= getCertification(movie, item, ">?<", "</tr>",
                            "<td style=\"color", "</span>", "ageLimit ");
                }
            }

            // Country
            if (OverrideTools.checkOverwriteCountry(movie, KINOPOISK_PLUGIN_ID)) {
                Collection<String> scraped = HTMLTools.extractTags(item, ">?<", "</tr>",
                        "a href=\"/lists/m_act%5Bcountry%5D/", "</a>");
                if (scraped != null && !scraped.isEmpty()) {
                    List<String> countries = new ArrayList<>();
                    for (String country : scraped) {
                        if (translitCountry) {
                            country = FileTools.makeSafeFilename(country);
                        }
                        countries.add(country);

                        if (!countryAll) {
                            // just first country, so break here
                            break;
                        }
                    }
                    movie.setCountries(countries, KINOPOISK_PLUGIN_ID);
                    countryFounded = true;
                }
            }

            // Year
            if (OverrideTools.checkOverwriteYear(movie, KINOPOISK_PLUGIN_ID)) {
                for (String year : HTMLTools.extractTags(item, "><", "</tr>",
                        "<a href=\"/lists/m_act%5Byear%5D/", "</a>")) {
                    movie.setYear(year, KINOPOISK_PLUGIN_ID);
                    yearFounded = true;
                    break;
                }
            }

            // Run time
            if (OverrideTools.checkOverwriteRuntime(movie, KINOPOISK_PLUGIN_ID) || etalonFlag) {
                for (String runtime : HTMLTools.extractTags(item, ">?<", "</tr>", "<td", "</td>")) {
                    if (runtime.contains("<span")) {
                        runtime = runtime.substring(0, runtime.indexOf("<span"));
                    }
                    movie.setRuntime(runtime, KINOPOISK_PLUGIN_ID);
                    runtimeFounded = true;
                    break;
                }
            }

            // Tagline
            if (OverrideTools.checkOverwriteTagline(movie, KINOPOISK_PLUGIN_ID)) {
                for (String tagline : HTMLTools.extractTags(item, ">?<", "</tr>", "<td ", "</td>")) {
                    if (tagline.length() > 0) {
                        movie.setTagline(tagline.replace("\u00AB", "\"").replace("\u00BB", "\""),
                                KINOPOISK_PLUGIN_ID);
                        taglineFounded = true;
                        break;
                    }
                }
            }

            // Release date
            if (OverrideTools.checkOverwriteReleaseDate(movie, KINOPOISK_PLUGIN_ID)) {
                String releaseDate = "";
                for (String release : HTMLTools.extractTags(item, "> ()<", "</tr>",
                        "<a href=\"/film/", "</a>")) {
                    releaseDate = release;
                    releaseFounded = true;
                    break;
                }
                if (StringUtils.isBlank(releaseDate)) {
                    for (String release : HTMLTools.extractTags(item, "> ()<", "</tr>",
                            "<a href=\"/film/", "</a>")) {
                        releaseDate = release;
                        releaseFounded = true;
                        break;
                    }
                }
                movie.setReleaseDate(releaseDate, KINOPOISK_PLUGIN_ID);
            }
        }

        if (etalonFlag) {
            if (!genresFounded) {
                LOG.error(SITE_DESIGN, "genres");
            }
            if (!certificationFounded) {
                LOG.error(SITE_DESIGN, "certification");
            }
            if (!countryFounded) {
                LOG.error(SITE_DESIGN, "country");
            }
            if (!yearFounded) {
                LOG.error(SITE_DESIGN, "year");
            }
            if (!runtimeFounded) {
                LOG.error(SITE_DESIGN, "runtime");
            }
            if (!taglineFounded) {
                LOG.error(SITE_DESIGN, "tagline");
            }
            if (!releaseFounded) {
                LOG.error(SITE_DESIGN, "release");
            }
        }

        // Rating
        if (!nfoRating) {
            valueFounded = false;
            for (String rating : HTMLTools.extractTags(xml, "<a href=\"/film/" + kinopoiskId + "/votes/\"",
                    "</a>", "<span", "</span>")) {
                int kinopoiskRating = StringTools.parseRating(rating);
                movie.addRating(KINOPOISK_PLUGIN_ID, kinopoiskRating);
                valueFounded = true;
                break;
            }
            if (!valueFounded && etalonFlag) {
                LOG.error(SITE_DESIGN, "rating");
            }

            int imdbRating = movie.getRating(IMDB_PLUGIN_ID);
            if (imdbRating == -1 || etalonFlag) {
                // Get IMDB rating from kinopoisk page
                String rating = HTMLTools.extractTag(xml, ">IMDb:", 0, " (");
                valueFounded = false;
                if (StringTools.isValidString(rating)) {
                    imdbRating = StringTools.parseRating(rating);
                    movie.addRating(IMDB_PLUGIN_ID, imdbRating);
                    valueFounded = true;
                }
                if (!valueFounded && etalonFlag) {
                    LOG.error(SITE_DESIGN, "IMDB rating");
                }
            }
        }

        // Top250
        if (OverrideTools.checkOverwriteTop250(movie, KINOPOISK_PLUGIN_ID)) {
            movie.setTop250(HTMLTools.extractTag(xml, "<a href=\"/level/20/#", 0, "\""), KINOPOISK_PLUGIN_ID);
        }

        // Poster
        String posterURL = movie.getPosterURL();
        if (StringTools.isNotValidString(posterURL) || (!nfoPoster && poster) || etalonFlag) {
            valueFounded = false;
            if (poster || etalonFlag) {
                String previousURL = posterURL;
                posterURL = Movie.UNKNOWN;

                // Load page with all poster
                String wholeArts = httpClient.request(FILM_URL + kinopoiskId + "/posters/", CHARSET);
                if (StringTools.isValidString(wholeArts)) {
                    if (wholeArts.contains("<table class=\"fotos")) {
                        String picture = HTMLTools.extractTag(wholeArts,
                                "src=\"http://st.kinopoisk.ru/images/poster/sm_", 0, "\"");
                        if (StringTools.isValidString(picture)) {
                            posterURL = "http://st.kinopoisk.ru/images/poster/" + picture;
                            valueFounded = true;
                        }
                    }
                }

                if (StringTools.isNotValidString(posterURL)) {
                    posterURL = previousURL;
                }

                if (StringTools.isValidString(posterURL)) {
                    movie.setPosterURL(posterURL);
                    movie.setPosterFilename(movie.getBaseName() + ".jpg");
                    LOG.debug("Set poster URL to {} for {}", posterURL, movie.getBaseName());
                }
            }
            if (StringTools.isNotValidString(movie.getPosterURL())) {
                if (overrideTitle) {
                    movie.setTitle(originalTitle, KINOPOISK_PLUGIN_ID);
                }
                // Removing Poster info from plugins. Use of PosterScanner routine instead.
                // movie.setPosterURL(locatePosterURL(movie, ""));
            }
            if (!valueFounded && etalonFlag) {
                LOG.error(SITE_DESIGN, "poster");
            }
        }

        // Fanart
        String fanURL = movie.getFanartURL();
        if (StringTools.isNotValidString(fanURL) || (!nfoFanart && (fanArt || kadr)) || etalonFlag
                || fanURL.contains("originalnull")) {
            valueFounded = false;
            try {
                String previousURL = fanURL;
                fanURL = Movie.UNKNOWN;

                // Load page with all wallpaper
                String wholeArts = httpClient.request("http://www.kinopoisk.ru/film/" + kinopoiskId + "/wall/",
                        CHARSET);
                if (StringTools.isValidString(wholeArts)) {
                    if (wholeArts.contains("<table class=\"fotos")) {
                        String picture = HTMLTools.extractTag(wholeArts,
                                "src=\"http://st.kinopoisk.ru/images/wallpaper/sm_", 0, ".jpg");
                        if (StringTools.isValidString(picture)) {
                            if (picture.contains("_")) {
                                picture = picture.substring(0, picture.indexOf('_'));
                            }
                            String size = HTMLTools.extractTag(wholeArts,
                                    "<u><a href=\"/picture/" + picture + "/w_size/", 0, "/");
                            wholeArts = httpClient.request(
                                    "http://www.kinopoisk.ru/picture/" + picture + "/w_size/" + size, CHARSET);
                            if (StringTools.isValidString(wholeArts)) {
                                picture = HTMLTools.extractTag(wholeArts,
                                        "src=\"http://st-im.kinopoisk.r/im/wallpaper/", 0, "\"");
                                if (StringTools.isValidString(picture)) {
                                    fanURL = "http://st.kinopoisk.ru/im/wallpaper/" + picture;
                                    valueFounded = true;
                                }
                            }
                        }
                    }
                }

                if (kadr && (StringTools.isNotValidString(fanURL) || fanURL.contains("originalnull"))) {
                    // Load page with all videoimage
                    wholeArts = httpClient.request(FILM_URL + kinopoiskId + "/stills/", CHARSET);
                    if (StringTools.isValidString(wholeArts)) {
                        // Looking for photos table
                        int photosInd = wholeArts.indexOf("<table class=\"fotos");
                        if (photosInd != -1) {
                            String picture = HTMLTools.extractTag(wholeArts,
                                    "src=\"http://st.kinopoisk.ru/images/kadr/sm_", 0, "\"");
                            if (StringTools.isValidString(picture)) {
                                fanURL = "http://www.kinopoisk.ru/images/kadr/" + picture;
                                valueFounded = true;
                            }
                        }
                    }
                }

                if (StringTools.isNotValidString(fanURL)) {
                    fanURL = previousURL;
                }

                if (StringTools.isValidString(fanURL)) {
                    movie.setFanartURL(fanURL);
                    movie.setFanartFilename(movie.getBaseName() + fanartToken + "." + fanartExtension);
                    LOG.debug("Set fanart URL to {} for {}", fanURL, movie.getBaseName());
                }
            } catch (IOException ex) {
                // Ignore
            }
        }

        // Studio/Company
        if (OverrideTools.checkOverwriteCompany(movie, KINOPOISK_PLUGIN_ID)) {
            xml = httpClient.request(FILM_URL + kinopoiskId + "/studio/", CHARSET);
            valueFounded = false;
            if (StringTools.isValidString(xml)) {
                Collection<String> studio = new ArrayList<>();
                if (xml.contains(">?:<")) {
                    for (String tmp : HTMLTools.extractTags(xml, ">?:<", "</table>",
                            "<a href=\"/lists/m_act%5Bstudio%5D/", "</a>")) {
                        studio.add(HTMLTools.removeHtmlTags(tmp));
                        if (!companyAll) {
                            break;
                        }
                    }
                }
                if (xml.contains(">:<") && (companyAll || studio.isEmpty())) {
                    for (String tmp : HTMLTools.extractTags(xml, ">:<", "</table>",
                            "<a href=\"/lists/m_act%5Bcompany%5D/", "</a>")) {
                        studio.add(HTMLTools.removeHtmlTags(tmp));
                    }
                }
                if (!studio.isEmpty()) {
                    movie.setCompany(companyAll ? StringUtils.join(studio, Movie.SPACE_SLASH_SPACE)
                            : new ArrayList<>(studio).get(0), KINOPOISK_PLUGIN_ID);
                    valueFounded = true;
                }
            }
            if (!valueFounded && etalonFlag) {
                LOG.error(SITE_DESIGN, "company");
            }
        }

        // Awards
        if ((scrapeAwards && !nfoAwards) || etalonFlag) {
            if (clearAward) {
                movie.clearAwards();
            }
            xml = httpClient.request("http://www.kinopoisk.ru/film/" + kinopoiskId + "/awards/", CHARSET);
            Collection<AwardEvent> awards = new ArrayList<>();
            if (StringTools.isValidString(xml)) {
                int beginIndex = xml.indexOf("<b><a href=\"/awards/");
                if (beginIndex != -1) {
                    for (String item : HTMLTools.extractTags(xml,
                            "<table cellspacing=0 cellpadding=0 border=0 width=100%>",
                            "<br /><br /><br /><br /><br /><br />",
                            "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\" ",
                            "</table>")) {
                        String name = Movie.UNKNOWN;
                        int year = -1;
                        int won = 0;
                        int nominated = 0;
                        Collection<String> wons = new ArrayList<>();
                        Collection<String> nominations = new ArrayList<>();
                        for (String tmp : HTMLTools.extractTags(item,
                                "<td height=\"40\" class=\"news\" style=\"padding: 10px\">", "</td>",
                                "<a href=\"/awards/", "</a>")) {
                            int coma = tmp.indexOf(",");
                            name = tmp.substring(0, coma);
                            year = NumberUtils.toInt(tmp.substring(coma + 2, coma + 6));
                            break;
                        }
                        for (String tmp : HTMLTools.extractTags(item, "><", ":", "(",
                                ")")) {
                            won = NumberUtils.toInt(tmp);
                            break;
                        }
                        if (won > 0) {
                            for (String tmp : HTMLTools.extractTags(item, "><", "</ul>",
                                    "<li class=\"trivia\">", "</li>")) {
                                wons.add(HTMLTools.removeHtmlTags(tmp).replaceAll(" {2,}", " "));
                            }
                        }
                        for (String tmp : HTMLTools.extractTags(item, ">?<", ":", "(", ")")) {
                            nominated = NumberUtils.toInt(tmp);
                            break;
                        }
                        if (nominated > 0) {
                            for (String tmp : HTMLTools.extractTags(item, ">?<", "</ul>",
                                    "<li class=\"trivia\"", "</li>")) {
                                nominations.add(HTMLTools.removeHtmlTags(tmp).replaceAll(" {2,}", " "));
                            }
                        }
                        if (StringTools.isValidString(name) && year > 1900 && year < 2020
                                && (!scrapeWonAwards || (won > 0))) {
                            Award award = new Award();
                            award.setName(name);
                            award.setYear(year);
                            if (won > 0) {
                                award.setWons(wons);
                            }
                            if (nominated > 0) {
                                award.setNominations(nominations);
                            }

                            AwardEvent event = new AwardEvent();
                            event.setName(name);
                            event.addAward(award);
                            awards.add(event);
                        }
                    }
                }
            }
            if (!awards.isEmpty()) {
                movie.setAwards(awards);
            } else if (etalonFlag) {
                LOG.error(SITE_DESIGN, "award");
            }
        }

        // Cast enhancement
        boolean overrideCast = OverrideTools.checkOverwriteActors(movie, KINOPOISK_PLUGIN_ID);
        boolean overridePeopleCast = OverrideTools.checkOverwritePeopleActors(movie, KINOPOISK_PLUGIN_ID);
        boolean overrideDirectors = OverrideTools.checkOverwriteDirectors(movie, KINOPOISK_PLUGIN_ID);
        boolean overridePeopleDirectors = OverrideTools.checkOverwritePeopleDirectors(movie,
                KINOPOISK_PLUGIN_ID);
        boolean overrideWriters = OverrideTools.checkOverwriteWriters(movie, KINOPOISK_PLUGIN_ID);
        boolean overridePeopleWriters = OverrideTools.checkOverwritePeopleWriters(movie, KINOPOISK_PLUGIN_ID);

        if (overrideCast || overridePeopleCast || overrideDirectors || overridePeopleDirectors
                || overrideWriters || overridePeopleWriters || etalonFlag) {
            xml = httpClient.request(FILM_URL + kinopoiskId + "/cast", CHARSET);
            if (StringTools.isValidString(xml)) {
                if (overrideDirectors || overridePeopleDirectors || etalonFlag) {
                    int count = scanMoviePerson(movie, xml, "director", directorMax, overrideDirectors,
                            overridePeopleDirectors);
                    if (etalonFlag && count == 0) {
                        LOG.error(SITE_DESIGN, "directors");
                    }
                }
                if (overrideWriters || overridePeopleWriters || etalonFlag) {
                    int count = scanMoviePerson(movie, xml, "writer", writerMax, overrideWriters,
                            overridePeopleWriters);
                    if (etalonFlag && count == 0) {
                        LOG.error(SITE_DESIGN, "writers");
                    }
                }
                if (overrideCast || overridePeopleCast || etalonFlag) {
                    int count = scanMoviePerson(movie, xml, "actor", actorMax, overrideCast,
                            overridePeopleCast);
                    if (etalonFlag && count == 0) {
                        LOG.error(SITE_DESIGN, "cast");
                    }
                }

                Collection<Filmography> outcast = new ArrayList<>();
                for (Filmography p : movie.getPeople()) {
                    if (StringTools.isNotValidString(p.getId(KINOPOISK_PLUGIN_ID))) {
                        outcast.add(p);
                    }
                }
                for (Filmography p : outcast) {
                    movie.removePerson(p);
                }
            }
        }

        // Business
        if (scrapeBusiness || etalonFlag) {
            xml = httpClient.request(FILM_URL + kinopoiskId + "/box/", CHARSET);
            if (StringTools.isValidString(xml)) {
                valueFounded = false;
                for (String tmp : HTMLTools.extractTags(xml, ">:<", "</table>",
                        "<font color=\"#ff6600\"", "</h3>")) {
                    if (StringTools.isValidString(tmp)) {
                        tmp = tmp.replaceAll("\u00A0", ",").replaceAll(",$", "");
                        if (StringTools.isValidString(tmp) && !"--".equals(tmp)) {
                            movie.setBudget(tmp);
                            valueFounded = true;
                            break;
                        }
                    }
                }
                if (!valueFounded && etalonFlag) {
                    LOG.error(SITE_DESIGN, "business: summary");
                }
                valueFounded = false;
                for (String tmp : HTMLTools.extractTags(xml, "> -? (?)<",
                        "</table>", "<h3 style=\"font-size: 18px; margin: 0; padding: 0;color:#f60\"",
                        "</h3>")) {
                    if (StringTools.isValidString(tmp)) {
                        tmp = tmp.replaceAll("\u00A0", ",").replaceAll(",$", "");
                        if (StringTools.isValidString(tmp) && !"--".equals(tmp)) {
                            movie.setOpenWeek("USA", tmp);
                            valueFounded = true;
                            break;
                        }
                    }
                }
                if (!valueFounded && etalonFlag) {
                    LOG.error(SITE_DESIGN, "business: first weekend");
                }
                valueFounded = false;
                for (String tmp : HTMLTools.extractTags(xml, ">?? ?<", "</table>",
                        "<tr><td colspan=2", "</h3>")) {
                    tmp += "</h3>";
                    String country = HTMLTools.extractTag(tmp, ">", ":");
                    if (StringTools.isValidString(country)) {
                        String money = HTMLTools
                                .removeHtmlTags("<h3 " + HTMLTools.extractTag(tmp, "<h3 ", "</h3>"))
                                .replaceAll("\u00A0", ",").replaceAll(",$", "");
                        if (!"--".equals(money)) {
                            movie.setGross(
                                    " ??"
                                            .equals(country)
                                                    ? "Russia"
                                                    : " ?".equals(country) ? "USA"
                                                            : " ?".equals(country)
                                                                    ? "Worldwide"
                                                                    : "  ?".equals(
                                                                            country) ? "Others" : country,
                                    money);
                            valueFounded = true;
                        }
                    }
                }
                if (!valueFounded && etalonFlag) {
                    LOG.error(SITE_DESIGN, "business: gross");
                }
            } else if (etalonFlag) {
                LOG.error(SITE_DESIGN, "business");
            }
        }

        // Did You Know
        if (scrapeTrivia || etalonFlag) {
            if (clearTrivia) {
                movie.clearDidYouKnow();
            }
            if (etalonFlag && triviaMax == 0) {
                triviaMax = 1;
            }
            if (triviaMax != 0) {
                xml = httpClient.request(FILM_URL + kinopoiskId + "/view_info/ok/#trivia", CHARSET);
                if (StringTools.isValidString(xml)) {
                    int i = 0;
                    for (String tmp : HTMLTools.extractTags(xml, ">  , ...<", "</ul>",
                            "<li class=\"trivia", "</li>")) {
                        if (i < triviaMax || triviaMax == -1) {
                            movie.addDidYouKnow(HTMLTools.removeHtmlTags(tmp));
                            valueFounded = true;
                            i++;
                        } else {
                            break;
                        }
                    }
                }
            }
            if (!valueFounded && etalonFlag) {
                LOG.error(SITE_DESIGN, "trivia");
            }
        }

        // Finally set title
        if (overrideTitle) {
            movie.setTitle(newTitle, KINOPOISK_PLUGIN_ID);
        }
    } catch (IOException | NumberFormatException error) {
        LOG.error("Failed retreiving movie data from Kinopoisk : {}", kinopoiskId);
        LOG.error(SystemTools.getStackTrace(error));
    }
    return true;
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnRJobRequest.java

public List<List<String>> getValueRange(String tkn) {

    dbgLog.fine("received token=" + tkn);
    String step0 = StringUtils.strip(tkn);
    dbgLog.fine("step0=" + step0);

    // string into tokens
    String[] step1raw = step0.split(",");

    dbgLog.fine("step1raw=" + StringUtils.join(step1raw, ","));

    // remove meaningless commas if exist

    List<String> step1 = new ArrayList<String>();

    for (String el : step1raw) {
        if (!el.equals("")) {
            step1.add(el);/*w ww.  j av  a 2  s . co  m*/
        }
    }

    dbgLog.fine("step1=" + StringUtils.join(step1, ","));

    List<List<String>> rangeData = new ArrayList<List<String>>();

    // for each token, check the range operator

    for (int i = 0; i < step1.size(); i++) {
        LinkedList<String> tmp = new LinkedList<String>(
                Arrays.asList(String2StringArray(String.valueOf(step1.get(i)))));

        Map<String, String> token = new HashMap<String, String>();
        boolean rangeMode = false;

        // .get(i) below CAN'T possibly be right (??) -- replacing
        // it with .get(0). -- L.A., v3.6
        //if ((!tmp.get(i).equals("[")) && (!tmp.get(i).equals("("))){
        if ((!tmp.get(0).equals("[")) && (!tmp.get(0).equals("("))) {
            // no LHS range operator
            // assume [
            token.put("start", "3");
        } else if (tmp.get(0).equals("[")) {
            rangeMode = true;
            token.put("start", "3");
            tmp.removeFirst();
        } else if (tmp.get(0).equals("(")) {
            rangeMode = true;
            token.put("start", "5");
            tmp.removeFirst();
        }

        if ((!tmp.getLast().equals("]")) && (!tmp.getLast().equals(")"))) {
            // no RHS range operator
            // assume ]
            token.put("end", "4");
        } else if (tmp.getLast().equals("]")) {
            rangeMode = true;
            tmp.removeLast();
            token.put("end", "4");
        } else if (tmp.getLast().equals(")")) {
            rangeMode = true;
            tmp.removeLast();
            token.put("end", "6");
        }

        // I'm now enforcing the following rules:
        // the "rangeMode" above - a range must have at least one range
        // operator, a square bracket or parenthesis, on one end, at
        // least; i.e., either on the left, or on the right. 
        // If there are no range operators, even if there are dashes
        // inside the token, they are not going to be interpreted as 
        // range definitions.  
        // still TODO: (possibly?) add more validation; figure out how 
        // to encode *date* ranges ("-" is not optimal, since dates already
        // contain dashes... although, since dates are (supposed to be) 
        // normalized it should still be possible to parse it unambiguously)
        //          -- L.A., v3.6

        if (rangeMode) {
            // after these steps, the string does not have range operators;
            // i.e., '-9--3', '--9', '-9-','-9', '-1-1', '1', '3-4', '6-'

            if ((tmp.get(0).equals("!")) && (tmp.get(1).equals("="))) {
                // != negation string is found
                token.put("start", "2");
                token.put("end", "");
                token.put("v1", StringUtils.join(tmp.subList(2, tmp.size()), ""));
                token.put("v2", "");
                dbgLog.fine("value=" + StringUtils.join(tmp.subList(2, tmp.size()), ","));

            } else if ((tmp.get(0).equals("-")) && (tmp.get(1).equals("-"))) {
                // type 2: --9
                token.put("v1", "");
                tmp.removeFirst();
                token.put("v2", StringUtils.join(tmp, ""));
            } else if ((tmp.get(0).equals("-")) && (tmp.getLast().equals("-"))) {
                // type 3: -9-
                token.put("v2", "");
                tmp.removeLast();
                token.put("v1", StringUtils.join(tmp, ""));
            } else if ((!tmp.get(0).equals("-")) && (tmp.getLast().equals("-"))) {
                // type 8: 6-
                token.put("v2", "");
                tmp.removeLast();
                token.put("v1", StringUtils.join(tmp, ""));
            } else {
                int count = 0;
                List<Integer> index = new ArrayList<Integer>();
                for (int j = 0; j < tmp.size(); j++) {
                    if (tmp.get(j).equals("-")) {
                        count++;
                        index.add(j);
                    }
                }

                if (count >= 2) {
                    // range type
                    // divide the second hyphen
                    // types 1 and 5: -9--3, -1-1
                    // token.put("v1", StringUtils.join(tmp[0..($index[1]-1)],"" ));
                    token.put("v2", StringUtils.join(tmp.subList((index.get(1) + 1), tmp.size()), ""));

                } else if (count == 1) {
                    if (tmp.get(0).equals("-")) {
                        // point negative type
                        // type 4: -9 or -inf,9
                        // do nothing
                        if ((token.get("start").equals("5"))
                                && ((token.get("end").equals("6")) || (token.get("end").equals("4")))) {
                            token.put("v1", "");
                            tmp.removeFirst();
                            token.put("v2", StringUtils.join(tmp, ""));
                        } else {
                            token.put("v1", StringUtils.join(tmp, ""));
                            token.put("v2", StringUtils.join(tmp, ""));
                        }
                    } else {
                        // type 7: 3-4
                        // both positive value and range type
                        String[] vset = (StringUtils.join(tmp, "")).split("-");
                        token.put("v1", vset[0]);
                        token.put("v2", vset[1]);
                    }

                } else {
                    // type 6: 1
                    token.put("v1", StringUtils.join(tmp, ""));
                    token.put("v2", StringUtils.join(tmp, ""));
                }
            }
        } else {
            // assume that this is NOT a range; treat the entire sequence 
            // of symbols as a single token:
            // type 6: 1
            token.put("v1", StringUtils.join(tmp, ""));
            token.put("v2", StringUtils.join(tmp, ""));
        }

        dbgLog.fine(i + "-th result=" + token.get("start") + "|" + token.get("v1") + "|" + token.get("end")
                + "|" + token.get("v2"));

        List<String> rangeSet = new ArrayList<String>();
        rangeSet.add(token.get("start"));
        rangeSet.add(token.get("v1"));
        rangeSet.add(token.get("end"));
        rangeSet.add(token.get("v2"));
        rangeData.add(rangeSet);

    }

    dbgLog.fine("rangeData:\n" + rangeData);
    return rangeData;
}

From source file:org.hyperic.hq.measurement.server.session.AvailabilityManagerImpl.java

private PageList<HighLowMetricValue> getPageList(List<AvailabilityDataRLE> availInfo, long begin, long end,
        long interval, boolean prependUnknowns) {
    PageList<HighLowMetricValue> rtn = new PageList<HighLowMetricValue>();
    for (Iterator<AvailabilityDataRLE> it = availInfo.iterator(); it.hasNext();) {
        AvailabilityDataRLE rle = it.next();
        long availStartime = rle.getStartime();
        long availEndtime = rle.getEndtime();
        //skip measurements that are before first time slot
        if (availEndtime < begin) {
            continue;
        }//from  w ww  .  j  a v a2  s. co  m
        LinkedList<AvailabilityDataRLE> queue = new LinkedList<AvailabilityDataRLE>();
        queue.add(rle);
        int i = 0;
        for (long curr = begin; curr <= end; curr += interval) {
            long next = curr + interval;
            next = (next > end) ? end : next;
            long endtime = ((AvailabilityDataRLE) queue.getFirst()).getEndtime();
            //while next time slot is after measurement time range
            while (next > endtime) {
                // it should not be the case that there are no more
                // avails in the array, but we need to handle it
                if (it.hasNext()) {
                    AvailabilityDataRLE tmp = (AvailabilityDataRLE) it.next();
                    queue.addFirst(tmp);
                    endtime = tmp.getEndtime();
                } else {
                    endtime = availEndtime;
                    int measId = rle.getMeasurement().getId().intValue();
                    String msg = "Measurement, " + measId + ", for interval " + begin + " - " + end
                            + " did not return a value for range " + curr + " - " + (curr + interval);
                    log.warn(msg);
                }
            }
            endtime = availEndtime;
            while (curr > endtime) {
                queue.removeLast();
                // this should not happen unless the above !it.hasNext()
                // else condition is true
                if (queue.size() == 0) {
                    rle = new AvailabilityDataRLE(rle.getMeasurement(), rle.getEndtime(), next, AVAIL_UNKNOWN);
                    queue.addLast(rle);
                }
                rle = (AvailabilityDataRLE) queue.getLast();
                availStartime = rle.getStartime();
                availEndtime = rle.getEndtime();
                endtime = availEndtime;
            }
            HighLowMetricValue val;
            if (curr >= availStartime) {
                val = getMetricValue(queue, curr);
            } else if (prependUnknowns) {
                val = new HighLowMetricValue(AVAIL_UNKNOWN, curr);
                val.incrementCount();
            } else {
                i++;
                continue;
            }
            if (rtn.size() <= i) {
                rtn.add(round(val));
            } else {
                updateMetricValue(val, (HighLowMetricValue) rtn.get(i));
            }
            i++;
        }
    }
    if (rtn.size() == 0) {
        rtn.addAll(getDefaultHistoricalAvail(end));
    }
    return rtn;
}

From source file:org.openxdm.xcap.client.test.error.CannotInsertTest.java

@Test
public void test() throws HttpException, IOException, JAXBException, InterruptedException {

    // create uri      
    UserDocumentUriKey key = new UserDocumentUriKey(appUsage.getAUID(), user, documentName);

    String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\">" + "<list name=\"friends\" />"
            + "</resource-lists>";

    // send put request and get response
    Response response = client.put(key, appUsage.getMimetype(), content, null);

    // check put response
    assertTrue("Put response must exists", response != null);
    assertTrue("Put response code should be 201", response.getCode() == 201);

    // create element selector      
    LinkedList<ElementSelectorStep> elementSelectorSteps = new LinkedList<ElementSelectorStep>();
    ElementSelectorStep step1 = new ElementSelectorStep("resource-lists");
    ElementSelectorStep step2 = new ElementSelectorStepByAttr("list", "name", "friends");
    elementSelectorSteps.add(step1);// w  w w . j  av  a 2 s  .  c o  m
    elementSelectorSteps.addLast(step2);
    ElementSelector elementSelector = new ElementSelector(elementSelectorSteps);

    // create exception for return codes
    CannotInsertConflictException exception = new CannotInsertConflictException();

    // 1st test is for attr replace

    // create attr uri      
    AttributeSelector attributeSelector = new AttributeSelector("name");
    UserAttributeUriKey attrKey = new UserAttributeUriKey(appUsage.getAUID(), user, documentName,
            elementSelector, attributeSelector, null);
    // send put and get response
    Response putResponse = client.put(attrKey, AttributeResource.MIMETYPE, "enemies", null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // 2nd is for elem replace

    // create elem uri
    UserElementUriKey elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName,
            elementSelector, null);

    // element content to put
    String elementContent = "<list name=\"enemies\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // element content to put
    elementContent = "<cannot-insert name=\"friends\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // 3rd is for new elem put by pos and attr

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStepByPosAttr("list", 1, "name", "3n3m13s");
    elementSelectorSteps.addLast(step2);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);

    // element content to put      
    elementContent = "<list name=\"enemies\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // element content to put
    elementContent = "<cannot-insert name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStepByPosAttr("list", 3, "name", "3n3m13s");
    elementSelectorSteps.addLast(step2);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);
    // element content to put      
    elementContent = "<list name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // 4th is for new elem put by pos

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStepByPos("list", 2);
    elementSelectorSteps.addLast(step2);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);

    // element content to put
    elementContent = "<cannot-insert name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStepByPos("list", 3);
    elementSelectorSteps.addLast(step2);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);
    // element content to put      
    elementContent = "<list name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // 5th is for new elem put by attr

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStepByAttr("list", "name", "3n3m13s");
    elementSelectorSteps.addLast(step2);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);

    // element content to put      
    elementContent = "<list name=\"enemies\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // element content to put
    elementContent = "<cannot-insert name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // 6th is for new elem put by name only

    // create elem uri
    elementSelectorSteps.removeLast();
    step2 = new ElementSelectorStep("list");
    elementSelectorSteps.addLast(step2);
    ElementSelectorStep step3 = new ElementSelectorStep("list");
    elementSelectorSteps.addLast(step3);
    elementKey = new UserElementUriKey(appUsage.getAUID(), user, documentName, elementSelector, null);

    // element content to put
    elementContent = "<cannot-insert name=\"3n3m13s\" />";
    // send put request and get response
    putResponse = client.put(elementKey, ElementResource.MIMETYPE, elementContent, null);
    assertTrue("Put response must exists", putResponse != null);
    assertTrue(
            "Put response content must be the expected and the response code should be "
                    + exception.getResponseStatus(),
            putResponse.getCode() == exception.getResponseStatus()
                    && putResponse.getContent().equals(exception.getResponseContent()));

    // clean up
    client.delete(key, null);
}