dew/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
changeset 1336 804f6f982f4e
parent 1327 217ca48c5b80
     1.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Thu Oct 03 10:03:22 2013 +0200
     1.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Thu Oct 03 15:51:55 2013 +0200
     1.3 @@ -29,6 +29,8 @@
     1.4  import java.util.HashMap;
     1.5  import java.util.List;
     1.6  import java.util.Map;
     1.7 +import java.util.regex.Matcher;
     1.8 +import java.util.regex.Pattern;
     1.9  import javax.tools.Diagnostic;
    1.10  import javax.tools.DiagnosticListener;
    1.11  import javax.tools.FileObject;
    1.12 @@ -53,8 +55,8 @@
    1.13      private final String html;
    1.14  
    1.15      private Compile(String html, String code) throws IOException {
    1.16 -        this.pkg = find("package", ';', code);
    1.17 -        this.cls = find("class", ' ', code);
    1.18 +        this.pkg = findPkg(code);
    1.19 +        this.cls = findCls(code);
    1.20          this.html = html;
    1.21          classes = compile(html, code);
    1.22      }
    1.23 @@ -118,20 +120,23 @@
    1.24      public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    1.25          errors.add(diagnostic);
    1.26      }
    1.27 -    private static String find(String pref, char term, String java) throws IOException {
    1.28 -        int pkg = java.indexOf(pref);
    1.29 -        if (pkg != -1) {
    1.30 -            pkg += pref.length();
    1.31 -            while (Character.isWhitespace(java.charAt(pkg))) {
    1.32 -                pkg++;
    1.33 -            }
    1.34 -            int semicolon = java.indexOf(term, pkg);
    1.35 -            if (semicolon != -1) {
    1.36 -                String pkgName = java.substring(pkg, semicolon).trim();
    1.37 -                return pkgName;
    1.38 -            }
    1.39 +    private static String findPkg(String java) throws IOException {
    1.40 +        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
    1.41 +        Matcher m = p.matcher(java);
    1.42 +        if (!m.find()) {
    1.43 +            throw new IOException("Can't find package declaration in the java file");
    1.44          }
    1.45 -        throw new IOException("Can't find " + pref + " declaration in the java file");
    1.46 +        String pkg = m.group(1);
    1.47 +        return pkg;
    1.48 +    }
    1.49 +    private static String findCls(String java) throws IOException {
    1.50 +        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
    1.51 +        Matcher m = p.matcher(java);
    1.52 +        if (!m.find()) {
    1.53 +            throw new IOException("Can't find package declaration in the java file");
    1.54 +        }
    1.55 +        String cls = m.group(1);
    1.56 +        return cls;
    1.57      }
    1.58  
    1.59      String getHtml() {