Initial version of @OnLocation API
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 06 Jul 2013 08:09:53 +0200
changeset 172a058550f8f1f
parent 171 54ac82353158
child 173 19f090945bd2
Initial version of @OnLocation API
geo/pom.xml
geo/src/main/java/net/java/html/geo/GeoHandle.java
geo/src/main/java/net/java/html/geo/OnLocation.java
geo/src/main/java/net/java/html/geo/Position.java
geo/src/main/java/org/apidesign/html/geo/impl/GeoProcessor.java
geo/src/test/java/net/java/html/geo/OnLocationTest.java
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/geo/pom.xml	Sat Jul 06 08:09:53 2013 +0200
     1.3 @@ -0,0 +1,29 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     1.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     1.7 +  <modelVersion>4.0.0</modelVersion>
     1.8 +  <parent>
     1.9 +    <groupId>org.apidesign</groupId>
    1.10 +    <artifactId>html</artifactId>
    1.11 +    <version>0.4-SNAPSHOT</version>
    1.12 +  </parent>
    1.13 +  <groupId>org.apidesign.html</groupId>
    1.14 +  <artifactId>net.java.html.geo</artifactId>
    1.15 +  <version>0.4-SNAPSHOT</version>
    1.16 +  <name>Geolocation API</name>
    1.17 +  <url>http://maven.apache.org</url>
    1.18 +  <properties>
    1.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.20 +  </properties>
    1.21 +  <dependencies>
    1.22 +      <dependency>
    1.23 +          <groupId>org.testng</groupId>
    1.24 +          <artifactId>testng</artifactId>
    1.25 +      </dependency>
    1.26 +    <dependency>
    1.27 +      <groupId>org.netbeans.api</groupId>
    1.28 +      <artifactId>org-openide-util-lookup</artifactId>
    1.29 +      <type>jar</type>
    1.30 +    </dependency>
    1.31 +  </dependencies>
    1.32 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/geo/src/main/java/net/java/html/geo/GeoHandle.java	Sat Jul 06 08:09:53 2013 +0200
     2.3 @@ -0,0 +1,57 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +package net.java.html.geo;
    2.25 +
    2.26 +/**
    2.27 + *
    2.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.29 + */
    2.30 +public abstract class GeoHandle {
    2.31 +    private final boolean oneTime;
    2.32 +    private boolean enableHighAccuracy;
    2.33 +    private long timeout;
    2.34 +    private long maximumAge;
    2.35 +
    2.36 +    protected GeoHandle(boolean oneTime) {
    2.37 +        this.oneTime = oneTime;
    2.38 +    }
    2.39 +    
    2.40 +    protected abstract void onLocation(Position p);
    2.41 +    protected abstract void onError(Throwable t);
    2.42 +    
    2.43 +    public void setHighAccuracy(boolean enable) {
    2.44 +        this.enableHighAccuracy = enable;
    2.45 +    }
    2.46 +    
    2.47 +    public void setTimeout(long timeout) {
    2.48 +        this.timeout = timeout;
    2.49 +    }
    2.50 +    
    2.51 +    public void setMaximumAge(long age) {
    2.52 +        this.maximumAge = age;
    2.53 +    }
    2.54 +    
    2.55 +    public void start() {
    2.56 +    }
    2.57 +    
    2.58 +    public void stop() {
    2.59 +    }
    2.60 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/geo/src/main/java/net/java/html/geo/OnLocation.java	Sat Jul 06 08:09:53 2013 +0200
     3.3 @@ -0,0 +1,38 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package net.java.html.geo;
    3.25 +
    3.26 +import java.lang.annotation.ElementType;
    3.27 +import java.lang.annotation.Retention;
    3.28 +import java.lang.annotation.RetentionPolicy;
    3.29 +import java.lang.annotation.Target;
    3.30 +
    3.31 +/**
    3.32 + *
    3.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.34 + */
    3.35 +@Retention(RetentionPolicy.SOURCE)
    3.36 +@Target(ElementType.METHOD)
    3.37 +public @interface OnLocation {
    3.38 +    public String className() default "";
    3.39 +    
    3.40 +    public String onError() default "";
    3.41 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/geo/src/main/java/net/java/html/geo/Position.java	Sat Jul 06 08:09:53 2013 +0200
     4.3 @@ -0,0 +1,49 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package net.java.html.geo;
    4.25 +
    4.26 +/**
    4.27 + *
    4.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.29 + */
    4.30 +public final class Position {
    4.31 +    private Position() {
    4.32 +    }
    4.33 +
    4.34 +    public Coordinates getCoords() {
    4.35 +        return null;
    4.36 +    }
    4.37 +    
    4.38 +    public long getTimestamp() {
    4.39 +        return 0L;
    4.40 +    }
    4.41 +    
    4.42 +    public static final class Coordinates {
    4.43 +        private double latitude;
    4.44 +        private double longitude;
    4.45 +        private double accuracy;
    4.46 +
    4.47 +        private Double altitude;
    4.48 +        private Double altitudeAccuracy;
    4.49 +        private Double heading;
    4.50 +        private Double speed;
    4.51 +    }
    4.52 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/geo/src/main/java/org/apidesign/html/geo/impl/GeoProcessor.java	Sat Jul 06 08:09:53 2013 +0200
     5.3 @@ -0,0 +1,135 @@
     5.4 +/**
     5.5 + * HTML via Java(tm) Language Bindings
     5.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, version 2 of the License.
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details. apidesign.org
    5.16 + * designates this particular file as subject to the
    5.17 + * "Classpath" exception as provided by apidesign.org
    5.18 + * in the License file that accompanied this code.
    5.19 + *
    5.20 + * You should have received a copy of the GNU General Public License
    5.21 + * along with this program. Look for COPYING file in the top folder.
    5.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    5.23 + */
    5.24 +package org.apidesign.html.geo.impl;
    5.25 +
    5.26 +import java.io.IOException;
    5.27 +import java.io.Writer;
    5.28 +import java.util.Locale;
    5.29 +import java.util.Set;
    5.30 +import java.util.logging.Level;
    5.31 +import java.util.logging.Logger;
    5.32 +import javax.annotation.processing.AbstractProcessor;
    5.33 +import javax.annotation.processing.Processor;
    5.34 +import javax.annotation.processing.RoundEnvironment;
    5.35 +import javax.annotation.processing.SupportedAnnotationTypes;
    5.36 +import javax.annotation.processing.SupportedSourceVersion;
    5.37 +import javax.lang.model.SourceVersion;
    5.38 +import javax.lang.model.element.Element;
    5.39 +import javax.lang.model.element.ElementKind;
    5.40 +import javax.lang.model.element.ExecutableElement;
    5.41 +import javax.lang.model.element.Modifier;
    5.42 +import javax.lang.model.element.PackageElement;
    5.43 +import javax.lang.model.element.TypeElement;
    5.44 +import javax.tools.Diagnostic;
    5.45 +import javax.tools.JavaFileObject;
    5.46 +import net.java.html.geo.OnLocation;
    5.47 +import org.openide.util.lookup.ServiceProvider;
    5.48 +
    5.49 +/** Annotation processor to generate callbacks from {@link GeoHandle} class.
    5.50 + *
    5.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.52 + */
    5.53 +@ServiceProvider(service=Processor.class)
    5.54 +@SupportedSourceVersion(SourceVersion.RELEASE_6)
    5.55 +@SupportedAnnotationTypes({
    5.56 +    "net.java.html.geo.OnLocation"
    5.57 +})
    5.58 +public final class GeoProcessor extends AbstractProcessor {
    5.59 +    private static final Logger LOG = Logger.getLogger(GeoProcessor.class.getName());
    5.60 +    @Override
    5.61 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    5.62 +        boolean ok = true;
    5.63 +        for (Element e : roundEnv.getElementsAnnotatedWith(OnLocation.class)) {
    5.64 +            if (!processLocation(e)) {
    5.65 +                ok = false;
    5.66 +            }
    5.67 +        }
    5.68 +        return ok;
    5.69 +    }
    5.70 +
    5.71 +    private void error(String msg, Element e) {
    5.72 +        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e);
    5.73 +    }
    5.74 +    
    5.75 +    private boolean processLocation(Element e) {
    5.76 +        if (e.getKind() != ElementKind.METHOD) {
    5.77 +            return false;
    5.78 +        }
    5.79 +        ExecutableElement me = (ExecutableElement) e;
    5.80 +        OnLocation ol = e.getAnnotation(OnLocation.class);
    5.81 +        if (ol == null) {
    5.82 +            return true;
    5.83 +        }
    5.84 +        String className = ol.className();
    5.85 +        if (className.isEmpty()) {
    5.86 +            String n = e.getSimpleName().toString();
    5.87 +            if (n.isEmpty()) {
    5.88 +                error("Empty method name", e);
    5.89 +                return false;
    5.90 +            }
    5.91 +            final String firstLetter = n.substring(0, 1).toUpperCase(Locale.ENGLISH);
    5.92 +            className = firstLetter + n.substring(1) + "Handle";
    5.93 +        }
    5.94 +        TypeElement te = (TypeElement)e.getEnclosingElement();
    5.95 +        PackageElement pe = (PackageElement) te.getEnclosingElement();
    5.96 +        final String pkg = pe.getQualifiedName().toString();
    5.97 +        final String fqn = pkg + "." + className;
    5.98 +        final boolean isStatic = me.getModifiers().contains(Modifier.STATIC);
    5.99 +        try {
   5.100 +            JavaFileObject fo = processingEnv.getFiler().createSourceFile(fqn, e);
   5.101 +            Writer w = fo.openWriter();
   5.102 +            w.append("package ").append(pkg).append(";\n");
   5.103 +            w.append("class ").append(className).append(" extends net.java.html.geo.GeoHandle {\n");
   5.104 +            w.append("  private ").append(te.getSimpleName()).append(" i;\n");
   5.105 +            w.append("  private ").append(className).append("(boolean oneTime");
   5.106 +            w.append(", ").append(te.getSimpleName()).append(" i");
   5.107 +            w.append(") {\n    super(oneTime);\n");
   5.108 +            if (!isStatic) {
   5.109 +                w.append("    this.i = i;\n");
   5.110 +            }
   5.111 +            w.append("}\n");
   5.112 +            w.append("  static net.java.html.geo.GeoHandle createQuery(");
   5.113 +            String inst;
   5.114 +            if (!isStatic) {
   5.115 +                w.append(te.getSimpleName()).append(" instance");
   5.116 +                inst = "instance";
   5.117 +            } else {
   5.118 +                inst = "null";
   5.119 +            }
   5.120 +            w.append(") { return new ").append(className).append("(true, ").append(inst).append("); }\n");
   5.121 +            w.append("  static net.java.html.geo.GeoHandle createWatch(");
   5.122 +            if (!isStatic) {
   5.123 +                w.append(te.getSimpleName()).append(" instance");
   5.124 +            }
   5.125 +            w.append(") { return new ").append(className).append("(false, ").append(inst).append("); }\n");
   5.126 +            w.append("  protected void onError(Throwable t) {}\n");
   5.127 +            w.append("  protected void onLocation(net.java.html.geo.Position p) {}\n");
   5.128 +            w.append("}\n");
   5.129 +            w.close();
   5.130 +        } catch (IOException ex) {
   5.131 +            Logger.getLogger(GeoProcessor.class.getName()).log(Level.SEVERE, null, ex);
   5.132 +            error("Can't write handler class: " + ex.getMessage(), e);
   5.133 +            return false;
   5.134 +        }
   5.135 +        
   5.136 +        return true;
   5.137 +    }
   5.138 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/geo/src/test/java/net/java/html/geo/OnLocationTest.java	Sat Jul 06 08:09:53 2013 +0200
     6.3 @@ -0,0 +1,74 @@
     6.4 +/**
     6.5 + * HTML via Java(tm) Language Bindings
     6.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, version 2 of the License.
    6.11 + *
    6.12 + * This program is distributed in the hope that it will be useful,
    6.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.15 + * GNU General Public License for more details. apidesign.org
    6.16 + * designates this particular file as subject to the
    6.17 + * "Classpath" exception as provided by apidesign.org
    6.18 + * in the License file that accompanied this code.
    6.19 + *
    6.20 + * You should have received a copy of the GNU General Public License
    6.21 + * along with this program. Look for COPYING file in the top folder.
    6.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    6.23 + */
    6.24 +package net.java.html.geo;
    6.25 +
    6.26 +import org.testng.annotations.Test;
    6.27 +
    6.28 +/**
    6.29 + */
    6.30 +public class OnLocationTest {
    6.31 +    
    6.32 +    static @OnLocation void onLocation(Position p) {
    6.33 +    }
    6.34 +
    6.35 +    @Test public void createOneTimeQueryStatic() {
    6.36 +        GeoHandle h = OnLocationHandle.createQuery();
    6.37 +        h.setHighAccuracy(false);
    6.38 +        h.setTimeout(1000L);
    6.39 +        h.setMaximumAge(1000L);
    6.40 +        h.start();
    6.41 +        h.stop();
    6.42 +    }
    6.43 +
    6.44 +    @Test public void createRepeatableWatchStatic() {
    6.45 +        GeoHandle h = OnLocationHandle.createQuery();
    6.46 +        h.setHighAccuracy(false);
    6.47 +        h.setTimeout(1000L);
    6.48 +        h.setMaximumAge(1000L);
    6.49 +        h.start();
    6.50 +        h.stop();
    6.51 +    }
    6.52 +    
    6.53 +    @OnLocation void instance(Position p) {
    6.54 +    }
    6.55 +    
    6.56 +    @Test public void createOneTimeQueryInstance() {
    6.57 +        OnLocationTest t = new OnLocationTest();
    6.58 +        
    6.59 +        GeoHandle h = InstanceHandle.createQuery(t);
    6.60 +        h.setHighAccuracy(false);
    6.61 +        h.setTimeout(1000L);
    6.62 +        h.setMaximumAge(1000L);
    6.63 +        h.start();
    6.64 +        h.stop();
    6.65 +    }
    6.66 +
    6.67 +    @Test public void createRepeatableWatch() {
    6.68 +        OnLocationTest t = new OnLocationTest();
    6.69 +        
    6.70 +        GeoHandle h = InstanceHandle.createWatch(t);
    6.71 +        h.setHighAccuracy(false);
    6.72 +        h.setTimeout(1000L);
    6.73 +        h.setMaximumAge(1000L);
    6.74 +        h.start();
    6.75 +        h.stop();
    6.76 +    }
    6.77 +}
     7.1 --- a/pom.xml	Sat Jun 29 22:44:59 2013 +0200
     7.2 +++ b/pom.xml	Sat Jul 06 08:09:53 2013 +0200
     7.3 @@ -25,6 +25,7 @@
     7.4      <module>context</module>
     7.5      <module>boot</module>
     7.6      <module>boot-fx</module>
     7.7 +    <module>geo</module>
     7.8    </modules>
     7.9    <licenses>
    7.10        <license>