Skip to content

Commit

Permalink
touched xpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jaykang920 committed Oct 20, 2017
1 parent 69524ad commit ef65f53
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 125 deletions.
9 changes: 4 additions & 5 deletions x2java/src/main/java/x2java/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,14 @@ public void setPosition(int value) {
throw new IndexOutOfBoundsException();
}
position = adjusted;
// Update the current block.
int blockIndex = position >> SIZE_EXPONENT;
// Prepare for blockFeed().
if ((blockIndex != 0) && ((position & REMAINDER_MASK) == 0)) {
--blockIndex;
}
if (blockIndex != currentIndex) {
currentIndex = blockIndex;
current = blocks.get(currentIndex);
}
// Force to update the current block.
currentIndex = blockIndex;
current = blocks.get(currentIndex);
}

public void shrink(int numBytes) {
Expand Down
2 changes: 1 addition & 1 deletion xpiler/src/main/java/xpiler/JavaFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public boolean format(Document doc, String outDir) {
def.name = StringUtil.firstToUpper(def.name);
}

if (!Xpiler.getOptions().isForced()) {
if (!Main.getOptions().isForced()) {
boolean flag = true;
for (Definition def : definitions) {
if (!isUpToDate(doc.inputPath, outDir, def)) {
Expand Down
111 changes: 107 additions & 4 deletions xpiler/src/main/java/xpiler/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,121 @@

package xpiler;

import java.io.*;
import java.util.*;

public class Main {
private static Map<String, Handler> handlers;
private static Map<String, Formatter> formatters;
private static Options options;

private Formatter formatter;
private Stack<String> subDirs;
private boolean error;

static {
handlers = new HashMap<String, Handler>();
handlers.put(".xml", new XmlHandler());

formatters = new HashMap<String, Formatter>();
formatters.put("java", new JavaFormatter());

options = new Options();
}

public Main() {
formatter = formatters.get(options.getSpec());
subDirs = new Stack<String>();
error = false;
}

public static Map<String, Formatter> getFormatters() { return formatters; }
public static Options getOptions() { return options; }

public static void main(String[] args) {
int index = Xpiler.getOptions().parse(args);
int index = Main.getOptions().parse(args);
if (index >= args.length) {
System.out.println("xpiler: missing arguments");
System.exit(2);
}

Xpiler xpiler = new Xpiler();
Main main = new Main();
while (index < args.length) {
xpiler.process(args[index++]);
main.process(args[index++]);
}
System.exit(main.error() ? 1 : 0);
}

public boolean error() { return error; }

public void process(String path) {
File file = new File(path);
if (file.exists()) {
if (file.isDirectory()) {
processDir(file);
} else {
processFile(file);
}
} else {
System.err.format("%s doesn't exist.\n", path);
error = true;
}
}

private void processDir(File dir) {
System.out.format("Directory %s\n", PathUtil.getCanonical(dir));
File[] entries = dir.listFiles();
for (File entry : entries) {
if (entry.isDirectory()) {
if (options.isRecursive()) {
subDirs.push(entry.getName());
processDir(entry);
subDirs.pop();
}
} else {
processFile(entry);
}
}
}

private void processFile(File file) {
String path = file.getPath();
String filename = file.getName();
String extension = PathUtil.getExtension(filename);
String outDir;

if (options.getOutDir() == null) {
outDir = file.getParent();
} else {
outDir = PathUtil.join(options.getOutDir(), PathUtil.join(subDirs));
}

Handler handler = handlers.get(extension.toLowerCase());
if (handler == null) {
return;
}

Handler.Result result = handler.handle(path);
if (result.handled == false) {
return;
}

if (result.handled == true && result.doc == null) {
System.out.format("error: %s\n", result.message);
error = true;
return;
}

Document doc = result.doc;
doc.inputPath = path;

File dir = new File(outDir);
if (!dir.exists()) {
dir.mkdirs();
}

if (formatter.format(doc, outDir) == false) {
error = true;
}
System.exit(xpiler.hasError() ? 1 : 0);
}
}
12 changes: 6 additions & 6 deletions xpiler/src/main/java/xpiler/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public int parse(String[] args) {
switch (getopt.getOpt()) {
case 's':
spec = getopt.getOptArg().toLowerCase();
if (!Xpiler.getFormatters().containsKey(spec)) {
if (!Main.getFormatters().containsKey(spec)) {
System.err.format("Unknown target formatter specified: %s\n", spec);
System.exit(1);
}
Expand All @@ -57,18 +57,18 @@ public int parse(String[] args) {
}
return getopt.getOptInd();
}

private static void printUsage() {
PrintStream out = System.out;
out.println("usage: xpiler (options) [path...]");
out.println(" options:");
out.println(" -o (--out-dir) dir : specify the output root directory");
out.println(" -r (--recursive) : process subdirectories recursively");
out.println(" -f (--force) : force all to be recompiled");
out.println(" -h (--help) : print this message and quit");
out.println(" -s (--spec) spec : specify the target formatter");
out.println(" -o (--out-dir) dir : specifies the output root directory");
out.println(" -r (--recursive) : process subdirectories recursively");
out.println(" -s (--spec) spec : specifies the target formatter");

for (Map.Entry<String, Formatter> entry : Xpiler.getFormatters().entrySet()) {
for (Map.Entry<String, Formatter> entry : Main.getFormatters().entrySet()) {
out.format("%20s : %s", entry.getKey(), entry.getValue().description());
if (entry.getKey() == DEFAULT_SPEC) {
out.print(" (default)");
Expand Down
109 changes: 0 additions & 109 deletions xpiler/src/main/java/xpiler/Xpiler.java

This file was deleted.

0 comments on commit ef65f53

Please sign in to comment.