Example usage for java.util Stack pop

List of usage examples for java.util Stack pop

Introduction

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

Prototype

public synchronized E pop() 

Source Link

Document

Removes the object at the top of this stack and returns that object as the value of this function.

Usage

From source file:com.baifendian.swordfish.execserver.parameter.placeholder.CalculateUtil.java

/**
 * ???/*  ww  w . ja v a  2  s.  c  o  m*/
 *
 * @param inOrderList
 * @return ??
 */
private static List<String> getPostOrder(List<String> inOrderList) {
    List<String> result = new ArrayList<>();
    Stack<String> stack = new Stack<>();

    for (int i = 0; i < inOrderList.size(); i++) {
        if (Character.isDigit(inOrderList.get(i).charAt(0))) {
            result.add(inOrderList.get(i));
        } else {
            switch (inOrderList.get(i).charAt(0)) {
            case '(':
                stack.push(inOrderList.get(i));
                break;
            case ')':
                while (!stack.peek().equals("(")) {
                    result.add(stack.pop());
                }
                stack.pop();
                break;
            default:
                while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))) {
                    result.add(stack.pop());
                }
                stack.push(inOrderList.get(i));
                break;
            }
        }
    }

    while (!stack.isEmpty()) {
        result.add(stack.pop());
    }

    return result;
}

From source file:com.jetbrains.pluginUtils.xml.JDOMXIncluder.java

@NotNull
private static List<Content> parseRemote(final Stack<String> bases, final URL remote,
        @Nullable Element fallbackElement) {
    try {/* ww w .j  a v a 2s  . c  om*/
        Document doc = JDOMUtil.loadResourceDocument(remote);
        bases.push(remote.toExternalForm());

        Element root = doc.getRootElement();

        final List<Content> list = resolve(root, bases);

        bases.pop();
        return list;
    } catch (JDOMException e) {
        throw new XIncludeException(e);
    } catch (IOException e) {
        if (fallbackElement != null) {
            // TODO[yole] return contents of fallback element (we don't have fallback elements with content ATM)
            return Collections.emptyList();
        }
        throw new XIncludeException(e);
    }
}

From source file:opennlp.tools.parse_thicket.kernel_interface.BracesProcessor.java

public static boolean isParenthesisMatch(String str) {
    Stack<Character> stack = new Stack<Character>();

    char c;/* w  w  w  .j a v  a  2 s.com*/
    for (int i = 0; i < str.length(); i++) {
        c = str.charAt(i);

        if (c == '{')
            return false;

        if (c == '(')
            stack.push(c);

        if (c == '{') {
            stack.push(c);
            if (c == '}')
                if (stack.empty())
                    return false;
                else if (stack.peek() == '{')
                    stack.pop();
        } else if (c == ')')
            if (stack.empty())
                return false;
            else if (stack.peek() == '(')
                stack.pop();
            else
                return false;
    }
    return stack.empty();
}

From source file:Main.java

public static String getXPath(Node node) {
    if (null == node)
        return null;

    // declarations
    Node parent = null;//  ww w  . j a  va2 s.  com
    Stack<Node> hierarchy = new Stack<Node>();
    StringBuilder buffer = new StringBuilder();

    // push element on stack
    hierarchy.push(node);

    parent = node.getParentNode();
    while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
        // push on stack
        hierarchy.push(parent);

        // get parent of parent
        parent = parent.getParentNode();
    }

    // construct xpath
    Object obj = null;
    while (!hierarchy.isEmpty() && null != (obj = hierarchy.pop())) {
        Node n = (Node) obj;
        boolean handled = false;

        // only consider elements
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) n;

            // is this the root element?
            if (buffer.length() == 0) {
                // root element - simply append element name
                buffer.append(n.getNodeName());
            } else {
                // child element - append slash and element name
                buffer.append("/");
                buffer.append(n.getNodeName());

                if (n.hasAttributes()) {
                    // see if the element has a name or id attribute
                    if (e.hasAttribute("id")) {
                        // id attribute found - use that
                        buffer.append("[@id='" + e.getAttribute("id") + "']");
                        handled = true;
                    } else if (e.hasAttribute("name")) {
                        // name attribute found - use that
                        buffer.append("[@name='" + e.getAttribute("name") + "']");
                        handled = true;
                    }
                }

                if (!handled) {
                    // no known attribute we could use - get sibling index
                    int prev_siblings = 1;
                    Node prev_sibling = n.getPreviousSibling();
                    while (null != prev_sibling) {
                        if (prev_sibling.getNodeType() == n.getNodeType()) {
                            if (prev_sibling.getNodeName().equalsIgnoreCase(n.getNodeName())) {
                                prev_siblings++;
                            }
                        }
                        prev_sibling = prev_sibling.getPreviousSibling();
                    }
                    buffer.append("[" + prev_siblings + "]");
                }
            }
        }
    }

    // return buffer
    return buffer.toString();
}

From source file:graphene.util.fs.FileUtils.java

public static void deleteRecursively(final File directory) throws IOException {
        final Stack<File> stack = new Stack<>();
        final List<File> temp = new LinkedList<>();
        stack.push(directory.getAbsoluteFile());
        while (!stack.isEmpty()) {
            final File top = stack.pop();
            File[] files = top.listFiles();
            if (files != null) {
                for (final File child : files) {
                    if (child.isFile()) {
                        if (!deleteFile(child)) {
                            throw new IOException("Failed to delete " + child.getCanonicalPath());
                        }/*from www . j  a  va  2s  .  c o m*/
                    } else {
                        temp.add(child);
                    }
                }
            }
            files = top.listFiles();
            if ((files == null) || (files.length == 0)) {
                if (!deleteFile(top)) {
                    throw new IOException("Failed to delete " + top.getCanonicalPath());
                }
            } else {
                stack.push(top);
                for (final File f : temp) {
                    stack.push(f);
                }
            }
            temp.clear();
        }
    }

From source file:de.tudarmstadt.ukp.csniper.webapp.search.tgrep.PennTreeUtils.java

public static PennTreeNode parsePennTree(String aTree) {
    StringTokenizer st = new StringTokenizer(aTree, "() ", true);

    PennTreeNode root = null;//from   w w w . java 2  s .  c o  m
    Stack<PennTreeNode> stack = new Stack<PennTreeNode>();
    boolean seenLabel = false;
    int i = 0;
    while (st.hasMoreTokens()) {
        String t = st.nextToken().trim();
        if (t.length() == 0) {
            // Skip
        } else if ("(".equals(t)) {
            PennTreeNode n = new PennTreeNode();
            stack.push(n);
            if (root == null) {
                root = n;
            }
            seenLabel = false;
        } else if (")".equals(t)) {
            PennTreeNode n = stack.pop();
            if (!stack.isEmpty()) {
                PennTreeNode p = stack.peek();
                p.addChild(n);
            }
        } else if (seenLabel) {
            // If the node has two labels, its a leaf, add a new terminal node then.
            PennTreeNode p = stack.peek();
            PennTreeNode n = new PennTreeNode();
            n.setTokenIndex(i);
            i++;
            n.setLabel(t);
            p.addChild(n);
        } else {
            PennTreeNode n = stack.peek();
            n.setLabel(t);
            seenLabel = true;
        }
    }

    return root;
}

From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java

public static JSONObject changeJSONResult(String rawString, String context, List<JSONDecorator> decs)
        throws UnsupportedEncodingException, JSONException {

    //List<JSONDecorator> decs = this.jsonDecoratorAggregates.getDecorators();
    List<JSONArray> docsArrays = new ArrayList<JSONArray>();

    JSONObject resultJSONObject = new JSONObject(rawString);
    Stack<JSONObject> prcStack = new Stack<JSONObject>();
    prcStack.push(resultJSONObject);/*ww w  .j a  va 2s .  com*/
    while (!prcStack.isEmpty()) {
        JSONObject popped = prcStack.pop();
        //Iterator keys = popped.keys();
        for (Iterator keys = popped.keys(); keys.hasNext();) {
            Object kobj = (Object) keys.next();
            String key = (String) kobj;
            Object obj = popped.get(key);
            boolean docsKey = key.equals("docs");
            if (docsKey && (obj instanceof JSONArray)) {
                docsArrays.add((JSONArray) obj);
            }
            if (obj instanceof JSONObject) {
                prcStack.push((JSONObject) obj);
            }
            if (obj instanceof JSONArray) {
                JSONArray arr = (JSONArray) obj;
                for (int i = 0, ll = arr.length(); i < ll; i++) {
                    Object arrObj = arr.get(i);
                    if (arrObj instanceof JSONObject) {
                        prcStack.push((JSONObject) arrObj);
                    }

                }
            }

        }
    }

    for (JSONArray docs : docsArrays) {
        for (int i = 0, ll = docs.length(); i < ll; i++) {
            JSONObject docJSON = (JSONObject) docs.get(i);
            // check master pid
            changeMasterPidInJSON(docJSON);

            // fiter protected fields
            filterFieldsInJSON(docJSON);

            // decorators
            decorators(context, decs, docJSON);

            // replace pids
            replacePidsInJSON(docJSON);
        }
    }
    return resultJSONObject;
}

From source file:Main.java

public static boolean parseXMLMessage(String msg, HashMap<String, String> strMap) {
    boolean bRet = true;

    // parse xml document.
    XmlPullParserFactory factory;//from w  w w.  j  a v  a  2 s. c o  m
    XmlPullParser parser;

    try {
        factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        Stack eleStack = new Stack();
        int parserEvent;

        parser.setInput(new StringReader(msg));
        parserEvent = parser.getEventType();

        while (parserEvent != XmlPullParser.END_DOCUMENT) {
            switch (parserEvent) {
            case XmlPullParser.START_TAG:
                String newtag = parser.getName();
                if (newtag.compareTo("xml") != 0) {
                    eleStack.push(newtag);
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getName().compareTo("xml") != 0) {
                    eleStack.pop();
                }
                break;
            case XmlPullParser.TEXT:
                String tagkey = "";
                for (int i = 0; i < eleStack.size(); i++) {
                    tagkey += eleStack.elementAt(i);
                    if (i < eleStack.size() - 1)
                        tagkey += "_";
                }
                strMap.put(tagkey, parser.getText());
                break;
            default:
                break;
            }

            parserEvent = parser.next();
        }

        eleStack = null;
        parser = null;
        factory = null;
    } catch (Exception e) {
        e.printStackTrace();
        bRet = false;
    }

    return bRet;
}

From source file:de.mpg.escidoc.services.citationmanager.utils.XsltHelper.java

/**
 * Check of the balanced tags sup/sub/*ww  w .j  av  a2 s  . co m*/
 * @param snippet
 * @return <code>true</code> if balanced, <code>false</code> otherwise 
 */
public static boolean isBalanced(String snippet) {
    if (snippet == null)
        return true; //????

    Stack<String> s = new Stack<String>();
    Matcher m = SUBS_OR_SUPS.matcher(snippet);
    while (m.find()) {
        String tag = m.group(1);
        if (tag.toLowerCase().startsWith("su")) {
            s.push(tag);
        } else {
            if (s.empty() || !tag.equals("/" + s.pop())) {
                return false;
            }
        }
    }

    return s.empty();
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.penntree.PennTreeUtils.java

public static PennTreeNode parsePennTree(String aTree) {
    StringTokenizer st = new StringTokenizer(aTree, "() ", true);

    PennTreeNode root = null;/*w  w w.j  a va  2  s . c  om*/
    Stack<PennTreeNode> stack = new Stack<PennTreeNode>();
    boolean seenLabel = false;

    while (st.hasMoreTokens()) {
        String t = st.nextToken().trim();
        if (t.length() == 0) {
            // Skip
        } else if ("(".equals(t)) {
            PennTreeNode n = new PennTreeNode();
            stack.push(n);
            if (root == null) {
                root = n;
            }
            seenLabel = false;
        } else if (")".equals(t)) {
            PennTreeNode n = stack.pop();
            if (!stack.isEmpty()) {
                PennTreeNode p = stack.peek();
                p.addChild(n);
            }
        } else if (seenLabel) {
            // If the node has two labels, its a leaf, add a new terminal node then.
            PennTreeNode p = stack.peek();
            PennTreeNode n = new PennTreeNode();
            n.setLabel(t);
            p.addChild(n);
        } else {
            PennTreeNode n = stack.peek();
            n.setLabel(t);
            seenLabel = true;
        }
    }

    return root;
}