Skip to content

Commit

Permalink
working on xpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jaykang920 committed Dec 29, 2015
1 parent 11b9765 commit 56565a4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
hs_err_pid*

# Maven
target
target

# Eclipse
.classpath
.project
.settings
2 changes: 1 addition & 1 deletion xpiler/src/main/java/xpiler/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.*;

/** Abstract base class for static output file formatters. */
abstract class Formatter {
interface Formatter {
public abstract boolean format(Document doc, String outDir);

public abstract String getDescription();
Expand Down
5 changes: 1 addition & 4 deletions xpiler/src/main/java/xpiler/JavaFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import java.io.*;
import java.util.*;

class JavaFormatter extends Formatter {
class JavaFormatter implements Formatter {
private static final String extension = ".java";
private static final String description = "Java";

@Override
public boolean format(Document doc, String outDir) {
PrintStream out = null;
try {
Expand Down Expand Up @@ -64,10 +63,8 @@ private void formatBody(Context context) {
o.println("}");
}

@Override
public String getDescription() { return description; }

@Override
public boolean isUpToDate(String path, String outDir) {
File source = new File(path);
File target = new File(
Expand Down
2 changes: 1 addition & 1 deletion xpiler/src/main/java/xpiler/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void printUsage() {
for (Map.Entry<String, Formatter> entry : Xpiler.getFormatters().entrySet()) {
out.format("%20s : %s", entry.getKey(), entry.getValue().getDescription());
if (entry.getKey() == DEFAULT_SPEC) {
out.print(" (default)");
out.print(" (default)");
}
out.println();
}
Expand Down
4 changes: 2 additions & 2 deletions xpiler/src/main/java/xpiler/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import java.util.*;

public class StringUtil {
class StringUtil {
private StringUtil() {}

public static String firstToLower(String s) {
if (isNullOrEmpty(s) || Character.isLowerCase(s.charAt(0))) {
return s;
Expand Down
33 changes: 21 additions & 12 deletions xpiler/src/main/java/xpiler/XmlHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013 Jae-jun Kang
// See the file COPYING for license details.
// Copyright (c) 2016 Jae-jun Kang
// See the file LICENSE for details.

package xpiler;

Expand All @@ -8,18 +8,27 @@
import javax.xml.parsers.*;

import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;

class XmlHandler implements Handler {
private static SAXParserFactory factory = SAXParserFactory.newInstance();

public Result handle(String path) {
Result result = new Result();
Context context = new Context();

try {
SAXParser parser = factory.newSAXParser();
parser.parse(new File(path), context);
Context context = new Context();

File file = new File(path);
InputStream inputStream = new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream, "UTF-8");

InputSource inputSource = new InputSource(reader);
inputSource.setEncoding("UTF-8");

parser.parse(inputSource, context);

result.handled = true;
result.doc = context.doc;
} catch (UnknownDocumentException ude) {
Expand All @@ -33,7 +42,7 @@ public Result handle(String path) {

@SuppressWarnings("serial")
private static class UnknownDocumentException extends SAXException {}

private static class Context extends DefaultHandler {
static interface StartHandler {
void handle(Context context, Attributes attributes);
Expand All @@ -45,7 +54,7 @@ static interface EndHandler {
private static Map<String, StartHandler> startHandlers;
private static Map<String, EndHandler> endHandlers;
private static Map<String, String> defaultValues;

static {
startHandlers = new HashMap<String, StartHandler>();
startHandlers.put("x2", new StartHandler() {
Expand Down Expand Up @@ -106,11 +115,11 @@ public void handle(Context context, Attributes attributes) {
defaultValues.put("int64", "0");
defaultValues.put("string", "\"\"");
}

public Document doc;
public Definition current;
public StringBuilder text;

public Context() {
doc = new Document();
text = new StringBuilder();
Expand Down Expand Up @@ -149,7 +158,7 @@ public void characters(char ch[], int start, int length)
}

// startElement helpers

private void startRoot(Attributes attributes) {
String namespace = attributes.getValue("namespace");
doc.namespace = (namespace != null ? namespace : "");
Expand Down Expand Up @@ -199,9 +208,9 @@ private void startProperty(Attributes attributes) {
prop.type = attributes.getValue("type");
def.getProperties().add(prop);
}

// endElement helpers

private void endRoot() {
}

Expand Down
2 changes: 1 addition & 1 deletion xpiler/src/main/java/xpiler/Xpiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Xpiler {
handlers.put(".xml", new XmlHandler());

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

public static Options getOptions() { return options; }
Expand Down

0 comments on commit 56565a4

Please sign in to comment.