Search in sources :

Example 71 with LineNumberReader

use of java.io.LineNumberReader in project drools by kiegroup.

the class DSLTokenizedMappingFile method readFile.

/**
 * Read a DSL file and convert it to a String. Comment lines are removed.
 * Split lines are joined, inserting a space for an EOL, but maintaining the
 * original number of lines by inserting EOLs. Options are recognized.
 * Keeps track of original line lengths for fixing parser error messages.
 * @param reader for the DSL file data
 * @return the transformed DSL file
 * @throws IOException
 */
private String readFile(Reader reader) throws IOException {
    lineLengths = new ArrayList<Integer>();
    lineLengths.add(null);
    LineNumberReader lnr = new LineNumberReader(reader);
    StringBuilder sb = new StringBuilder();
    int nlCount = 0;
    boolean inEntry = false;
    String line;
    while ((line = lnr.readLine()) != null) {
        lineLengths.add(line.length());
        Matcher commentMat = commentPat.matcher(line);
        if (commentMat.matches()) {
            if (inEntry) {
                nlCount++;
            } else {
                sb.append('\n');
            }
            if ("#/".equals(commentMat.group(2))) {
                String[] options = commentMat.group(1).substring(2).trim().split("\\s+");
                for (String option : options) {
                    optionSet.add(option);
                }
            }
            continue;
        }
        if (entryPat.matcher(line).matches()) {
            if (inEntry) {
                for (int i = 0; i < nlCount; i++) sb.append('\n');
            }
            sb.append(line);
            nlCount = 1;
            inEntry = true;
            continue;
        }
        sb.append(' ').append(line);
        nlCount++;
    }
    if (inEntry)
        sb.append('\n');
    lnr.close();
    return sb.toString();
}
Also used : Matcher(java.util.regex.Matcher) LineNumberReader(java.io.LineNumberReader)

Example 72 with LineNumberReader

use of java.io.LineNumberReader in project checker-framework by typetools.

the class FactoryTestChecker method buildExpected.

/**
 * Builds the expected type for the trees from the source file of the tree compilation unit.
 */
// This method is extremely ugly
private Map<TreeSpec, String> buildExpected(CompilationUnitTree tree) {
    Map<TreeSpec, String> expected = new HashMap<TreeSpec, String>();
    try {
        JavaFileObject o = tree.getSourceFile();
        File sourceFile = new File(o.toUri());
        LineNumberReader reader = new LineNumberReader(new FileReader(sourceFile));
        String line = reader.readLine();
        Pattern prevsubtreePattern = Pattern.compile("\\s*///(.*)-:-(.*)");
        Pattern prevfulltreePattern = Pattern.compile("\\s*///(.*)");
        Pattern subtreePattern = Pattern.compile("(.*)///(.*)-:-(.*)");
        Pattern fulltreePattern = Pattern.compile("(.*)///(.*)");
        while (line != null) {
            Matcher prevsubtreeMatcher = prevsubtreePattern.matcher(line);
            Matcher prevfulltreeMatcher = prevfulltreePattern.matcher(line);
            Matcher subtreeMatcher = subtreePattern.matcher(line);
            Matcher fulltreeMatcher = fulltreePattern.matcher(line);
            if (prevsubtreeMatcher.matches()) {
                String treeString = prevsubtreeMatcher.group(1).trim();
                if (treeString.endsWith(";")) {
                    treeString = treeString.substring(0, treeString.length() - 1);
                }
                TreeSpec treeSpec = new TreeSpec(treeString.trim(), reader.getLineNumber() + 1);
                expected.put(treeSpec, canonizeTypeString(prevsubtreeMatcher.group(2)));
            } else if (prevfulltreeMatcher.matches()) {
                String treeString = reader.readLine().trim();
                if (treeString.endsWith(";")) {
                    treeString = treeString.substring(0, treeString.length() - 1);
                }
                TreeSpec treeSpec = new TreeSpec(treeString.trim(), reader.getLineNumber());
                expected.put(treeSpec, canonizeTypeString(prevfulltreeMatcher.group(1)));
            } else if (subtreeMatcher.matches()) {
                String treeString = subtreeMatcher.group(2).trim();
                if (treeString.endsWith(";")) {
                    treeString = treeString.substring(0, treeString.length() - 1);
                }
                TreeSpec treeSpec = new TreeSpec(treeString.trim(), reader.getLineNumber());
                expected.put(treeSpec, canonizeTypeString(subtreeMatcher.group(3)));
            } else if (fulltreeMatcher.matches()) {
                String treeString = fulltreeMatcher.group(1).trim();
                if (treeString.endsWith(";")) {
                    treeString = treeString.substring(0, treeString.length() - 1);
                }
                TreeSpec treeSpec = new TreeSpec(treeString.trim(), reader.getLineNumber());
                expected.put(treeSpec, canonizeTypeString(fulltreeMatcher.group(2)));
            }
            line = reader.readLine();
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return expected;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) LineNumberReader(java.io.LineNumberReader)

Example 73 with LineNumberReader

use of java.io.LineNumberReader in project RxTools by vondear.

the class RxDeviceTool method getMacAddress.

/**
 * 获取设备MAC地址
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
 *
 * @return MAC地址
 */
public static String getMacAddress() {
    String macAddress = null;
    LineNumberReader lnr = null;
    InputStreamReader isr = null;
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        isr = new InputStreamReader(pp.getInputStream());
        lnr = new LineNumberReader(isr);
        macAddress = lnr.readLine().replace(":", "");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        RxFileTool.closeIO(lnr, isr);
    }
    return macAddress == null ? "" : macAddress;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 74 with LineNumberReader

use of java.io.LineNumberReader in project mkgmap by openstreetmap.

the class SeaGenerator method loadIndex.

/**
 * Read the index from stream and populate the index grid.
 * @param fileStream already opened stream
 */
private PrecompData loadIndex(InputStream fileStream) throws IOException {
    int indexWidth = (SeaGenerator.getPrecompTileStart(MAX_LON) - SeaGenerator.getPrecompTileStart(MIN_LON)) / SeaGenerator.PRECOMP_RASTER;
    int indexHeight = (SeaGenerator.getPrecompTileStart(MAX_LAT) - SeaGenerator.getPrecompTileStart(MIN_LAT)) / SeaGenerator.PRECOMP_RASTER;
    PrecompData pi = null;
    LineNumberReader indexReader = new LineNumberReader(new InputStreamReader(fileStream));
    Pattern csvSplitter = Pattern.compile(Pattern.quote(";"));
    String indexLine = null;
    byte[][] indexGrid = new byte[indexWidth + 1][indexHeight + 1];
    boolean detectExt = true;
    String prefix = null;
    String ext = null;
    while ((indexLine = indexReader.readLine()) != null) {
        if (indexLine.startsWith("#")) {
            // comment
            continue;
        }
        String[] items = csvSplitter.split(indexLine);
        if (items.length != 2) {
            log.warn("Invalid format in index file name:", indexLine);
            continue;
        }
        String precompKey = items[0];
        byte type = updatePrecompSeaTileIndex(precompKey, items[1], indexGrid);
        if (type == '?') {
            log.warn("Invalid format in index file name:", indexLine);
            continue;
        }
        if (type == MIXED_TILE) {
            // make sure that all file names are using the same name scheme
            int prePos = items[1].indexOf(items[0]);
            if (prePos >= 0) {
                if (detectExt) {
                    prefix = items[1].substring(0, prePos);
                    ext = items[1].substring(prePos + items[0].length());
                    detectExt = false;
                } else {
                    StringBuilder sb = new StringBuilder(prefix);
                    sb.append(precompKey);
                    sb.append(ext);
                    if (items[1].equals(sb.toString()) == false) {
                        log.warn("Unexpected file name in index file:", indexLine);
                    }
                }
            }
        }
    }
    // 
    pi = new PrecompData();
    pi.precompIndex = indexGrid;
    pi.precompSeaPrefix = prefix;
    pi.precompSeaExt = ext;
    return pi;
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) LineNumberReader(java.io.LineNumberReader)

Example 75 with LineNumberReader

use of java.io.LineNumberReader in project pdfbox by apache.

the class TestTextStripper method doTestFile.

/**
 * Validate text extraction on a single file.
 *
 * @param inFile The PDF file to validate
 * @param outDir The directory to store the output in
 * @param bLogResult Whether to log the extracted text
 * @param bSort Whether or not the extracted text is sorted
 * @throws Exception when there is an exception
 */
public void doTestFile(File inFile, File outDir, boolean bLogResult, boolean bSort) throws Exception {
    if (bSort) {
        log.info("Preparing to parse " + inFile.getName() + " for sorted test");
    } else {
        log.info("Preparing to parse " + inFile.getName() + " for standard test");
    }
    if (!outDir.exists()) {
        if (!outDir.mkdirs()) {
            throw (new Exception("Error creating " + outDir.getAbsolutePath() + " directory"));
        }
    }
    // System.out.println("  " + inFile + (bSort ? " (sorted)" : ""));
    try (PDDocument document = PDDocument.load(inFile)) {
        File outFile;
        File diffFile;
        File expectedFile;
        if (bSort) {
            outFile = new File(outDir, inFile.getName() + "-sorted.txt");
            diffFile = new File(outDir, inFile.getName() + "-sorted-diff.txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + "-sorted.txt");
        } else {
            outFile = new File(outDir, inFile.getName() + ".txt");
            diffFile = new File(outDir, inFile.getName() + "-diff.txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + ".txt");
        }
        // delete possible leftover
        diffFile.delete();
        try (OutputStream os = new FileOutputStream(outFile)) {
            os.write(0xEF);
            os.write(0xBB);
            os.write(0xBF);
            try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, ENCODING))) {
                // Allows for sorted tests
                stripper.setSortByPosition(bSort);
                stripper.writeText(document, writer);
            // close the written file before reading it again
            }
        }
        if (bLogResult) {
            log.info("Text for " + inFile.getName() + ":");
            log.info(stripper.getText(document));
        }
        if (!expectedFile.exists()) {
            this.bFail = true;
            log.error("FAILURE: Input verification file: " + expectedFile.getAbsolutePath() + " did not exist");
            return;
        }
        boolean localFail = false;
        try (LineNumberReader expectedReader = new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile), ENCODING));
            LineNumberReader actualReader = new LineNumberReader(new InputStreamReader(new FileInputStream(outFile), ENCODING))) {
            while (true) {
                String expectedLine = expectedReader.readLine();
                while (expectedLine != null && expectedLine.trim().length() == 0) {
                    expectedLine = expectedReader.readLine();
                }
                String actualLine = actualReader.readLine();
                while (actualLine != null && actualLine.trim().length() == 0) {
                    actualLine = actualReader.readLine();
                }
                if (!stringsEqual(expectedLine, actualLine)) {
                    this.bFail = true;
                    localFail = true;
                    log.error("FAILURE: Line mismatch for file " + inFile.getName() + " (sort = " + bSort + ")" + " at expected line: " + expectedReader.getLineNumber() + " at actual line: " + actualReader.getLineNumber() + "\nexpected line was: \"" + expectedLine + "\"" + "\nactual line was:   \"" + actualLine + "\"" + "\n");
                // lets report all lines, even though this might produce some verbose logging
                // break;
                }
                if (expectedLine == null || actualLine == null) {
                    break;
                }
            }
        }
        if (!localFail) {
            outFile.delete();
        } else {
            // https://code.google.com/p/java-diff-utils/wiki/SampleUsage
            List<String> original = fileToLines(expectedFile);
            List<String> revised = fileToLines(outFile);
            // Compute diff. Get the Patch object. Patch is the container for computed deltas.
            Patch patch = DiffUtils.diff(original, revised);
            try (PrintStream diffPS = new PrintStream(diffFile, ENCODING)) {
                for (Object delta : patch.getDeltas()) {
                    if (delta instanceof ChangeDelta) {
                        ChangeDelta cdelta = (ChangeDelta) delta;
                        diffPS.println("Org: " + cdelta.getOriginal());
                        diffPS.println("New: " + cdelta.getRevised());
                        diffPS.println();
                    } else if (delta instanceof DeleteDelta) {
                        DeleteDelta ddelta = (DeleteDelta) delta;
                        diffPS.println("Org: " + ddelta.getOriginal());
                        diffPS.println("New: " + ddelta.getRevised());
                        diffPS.println();
                    } else if (delta instanceof InsertDelta) {
                        InsertDelta idelta = (InsertDelta) delta;
                        diffPS.println("Org: " + idelta.getOriginal());
                        diffPS.println("New: " + idelta.getRevised());
                        diffPS.println();
                    } else {
                        diffPS.println(delta);
                    }
                }
            }
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ChangeDelta(difflib.ChangeDelta) InputStreamReader(java.io.InputStreamReader) DeleteDelta(difflib.DeleteDelta) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) LineNumberReader(java.io.LineNumberReader) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Patch(difflib.Patch) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) InsertDelta(difflib.InsertDelta)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14