-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d56ee56
Showing
9 changed files
with
406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
*~ | ||
*.class | ||
|
||
# project | ||
buffers/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# maven | ||
target/ | ||
|
||
# eclipse | ||
|
||
*.pydevproject | ||
.project | ||
.metadata | ||
bin/** | ||
tmp/** | ||
tmp/**/* | ||
*.tmp | ||
*.bak | ||
*.swp | ||
*~.nib | ||
local.properties | ||
.classpath | ||
.settings/ | ||
.loadpath | ||
|
||
# External tool builders | ||
.externalToolBuilders/ | ||
|
||
# Locally stored "Eclipse launch configurations" | ||
*.launch | ||
|
||
# CDT-specific | ||
.cproject | ||
|
||
# PDT-specific | ||
.buildpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2014- Mitoma Ryo - https://github.com/mitoma | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Ponto | ||
|
||
Pontoはタイプセーフなプロパティファイルへのアクセサを生成するライブラリです。 | ||
|
||
## 特徴 | ||
|
||
タイプセーフなプロパティファイルへのアクセサをaptによって自動生成することにより、 | ||
プロパティファイルのキー名の入力間違いなどによるバグをコンパイル時に検出することができます。 | ||
|
||
### Java標準のプロパティファイルへのアクセス方法 | ||
|
||
``` | ||
Properties properties = new Properties(); | ||
ClassLoader loader = ClassLoader.getSystemClassLoader(); | ||
try{ | ||
properties.load(loader.getResourceAsStream("ponto.properties")); | ||
}catch (Exception e){} | ||
properties.getProperty("setting.key1"); | ||
// => "value1" | ||
// "setting.key1" が本当に存在するのかわからない。 | ||
// (コンパイル時に検出できない) | ||
``` | ||
|
||
### Pontoを使ったプロパティファイルへのアクセス方法 | ||
|
||
``` | ||
PontoResource.setting.key1(); | ||
// => "value1" | ||
// aptにより自動生成された PontoResourceクラスから安全にプロパティを取得できる。 | ||
// (存在しないキーを指定した場合コンパイルエラーになる) | ||
``` | ||
|
||
## 簡単な使い方 | ||
|
||
1. maven に依存関係を追加する | ||
> ``` | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.mitoma.ponto</groupId> | ||
<artifactId>Ponto</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
1. プロパティファイルを用意する。 | ||
> src/main/resources/ponto.properties | ||
``` | ||
setting.key1=value1 | ||
setting.key2=value2 | ||
setting.key3=value3 | ||
``` | ||
|
||
1. コンフィグファイルを作成し、アノテーションをつける。 | ||
> src/main/java/your/package/PontoConfig.java | ||
``` | ||
@ConstantResource | ||
public class PontoConfig { | ||
} | ||
``` | ||
|
||
1. mvn compile する。 | ||
> target/generated-sources/annotations に org.mitoma.PontoResource というクラスファイルが生成される。 | ||
## アノテーションリファレンス | ||
|
||
@ConstantResource に指定できる引数 | ||
|
||
- value | ||
- String[] propertiesファイルを複数指定することができる。 | ||
- .properties と .xml 形式の2つのプロパティファイルの書式に対応している。 | ||
- default {"ponto.properties"} | ||
- packageName | ||
- String 自動生成されるクラスのパッケージを指定 | ||
- default "org.mitoma.ponto" | ||
- className | ||
- String 自動生成されるクラスのクラス名を指定 | ||
- default "PontoResource" | ||
- keyStyle | ||
- KeyStyle アクセサのスタイルを選択できる。Hierarchical は PontoResource.setting.key1() のようにアクセスできる。 Flat は PontoResource.setting_key1() のようにアクセスできる。 | ||
- default KeyStyle.Hierarchical | ||
|
||
## ライセンス | ||
|
||
MIT-LICENSE | ||
|
||
## Pontoの由来 | ||
|
||
言わずと知れた京都の飲み屋街、[先斗町](http://ja.wikipedia.org/wiki/%E5%85%88%E6%96%97%E7%94%BA)である。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.mitoma.ponto</groupId> | ||
<artifactId>ponto</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<encoding>UTF-8</encoding> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
<compilerArgument>-proc:none</compilerArgument> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.mitoma.ponto; | ||
|
||
public @interface ConstantResource { | ||
public String[] value() default { "ponto.properties" }; | ||
|
||
public String packageName() default "org.mitoma.ponto"; | ||
|
||
public String className() default "PontoResource"; | ||
|
||
public KeyStyle keyStyle() default KeyStyle.Hierarchical; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.mitoma.ponto; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.Properties; | ||
|
||
public enum KeyStyle { | ||
|
||
Flat { | ||
@Override | ||
public void writeMethods(PrintWriter pw, Properties properties) { | ||
for (Object key : properties.keySet()) { | ||
String keyString = key.toString(); | ||
String methodName = keyString.replace('.', '_'); | ||
pw.println(String.format( | ||
" public static String %s(){ return getProperties(\"%s\"); }", | ||
methodName, keyString)); | ||
} | ||
} | ||
}, | ||
Hierarchical { | ||
@Override | ||
public void writeMethods(PrintWriter pw, Properties properties) { | ||
Node root = new Node(null, ""); | ||
for (Object key : properties.keySet()) { | ||
String keyString = key.toString(); | ||
root.addKeyString(keyString); | ||
} | ||
writeNode(pw, root, 1); | ||
} | ||
|
||
private void writeNode(PrintWriter pw, Node node, int depth) { | ||
for (String methodName : node.getMethods()) { | ||
pw.print(indent(depth)); | ||
pw.println(String.format( | ||
"public static String %s(){ return getProperties(\"%s\"); }", | ||
methodName, String.format("%s.%s", node.getFullName(), methodName))); | ||
} | ||
for (Node child : node.getChilds()) { | ||
pw.print(indent(depth)); | ||
pw.println(String.format("public static class %s {", child.getName())); | ||
writeNode(pw, child, depth + 1); | ||
pw.print(indent(depth)); | ||
pw.println("}"); | ||
} | ||
} | ||
|
||
private String indent(int depth) { | ||
StringBuilder buf = new StringBuilder(); | ||
for (int i = 0; i < depth; i++) { | ||
buf.append(" "); | ||
} | ||
return buf.toString(); | ||
} | ||
|
||
}; | ||
|
||
public abstract void writeMethods(PrintWriter pw, Properties properties); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.mitoma.ponto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Node { | ||
|
||
private Node parent = null; | ||
private List<Node> childs = new ArrayList<>(); | ||
private List<String> methods = new ArrayList<>(); | ||
private String name; | ||
|
||
public Node(Node parent, String name) { | ||
if (parent == null) { | ||
return; | ||
} | ||
this.name = name; | ||
this.parent = parent; | ||
this.parent.childs.add(this); | ||
} | ||
|
||
public Node getParent() { | ||
return parent; | ||
} | ||
|
||
public List<Node> getChilds() { | ||
return childs; | ||
} | ||
|
||
public List<String> getMethods() { | ||
return methods; | ||
} | ||
|
||
public void addKeyString(String keyString) { | ||
String[] elems = keyString.split("\\."); | ||
addKeys(elems); | ||
} | ||
|
||
private void addKeys(String[] elems) { | ||
if (elems.length == 1) { | ||
methods.add(elems[0]); | ||
} else { | ||
Node child = getChildsOrCreate(elems[0]); | ||
child.addKeys(Arrays.copyOfRange(elems, 1, elems.length)); | ||
} | ||
} | ||
|
||
private Node getChildsOrCreate(String name) { | ||
for (Node child : childs) { | ||
if (child.name.equals(name)) { | ||
return child; | ||
} | ||
} | ||
return new Node(this, name); | ||
} | ||
|
||
public String getFullName() { | ||
if (parent == null) { | ||
return ""; | ||
} | ||
String parentFullName = parent.getFullName(); | ||
if (parentFullName.isEmpty()) { | ||
return name; | ||
} | ||
return String.format("%s.%s", parentFullName, name); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
Oops, something went wrong.