Skip to content

Commit

Permalink
1. add TagLogger.java
Browse files Browse the repository at this point in the history
2. add methods to UriBuilder in Provider class
3. improve plugin
  • Loading branch information
hamsterksu committed Oct 20, 2014
1 parent d4ff92d commit 69f1b97
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 56 deletions.
96 changes: 74 additions & 22 deletions android-annotatedsql-processor/res/res/provider.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,6 @@ public class ${className} extends ContentProvider{
return getUriBuilder().append(path).build();
}

@Deprecated
public static Uri getContentUriGroupBy(String path, String groupBy){
return getUriBuilder().append(path).groupBy(groupBy).build();
}

public static Uri contentUriGroupBy(String path, String groupBy){
return getUriBuilder().append(path).groupBy(groupBy).build();
}

@Deprecated
public static Uri getContentUri(String path, long id){
return getUriBuilder().append(path).append(id).build();
Expand All @@ -437,21 +428,43 @@ public class ${className} extends ContentProvider{
}

@Deprecated
public static Uri getContentWithLimitUri(String path, int limit){
return getUriBuilder().append(path).limit(limit).build();
public static Uri getNoNotifyContentUri(String path){
return getUriBuilder().append(path).noNotify().build();
}

public static Uri contentUriWithLimit(String path, int limit){
return getUriBuilder().append(path).limit(limit).build();
public static Uri contentUriNoNotify(String path){
return getUriBuilder().append(path).noNotify().build();
}

@Deprecated
public static Uri getNoNotifyContentUri(String path){
return getUriBuilder().append(path).noNotify().build();
public static Uri getNoNotifyContentUri(String path, long id){
return getUriBuilder().append(path).append(id).noNotify().build();
}

public static Uri contentUriNoNotify(String path){
return getUriBuilder().append(path).noNotify().build();
public static Uri contentUriNoNotify(String path, long id){
return getUriBuilder().append(path).append(id).noNotify().build();
}

public static Uri contentUriNoNotify(String path, String id){
return getUriBuilder().append(path).append(id).noNotify().build();
}

@Deprecated
public static Uri getContentUriGroupBy(String path, String groupBy){
return getUriBuilder().append(path).groupBy(groupBy).build();
}

public static Uri contentUriGroupBy(String path, String groupBy){
return getUriBuilder().append(path).groupBy(groupBy).build();
}

@Deprecated
public static Uri getContentWithLimitUri(String path, int limit){
return getUriBuilder().append(path).limit(limit).build();
}

public static Uri contentUriWithLimit(String path, int limit){
return getUriBuilder().append(path).limit(limit).build();
}

public static Uri contentUriInsertNoNotify(String path, int conflicResolution){
Expand All @@ -470,11 +483,6 @@ public class ${className} extends ContentProvider{
return getUriBuilder().append(path).bulkInsertMode(conflict).build();
}

@Deprecated
public static Uri getNoNotifyContentUri(String path, long id){
return getUriBuilder().append(path).append(id).noNotify().build();
}

public static UriBuilder getUriBuilder(){
return new UriBuilder(BASE_URI);
}
Expand Down Expand Up @@ -575,6 +583,50 @@ public class ${className} extends ContentProvider{
public static String getGroupBy(Uri uri){
return uri.getQueryParameter(QUERY_GROUP_BY);
}

public static Uri contentUri(Uri uri, long id){
return new UriBuilder(uri).append(id).build();
}

public static Uri contentUri(Uri uri, String id){
return new UriBuilder(uri).append(id).build();
}

public static Uri contentUriNoNotify(Uri uri){
return new UriBuilder(uri).noNotify().build();
}

public static Uri contentUriNoNotify(Uri uri, long id){
return new UriBuilder(uri).append(id).noNotify().build();
}

public static Uri contentUriNoNotify(Uri uri, String id){
return new UriBuilder(uri).append(id).noNotify().build();
}

public static Uri contentUriWithLimit(Uri uri, int limit){
return new UriBuilder(uri).limit(limit).build();
}

public static Uri contentUriGroupBy(Uri uri, String groupBy){
return new UriBuilder(uri).groupBy(groupBy).build();
}

public static Uri contentUriInsertNoNotify(Uri uri, int conflicResolution){
return new UriBuilder(uri).noNotify().insertConflictMode(conflicResolution).build();
}

public static Uri contentUriInsert(Uri uri, int conflicResolution){
return new UriBuilder(uri).insertConflictMode(conflicResolution).build();
}

public static Uri contentUriBulkInsertNoNotify(Uri uri, BulkInsertConflictMode conflict){
return new UriBuilder(uri).noNotify().bulkInsertMode(conflict).build();
}

public static Uri contentUriBulkInsert(Uri uri, BulkInsertConflictMode conflict){
return new UriBuilder(uri).bulkInsertMode(conflict).build();
}
}

<#if generateHelper>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,73 @@
package com.annotatedsql.processor;

import com.annotatedsql.processor.logger.ILogger;
import com.annotatedsql.util.TextUtils;

import java.util.Collection;
import java.util.Map;

import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic.Kind;

public class ProcessorLogger {
public class ProcessorLogger implements ILogger {

public static final String ARG_LOG_LEVEL = "logLevel";
private static final String LOG_TAG = "aSQL: ";
private static final LogLevel DEFAULT_LEVEL_VALUE = LogLevel.INFO;

private enum LogLevel {
INFO, WARN, ERROR;

public static LogLevel parse(String name) {
if (TextUtils.isEmpty(name))
return DEFAULT_LEVEL_VALUE;
for (LogLevel logLevel : LogLevel.values()) {
if (logLevel.name().equalsIgnoreCase(name)) {
return logLevel;
}
}
return DEFAULT_LEVEL_VALUE;
}
}

public static final String LOG_TAG = "A_SQL: ";
private Messager messager;

public ProcessorLogger(Messager messager) {
private LogLevel logLevel = DEFAULT_LEVEL_VALUE;

public ProcessorLogger(Messager messager, Map<String, String> options) {
this.messager = messager;
if (options != null && options.containsKey(ARG_LOG_LEVEL)) {
String level = options.get(ARG_LOG_LEVEL);
logLevel = LogLevel.parse(level);
}
}

@Override
public void w(String msg) {
w(msg, null);
}

@Override
public void w(String msg, Element element) {
messager.printMessage(Kind.WARNING, LOG_TAG + msg, element);
if (logLevel.ordinal() <= LogLevel.WARN.ordinal()) {
messager.printMessage(Kind.WARNING, LOG_TAG + msg, element);
}
}

@Override
public void i(String msg) {
i(msg, null);
}

@Override
public void i(String msg, Element element) {
//messager.printMessage(Kind.NOTE, LOG_TAG + msg, element);
if (logLevel.ordinal() == LogLevel.INFO.ordinal()) {
messager.printMessage(Kind.NOTE, LOG_TAG + msg, element);
}
}

@Override
public void e(String msg, Throwable e, Element element) {
if (e != null) {
messager.printMessage(Kind.ERROR, LOG_TAG + msg + ": " + e.getMessage(), element);
Expand All @@ -39,14 +76,17 @@ public void e(String msg, Throwable e, Element element) {
}
}

@Override
public void e(String msg, Throwable e) {
e(msg, e, null);
}

@Override
public void e(String msg, Element e) {
e(msg, null, e);
}

@Override
public void e(String msg, Element... elms) {
if (elms != null && elms.length != 0) {
for (Element e : elms) {
Expand All @@ -57,6 +97,7 @@ public void e(String msg, Element... elms) {
}
}

@Override
public void e(String msg, Collection<? extends Element> elms) {
if (elms != null && !elms.isEmpty()) {
for (Element e : elms) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.annotatedsql.processor.logger;

import java.util.Collection;

import javax.lang.model.element.Element;

/**
* Created by hamsterksu on 20.10.2014.
*/
public interface ILogger {
void w(String msg);

void w(String msg, Element element);

void i(String msg);

void i(String msg, Element element);

void e(String msg, Throwable e, Element element);

void e(String msg, Throwable e);

void e(String msg, Element e);

void e(String msg, Element... elms);

void e(String msg, Collection<? extends Element> elms);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.annotatedsql.processor.logger;

import com.annotatedsql.processor.ProcessorLogger;

import java.util.Collection;

import javax.lang.model.element.Element;

/**
* Created by hamsterksu on 20.10.2014.
*/
public class TagLogger implements ILogger{

private final ProcessorLogger logger;
private final String tag;

public TagLogger(String tag, ProcessorLogger logger) {
this.tag = tag;
this.logger = logger;
}

public ProcessorLogger getLogger() {
return logger;
}

@Override
public void w(String msg) {
logger.w("[" + tag + "] " + msg);
}

@Override
public void w(String msg, Element element) {
logger.w("[" + tag + "] " + msg, element);
}

@Override
public void i(String msg) {
logger.i("[" + tag + "] " + msg);
}

@Override
public void i(String msg, Element element) {
logger.i("[" + tag + "] " + msg, element);
}

@Override
public void e(String msg, Throwable e, Element element) {
logger.e("[" + tag + "] " + msg, e, element);
}

@Override
public void e(String msg, Throwable e) {
logger.e("[" + tag + "] " + msg, e);
}

@Override
public void e(String msg, Element e) {
logger.e("[" + tag + "] " + msg, e);
}

@Override
public void e(String msg, Element... elms) {
logger.e("[" + tag + "] " + msg, elms);
}

@Override
public void e(String msg, Collection<? extends Element> elms) {
logger.e("[" + tag + "] " + msg, elms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.annotatedsql.ftl.TriggerMeta;
import com.annotatedsql.ftl.UriMeta;
import com.annotatedsql.processor.ProcessorLogger;
import com.annotatedsql.processor.logger.TagLogger;
import com.annotatedsql.processor.sql.TableParser;
import com.annotatedsql.processor.sql.TableResult;
import com.annotatedsql.util.TextUtils;
Expand All @@ -28,8 +29,10 @@
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
Expand All @@ -45,6 +48,7 @@

@SupportedAnnotationTypes({"com.annotatedsql.annotation.provider.Provider", "com.annotatedsql.annotation.provider.Providers"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedOptions({ProcessorLogger.ARG_LOG_LEVEL})
public class ProviderProcessor extends AbstractProcessor {

private final static int MATCH_TYPE_ITEM = 0x0001;
Expand All @@ -54,15 +58,22 @@ public class ProviderProcessor extends AbstractProcessor {

private int elementCode = 0x1000;

private ProcessorLogger logger;
private TagLogger logger;
private Configuration cfg = new Configuration();

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
logger = new TagLogger("ProviderProcessor", new ProcessorLogger(processingEnv.getMessager(), processingEnv.getOptions()));
logger.i("init");

cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/res"));
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
logger = new ProcessorLogger(processingEnv.getMessager());
if (annotations == null || annotations.size() == 0)
return false;
cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/res"));
try {
return processProviders(roundEnv);
} catch (AnnotationParsingException e) {
Expand Down Expand Up @@ -243,6 +254,9 @@ private UriMeta createUriMeta(Type type, String customMimeType, String path, Str
}
}
int typeMask = type == Type.DIR ? MATCH_TYPE_DIR : MATCH_TYPE_ITEM;
if(!TextUtils.isEmpty(customMimeType)){
typeMask = MATCH_TYPE_CUSTOM;
}
int code = elementCode | typeMask;
elementCode += 0x0010;
return new UriMeta(path, code, type == Type.ITEM, customMimeType, selectColumn, from, altNotify, onlyQuery, triggers, rawQuery, builder);
Expand Down

0 comments on commit 69f1b97

Please sign in to comment.