Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

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

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

From source file:org.pgptool.gui.encryption.implpgp.KeyFilesOperationsPgpImpl.java

@Override
public void exportPublicKey(Key key, String targetFilePathname) {
    Preconditions.checkArgument(key != null && key.getKeyData() != null && key.getKeyInfo() != null,
            "Key must be providedand fully described");
    Preconditions.checkArgument(StringUtils.hasText(targetFilePathname), "targetFilePathname must be provided");
    Stack<OutputStream> os = new Stack<>();
    try {//  ww w  . j a v  a2  s .  c  o m
        os.push(new FileOutputStream(targetFilePathname));
        if ("asc".equalsIgnoreCase(FilenameUtils.getExtension(targetFilePathname))) {
            os.push(new ArmoredOutputStream(os.peek()));
        }
        KeyDataPgp keyDataPgp = KeyDataPgp.get(key);
        if (keyDataPgp.getPublicKeyRing() != null) {
            keyDataPgp.getPublicKeyRing().encode(os.peek());
        } else {
            keyDataPgp.getSecretKeyRing().getPublicKey().encode(os.peek());
        }
    } catch (Throwable t) {
        throw new RuntimeException(
                "Failed to export public key " + key.getKeyInfo().getUser() + " to " + targetFilePathname, t);
    } finally {
        while (!os.isEmpty()) {
            IoStreamUtils.safeClose(os.pop());
        }
    }
}

From source file:org.pgptool.gui.encryption.implpgp.KeyFilesOperationsPgpImpl.java

@Override
public void exportPrivateKey(Key key, String targetFilePathname) {
    Preconditions.checkArgument(key != null && key.getKeyData() != null && key.getKeyInfo() != null,
            "Key must be providedand fully described");
    KeyDataPgp keyDataPgp = KeyDataPgp.get(key);
    Preconditions.checkArgument(keyDataPgp.getSecretKeyRing() != null, "KeyPair key wasn't provided");
    Preconditions.checkArgument(StringUtils.hasText(targetFilePathname), "targetFilePathname must be provided");
    Stack<OutputStream> os = new Stack<>();
    try {//from  w w w. ja  va  2  s .  c om
        os.push(new FileOutputStream(targetFilePathname));
        if ("asc".equalsIgnoreCase(FilenameUtils.getExtension(targetFilePathname))) {
            os.push(new ArmoredOutputStream(os.peek()));
        }
        keyDataPgp.getSecretKeyRing().encode(os.peek());
        if (keyDataPgp.getPublicKeyRing() != null) {
            keyDataPgp.getPublicKeyRing().encode(os.peek());
        }
    } catch (Throwable t) {
        throw new RuntimeException(
                "Failed to export private key " + key.getKeyInfo().getUser() + " to " + targetFilePathname, t);
    } finally {
        while (!os.isEmpty()) {
            IoStreamUtils.safeClose(os.pop());
        }
    }
}

From source file:com.dianping.cat.consumer.problem.ProblemReportMerger.java

@Override
protected void visitMachineChildren(Machine to, Machine from) {
    Stack<Object> objs = getObjects();

    for (Entry source : from.getEntries()) {
        Entry target = findOrCreateEntry(to, source);

        objs.push(target);
        source.accept(this);
        objs.pop();/*from  ww  w . j av  a  2  s .c o  m*/
    }
    for (Entity source : from.getEntities().values()) {
        Entity target = findOrCreateEntity(to, source);

        objs.push(target);
        source.accept(this);
        objs.pop();
    }
}

From source file:com.inmobi.conduit.distcp.tools.SimpleCopyListing.java

private void traverseNonEmptyDirectory(SequenceFile.Writer fileListWriter, FileStatus sourceStatus,
        Path sourcePathRoot, boolean localFile, DistCpOptions options) throws IOException {
    FileSystem sourceFS = sourcePathRoot.getFileSystem(getConf());
    Stack<FileStatus> pathStack = new Stack<FileStatus>();
    pathStack.push(sourceStatus);

    while (!pathStack.isEmpty()) {
        for (FileStatus child : getChildren(sourceFS, pathStack.pop())) {
            if (LOG.isDebugEnabled())
                LOG.debug("Recording source-path: " + sourceStatus.getPath() + " for copy.");
            writeToFileListing(fileListWriter, child, sourcePathRoot, localFile, options);
            if (isDirectoryAndNotEmpty(sourceFS, child)) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Traversing non-empty source dir: " + sourceStatus.getPath());
                pathStack.push(child);/*from   w  ww.j  a v a2 s. c  o  m*/
            }
        }
    }
}

From source file:com.webcohesion.ofx4j.io.tagsoup.TestTagSoupOFXReader.java

/**
 * tests using sax to parse an OFX doc.// w w  w.j  a v a2 s  .c  o m
 */
public void testVersion1() throws Exception {
    TagSoupOFXReader reader = new TagSoupOFXReader();
    final Map<String, String> headers = new HashMap<String, String>();
    final Stack<Map<String, Object>> aggregateStack = new Stack<Map<String, Object>>();
    TreeMap<String, Object> root = new TreeMap<String, Object>();
    aggregateStack.push(root);

    reader.setContentHandler(new DefaultHandler() {

        @Override
        public void onHeader(String name, String value) {
            LOG.debug(name + ":" + value);
            headers.put(name, value);
        }

        @Override
        public void onElement(String name, String value) {
            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + name + "=" + value);

            aggregateStack.peek().put(name, value);
        }

        @Override
        public void startAggregate(String aggregateName) {
            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + aggregateName + " {");

            TreeMap<String, Object> aggregate = new TreeMap<String, Object>();
            aggregateStack.peek().put(aggregateName, aggregate);
            aggregateStack.push(aggregate);
        }

        @Override
        public void endAggregate(String aggregateName) {
            aggregateStack.pop();

            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + "}");
        }
    });
    reader.parse(TestNanoXMLOFXReader.class.getResourceAsStream("example-response.ofx"));
    assertEquals(9, headers.size());
    assertEquals(1, aggregateStack.size());
    assertSame(root, aggregateStack.pop());
}

From source file:com.webcohesion.ofx4j.io.tagsoup.TestTagSoupOFXReader.java

/**
 * tests using sax to parse an OFX doc./*from   w w  w  . ja v a2 s .  com*/
 */
public void testSimpleVersion1() throws Exception {
    TagSoupOFXReader reader = new TagSoupOFXReader();
    final Map<String, String> headers = new HashMap<String, String>();
    final Stack<Map<String, Object>> aggregateStack = new Stack<Map<String, Object>>();
    TreeMap<String, Object> root = new TreeMap<String, Object>();
    aggregateStack.push(root);

    reader.setContentHandler(new DefaultHandler() {

        @Override
        public void onHeader(String name, String value) {
            LOG.debug(name + ":" + value);
            headers.put(name, value);
        }

        @Override
        public void onElement(String name, String value) {
            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + name + "=" + value);

            aggregateStack.peek().put(name, value);
        }

        @Override
        public void startAggregate(String aggregateName) {
            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + aggregateName + " {");

            TreeMap<String, Object> aggregate = new TreeMap<String, Object>();
            aggregateStack.peek().put(aggregateName, aggregate);
            aggregateStack.push(aggregate);
        }

        @Override
        public void endAggregate(String aggregateName) {
            aggregateStack.pop();

            char[] tabs = new char[aggregateStack.size() * 2];
            Arrays.fill(tabs, ' ');
            LOG.debug(new String(tabs) + "}");
        }
    });
    reader.parse(TestNanoXMLOFXReader.class.getResourceAsStream("simple.ofx"));
    assertEquals(9, headers.size());
    assertEquals(1, aggregateStack.size());
    assertSame(root, aggregateStack.pop());
}

From source file:com.pinterest.hdfsbackup.distcp.DistCp.java

/** Delete the dst files/dirs which do not exist in src */
static private void deleteNonexisting(FileSystem dstfs, FileStatus dstroot, Path dstsorted, FileSystem jobfs,
        Path jobdir, JobConf jobconf, Configuration conf) throws IOException {
    if (!dstroot.isDir()) {
        throw new IOException("dst must be a directory when option " + Options.DELETE.cmd
                + " is set, but dst (= " + dstroot.getPath() + ") is not a directory.");
    }/*ww w . j  a v  a2 s  .c  o m*/

    //write dst lsr results
    final Path dstlsr = new Path(jobdir, "_distcp_dst_lsr");
    final SequenceFile.Writer writer = SequenceFile.createWriter(jobfs, jobconf, dstlsr, Text.class,
            FileStatus.class, SequenceFile.CompressionType.NONE);
    try {
        //do lsr to get all file statuses in dstroot
        final Stack<FileStatus> lsrstack = new Stack<FileStatus>();
        for (lsrstack.push(dstroot); !lsrstack.isEmpty();) {
            final FileStatus status = lsrstack.pop();
            if (status.isDir()) {
                for (FileStatus child : dstfs.listStatus(status.getPath())) {
                    String relative = makeRelative(dstroot.getPath(), child.getPath());
                    writer.append(new Text(relative), child);
                    lsrstack.push(child);
                }
            }
        }
    } finally {
        checkAndClose(writer);
    }

    //sort lsr results
    final Path sortedlsr = new Path(jobdir, "_distcp_dst_lsr_sorted");
    SequenceFile.Sorter sorter = new SequenceFile.Sorter(jobfs, new Text.Comparator(), Text.class,
            FileStatus.class, jobconf);
    sorter.sort(dstlsr, sortedlsr);

    //compare lsr list and dst list
    SequenceFile.Reader lsrin = null;
    SequenceFile.Reader dstin = null;
    try {
        lsrin = new SequenceFile.Reader(jobfs, sortedlsr, jobconf);
        dstin = new SequenceFile.Reader(jobfs, dstsorted, jobconf);

        //compare sorted lsr list and sorted dst list
        final Text lsrpath = new Text();
        final FileStatus lsrstatus = new FileStatus();
        final Text dstpath = new Text();
        final Text dstfrom = new Text();
        final FsShell shell = new FsShell(conf);
        final String[] shellargs = { "-rmr", null };

        boolean hasnext = dstin.next(dstpath, dstfrom);
        for (; lsrin.next(lsrpath, lsrstatus);) {
            int dst_cmp_lsr = dstpath.compareTo(lsrpath);
            for (; hasnext && dst_cmp_lsr < 0;) {
                hasnext = dstin.next(dstpath, dstfrom);
                dst_cmp_lsr = dstpath.compareTo(lsrpath);
            }

            if (dst_cmp_lsr == 0) {
                //lsrpath exists in dst, skip it
                hasnext = dstin.next(dstpath, dstfrom);
            } else {
                //lsrpath does not exist, delete it
                String s = new Path(dstroot.getPath(), lsrpath.toString()).toString();
                if (shellargs[1] == null || !isAncestorPath(shellargs[1], s)) {
                    shellargs[1] = s;
                    int r = 0;
                    try {
                        r = shell.run(shellargs);
                    } catch (Exception e) {
                        throw new IOException("Exception from shell.", e);
                    }
                    if (r != 0) {
                        throw new IOException(
                                "\"" + shellargs[0] + " " + shellargs[1] + "\" returns non-zero value " + r);
                    }
                }
            }
        }
    } finally {
        checkAndClose(lsrin);
        checkAndClose(dstin);
    }
}

From source file:ParenMatcher.java

public void run() {
    StyledDocument doc = getStyledDocument();
    String text = "";
    int len = doc.getLength();
    try {//w ww  .  j  a  v a  2 s . com
        text = doc.getText(0, len);
    } catch (BadLocationException ble) {
    }
    java.util.Stack stack = new java.util.Stack();
    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (ch == '(' || ch == '[' || ch == '{') {
            int depth = stack.size();
            stack.push("" + ch + j); // push a String containg the char and
            // the offset
            AttributeSet aset = matchAttrSet[depth % matchAttrSet.length];
            doc.setCharacterAttributes(j, 1, aset, false);
        }
        if (ch == ')' || ch == ']' || ch == '}') {
            String peek = stack.empty() ? "." : (String) stack.peek();
            if (matches(peek.charAt(0), ch)) { // does it match?
                stack.pop();
                int depth = stack.size();
                AttributeSet aset = matchAttrSet[depth % matchAttrSet.length];
                doc.setCharacterAttributes(j, 1, aset, false);
            } else { // mismatch
                doc.setCharacterAttributes(j, 1, badAttrSet, false);
            }
        }
    }

    while (!stack.empty()) { // anything left in the stack is a mismatch
        String pop = (String) stack.pop();
        int offset = Integer.parseInt(pop.substring(1));
        doc.setCharacterAttributes(offset, 1, badAttrSet, false);
    }
}

From source file:io.fabric8.maven.enricher.fabric8.AbstractLiveEnricher.java

/**
 * Creates an Iterable to walk the exception from the bottom up
 * (the last caused by going upwards to the root exception).
 *
 * @param exception the exception/*from w  w w  . j av a2 s  .  co  m*/
 * @return the Iterable
 * @see java.lang.Iterable
 */
protected Stack<Throwable> unfoldExceptions(Throwable exception) {
    Stack<Throwable> throwables = new Stack<>();

    Throwable current = exception;
    // spool to the bottom of the caused by tree
    while (current != null) {
        throwables.push(current);
        current = current.getCause();
    }
    return throwables;
}

From source file:com.madrobot.di.wizard.json.JSONDeserializer.java

/**
 * Deserializes the JSON data from the input to the corresponding entity type <br/>
 * If there is an error while parsing, if possible it will try to ignore it, otherwise returns a null value.
 * //  www .j a v a  2s .  c  om
 * @param parser
 *            Parser to read XML from
 * @param objType
 *            Type of the entity to deserialize data to
 * 
 * @return Deserialized object, if successful, null otherwise
 * @see #deserialize(InputStream, Class)
 */
public <T> T deserialize(final Class<T> objType, final JSONObject jsonObject) throws JSONException {

    try {

        Stack<Class<?>> stack = new Stack<Class<?>>();
        stack.push(objType);

        T resultObject = objType.newInstance();

        deserialize(resultObject, jsonObject, stack);

        return resultObject;
    } catch (IllegalAccessException e) {
        Log.e(TAG, e.getMessage());
    } catch (InstantiationException e) {
        Log.e(TAG, e.getMessage());
    }

    return null;
}