Search in sources :

Example 1 with Stack

use of java.util.Stack in project buck by facebook.

the class WorkspaceGenerator method writeWorkspace.

public Path writeWorkspace() throws IOException {
    DocumentBuilder docBuilder;
    Transformer transformer;
    try {
        docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (ParserConfigurationException | TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    DOMImplementation domImplementation = docBuilder.getDOMImplementation();
    final Document doc = domImplementation.createDocument(/* namespaceURI */
    null, "Workspace", /* docType */
    null);
    doc.setXmlVersion("1.0");
    Element rootElem = doc.getDocumentElement();
    rootElem.setAttribute("version", "1.0");
    final Stack<Element> groups = new Stack<>();
    groups.push(rootElem);
    FileVisitor<Map.Entry<String, WorkspaceNode>> visitor = new FileVisitor<Map.Entry<String, WorkspaceNode>>() {

        @Override
        public FileVisitResult preVisitDirectory(Map.Entry<String, WorkspaceNode> dir, BasicFileAttributes attrs) throws IOException {
            Preconditions.checkArgument(dir.getValue() instanceof WorkspaceGroup);
            Element element = doc.createElement("Group");
            element.setAttribute("location", "container:");
            element.setAttribute("name", dir.getKey());
            groups.peek().appendChild(element);
            groups.push(element);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Map.Entry<String, WorkspaceNode> file, BasicFileAttributes attrs) throws IOException {
            Preconditions.checkArgument(file.getValue() instanceof WorkspaceFileRef);
            WorkspaceFileRef fileRef = (WorkspaceFileRef) file.getValue();
            Element element = doc.createElement("FileRef");
            element.setAttribute("location", "container:" + MorePaths.relativize(MorePaths.normalize(outputDirectory), fileRef.getPath()).toString());
            groups.peek().appendChild(element);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Map.Entry<String, WorkspaceNode> file, IOException exc) throws IOException {
            return FileVisitResult.TERMINATE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Map.Entry<String, WorkspaceNode> dir, IOException exc) throws IOException {
            groups.pop();
            return FileVisitResult.CONTINUE;
        }
    };
    walkNodeTree(visitor);
    Path projectWorkspaceDir = getWorkspaceDir();
    projectFilesystem.mkdirs(projectWorkspaceDir);
    Path serializedWorkspace = projectWorkspaceDir.resolve("contents.xcworkspacedata");
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(outputStream);
        transformer.transform(source, result);
        String contentsToWrite = outputStream.toString();
        if (MoreProjectFilesystems.fileContentsDiffer(new ByteArrayInputStream(contentsToWrite.getBytes(Charsets.UTF_8)), serializedWorkspace, projectFilesystem)) {
            projectFilesystem.writeContentsToPath(contentsToWrite, serializedWorkspace);
        }
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    Path xcshareddata = projectWorkspaceDir.resolve("xcshareddata");
    projectFilesystem.mkdirs(xcshareddata);
    Path workspaceSettingsPath = xcshareddata.resolve("WorkspaceSettings.xcsettings");
    String workspaceSettings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"" + " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n" + "<dict>\n" + "\t<key>IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded</key>\n" + "\t<false/>\n" + "</dict>\n" + "</plist>";
    projectFilesystem.writeContentsToPath(workspaceSettings, workspaceSettingsPath);
    return projectWorkspaceDir;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document) FileVisitor(java.nio.file.FileVisitor) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) TransformerException(javax.xml.transform.TransformerException) Path(java.nio.file.Path) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Stack(java.util.Stack) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 2 with Stack

use of java.util.Stack in project buck by facebook.

the class CxxBoostTest method parseResults.

@Override
protected ImmutableList<TestResultSummary> parseResults(Path exitCode, Path output, Path results) throws Exception {
    ImmutableList.Builder<TestResultSummary> summariesBuilder = ImmutableList.builder();
    // Process the test run output to grab the individual test stdout/stderr and
    // test run times.
    Map<String, String> messages = Maps.newHashMap();
    Map<String, List<String>> stdout = Maps.newHashMap();
    Map<String, Long> times = Maps.newHashMap();
    try (BufferedReader reader = Files.newBufferedReader(output, Charsets.US_ASCII)) {
        Stack<String> testSuite = new Stack<>();
        Optional<String> currentTest = Optional.empty();
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher matcher;
            if ((matcher = SUITE_START.matcher(line)).matches()) {
                String suite = matcher.group(1);
                testSuite.push(suite);
            } else if ((matcher = SUITE_END.matcher(line)).matches()) {
                String suite = matcher.group(1);
                Preconditions.checkState(testSuite.peek().equals(suite));
                testSuite.pop();
            } else if ((matcher = CASE_START.matcher(line)).matches()) {
                String test = Joiner.on(".").join(testSuite) + "." + matcher.group(1);
                currentTest = Optional.of(test);
                stdout.put(test, Lists.newArrayList());
            } else if ((matcher = CASE_END.matcher(line)).matches()) {
                String test = Joiner.on(".").join(testSuite) + "." + matcher.group(1);
                Preconditions.checkState(currentTest.isPresent() && currentTest.get().equals(test));
                String time = matcher.group(2);
                times.put(test, time == null ? 0 : Long.valueOf(time));
                currentTest = Optional.empty();
            } else if (currentTest.isPresent()) {
                if (ERROR.matcher(line).matches()) {
                    messages.put(currentTest.get(), line);
                } else {
                    Preconditions.checkNotNull(stdout.get(currentTest.get())).add(line);
                }
            }
        }
    }
    // Parse the XML result file for the actual test result summaries.
    Document doc = XmlDomParser.parse(results);
    Node testResult = doc.getElementsByTagName("TestResult").item(0);
    Node testSuite = testResult.getFirstChild();
    visitTestSuite(summariesBuilder, messages, stdout, times, "", testSuite);
    return summariesBuilder.build();
}
Also used : Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Node(org.w3c.dom.Node) TestResultSummary(com.facebook.buck.test.TestResultSummary) Document(org.w3c.dom.Document) Stack(java.util.Stack) BufferedReader(java.io.BufferedReader) ImmutableList(com.google.common.collect.ImmutableList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 3 with Stack

use of java.util.Stack in project tomcat by apache.

the class JspC method scanFiles.

/**
     * Locate all jsp files in the webapp. Used if no explicit
     * jsps are specified.
     * @param base Base path
     */
public void scanFiles(File base) {
    Stack<String> dirs = new Stack<>();
    dirs.push(base.toString());
    // Make sure default extensions are always included
    if ((getExtensions() == null) || (getExtensions().size() < 2)) {
        addExtension("jsp");
        addExtension("jspx");
    }
    while (!dirs.isEmpty()) {
        String s = dirs.pop();
        File f = new File(s);
        if (f.exists() && f.isDirectory()) {
            String[] files = f.list();
            String ext;
            for (int i = 0; (files != null) && i < files.length; i++) {
                File f2 = new File(s, files[i]);
                if (f2.isDirectory()) {
                    dirs.push(f2.getPath());
                } else {
                    String path = f2.getPath();
                    String uri = path.substring(uriRoot.length());
                    ext = files[i].substring(files[i].lastIndexOf('.') + 1);
                    if (getExtensions().contains(ext) || jspConfig.isJspPage(uri)) {
                        pages.add(path);
                    }
                }
            }
        }
    }
}
Also used : File(java.io.File) Stack(java.util.Stack)

Example 4 with Stack

use of java.util.Stack in project hive by apache.

the class ParseUtils method sameTree.

public static boolean sameTree(ASTNode node, ASTNode otherNode) {
    if (node == null && otherNode == null) {
        return true;
    }
    if ((node == null && otherNode != null) || (node != null && otherNode == null)) {
        return false;
    }
    Stack<Tree> stack = new Stack<Tree>();
    stack.push(node);
    Stack<Tree> otherStack = new Stack<Tree>();
    otherStack.push(otherNode);
    while (!stack.empty() && !otherStack.empty()) {
        Tree p = stack.pop();
        Tree otherP = otherStack.pop();
        if (p.isNil() != otherP.isNil()) {
            return false;
        }
        if (!p.isNil()) {
            if (!p.toString().equals(otherP.toString())) {
                return false;
            }
        }
        if (p.getChildCount() != otherP.getChildCount()) {
            return false;
        }
        for (int i = p.getChildCount() - 1; i >= 0; i--) {
            Tree t = p.getChild(i);
            stack.push(t);
            Tree otherT = otherP.getChild(i);
            otherStack.push(otherT);
        }
    }
    return stack.empty() && otherStack.empty();
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree) Stack(java.util.Stack)

Example 5 with Stack

use of java.util.Stack in project MaterialDrawer by mikepenz.

the class AccountHeaderBuilder method calculateProfiles.

/**
     * helper method to calculate the order of the profiles
     */
protected void calculateProfiles() {
    if (mProfiles == null) {
        mProfiles = new ArrayList<>();
    }
    if (mCurrentProfile == null) {
        int setCount = 0;
        for (int i = 0; i < mProfiles.size(); i++) {
            if (mProfiles.size() > i && mProfiles.get(i).isSelectable()) {
                if (setCount == 0 && (mCurrentProfile == null)) {
                    mCurrentProfile = mProfiles.get(i);
                } else if (setCount == 1 && (mProfileFirst == null)) {
                    mProfileFirst = mProfiles.get(i);
                } else if (setCount == 2 && (mProfileSecond == null)) {
                    mProfileSecond = mProfiles.get(i);
                } else if (setCount == 3 && (mProfileThird == null)) {
                    mProfileThird = mProfiles.get(i);
                }
                setCount++;
            }
        }
        return;
    }
    IProfile[] previousActiveProfiles = new IProfile[] { mCurrentProfile, mProfileFirst, mProfileSecond, mProfileThird };
    IProfile[] newActiveProfiles = new IProfile[4];
    Stack<IProfile> unusedProfiles = new Stack<>();
    // try to keep existing active profiles in the same positions
    for (int i = 0; i < mProfiles.size(); i++) {
        IProfile p = mProfiles.get(i);
        if (p.isSelectable()) {
            boolean used = false;
            for (int j = 0; j < 4; j++) {
                if (previousActiveProfiles[j] == p) {
                    newActiveProfiles[j] = p;
                    used = true;
                    break;
                }
            }
            if (!used) {
                unusedProfiles.push(p);
            }
        }
    }
    Stack<IProfile> activeProfiles = new Stack<>();
    // try to fill the gaps with new available profiles
    for (int i = 0; i < 4; i++) {
        if (newActiveProfiles[i] != null) {
            activeProfiles.push(newActiveProfiles[i]);
        } else if (!unusedProfiles.isEmpty()) {
            activeProfiles.push(unusedProfiles.pop());
        }
    }
    Stack<IProfile> reversedActiveProfiles = new Stack<>();
    while (!activeProfiles.empty()) {
        reversedActiveProfiles.push(activeProfiles.pop());
    }
    // reassign active profiles
    if (reversedActiveProfiles.isEmpty()) {
        mCurrentProfile = null;
    } else {
        mCurrentProfile = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileFirst = null;
    } else {
        mProfileFirst = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileSecond = null;
    } else {
        mProfileSecond = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileThird = null;
    } else {
        mProfileThird = reversedActiveProfiles.pop();
    }
}
Also used : IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) Stack(java.util.Stack)

Aggregations

Stack (java.util.Stack)705 ArrayList (java.util.ArrayList)137 HashSet (java.util.HashSet)82 IOException (java.io.IOException)56 File (java.io.File)52 List (java.util.List)45 Test (org.junit.Test)42 HashMap (java.util.HashMap)37 Map (java.util.Map)32 Set (java.util.Set)30 Paint (android.graphics.Paint)29 LinkedList (java.util.LinkedList)27 Iterator (java.util.Iterator)24 Matrix (android.graphics.Matrix)22 Path (android.graphics.Path)22 RectF (android.graphics.RectF)22 Scanner (java.util.Scanner)20 TreeSet (java.util.TreeSet)20 View (android.view.View)18 Document (org.w3c.dom.Document)18