2
0
Fork 0

Added i18n lib and added paging on mongo and meta backends.

This commit is contained in:
Willem Cazander 2012-05-08 04:38:07 +02:00
parent fe5842768f
commit efcbdbd519
45 changed files with 1767 additions and 28 deletions

View file

@ -1,241 +0,0 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.annotations;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
/**
* Parses the Vasc annotations and generates all i18n keys for you.
*
* @author Willem Cazander
* @version 1.0 Mar 23, 2009
*/
public class VascBundleKeyGenerator {
private List<Class<?>> models = null;
private Map<String,String> keys = null;
private boolean generateName = true;
private boolean generateDescription = true;
private boolean generateImage = true;
private boolean generateHelpId = true;
private List<String> dubId = null;
public VascBundleKeyGenerator() {
models = new ArrayList<Class<?>>(30);
keys = new HashMap<String,String>(300);
dubId = new ArrayList<String>(30);
}
public void addModelClass(Class<?> model) {
models.add(model);
}
public List<Class<?>> getModelClasses() {
return models;
}
public void generateMissingKeys(StringBuffer buffer,ResourceBundle bundle) {
if (keys.isEmpty()==false) {
keys.clear();
dubId.clear();
}
for (Class<?> modelClass:models) {
generatorI18nKeys(modelClass);
}
List<String> keys2 = new ArrayList<String>(keys.keySet());
keys2.removeAll(bundle.keySet());
Collections.sort(keys2);
for (String key:keys2) {
String value = keys.get(key);
buffer.append(key);
buffer.append(" = ");
buffer.append(value);
buffer.append('\n');
}
}
public void generateRemoveKeys(StringBuffer buffer,ResourceBundle bundle,String excludeRegex) {
if (keys.isEmpty()==false) {
keys.clear();
dubId.clear();
}
for (Class<?> modelClass:models) {
generatorI18nKeys(modelClass);
}
List<String> keys2 = new ArrayList<String>(bundle.keySet());
keys2.removeAll(keys.keySet());
Collections.sort(keys2);
for (String key:keys2) {
// exlude code and user keys
if (excludeRegex!=null && key.matches(excludeRegex)) {
continue;
}
buffer.append(key);
buffer.append('\n');
}
}
public void generateKeys(StringBuffer buffer) {
if (keys.isEmpty()==false) {
keys.clear();
dubId.clear();
}
for (Class<?> modelClass:models) {
generatorI18nKeys(modelClass);
}
List<String> keys2 = new ArrayList<String>(keys.keySet());
Collections.sort(keys2);
for (String key:keys2) {
String value = keys.get(key);
buffer.append(key);
buffer.append(" = ");
buffer.append(value);
buffer.append('\n');
}
}
private void generatorI18nKeys(Class<?> bean) {
VascAnnotationParser vap = new VascAnnotationParser();
String prop1 = bean.getName().substring(bean.getName().lastIndexOf('.')+1);
if (isGenerateName()) {
appendKey(vap.getVascI18nName(bean),prop1);
}
if (isGenerateDescription()) {
appendKey(vap.getVascI18nDescription(bean),prop1);
}
if (isGenerateImage()) {
appendKey(vap.getVascI18nImage(bean),"resources/images/models/"+prop1+".png");
}
if (isGenerateHelpId()) {
appendKey(vap.getVascI18nHelpId(bean),prop1);
}
String idKey = vap.getVascPrimaryKey(bean);
String nameKey = vap.getVascDisplayName(bean);
if (idKey!=null && idKey.equals(nameKey)) {
dubId.add("idKey and nameKey is the same: "+bean.getName());
}
for (Method method:bean.getMethods()) {
if (method.getName().startsWith("get")==false) { //a bit durty
continue;
}
if (method.getName().equals("getClass")==true) {
continue;
}
String prop = method.getName().substring(3);
prop = prop.substring(0,1).toLowerCase()+prop.substring(1);
if (isGenerateName()) {
appendKey(vap.getVascI18nName(bean, prop),prop);
}
if (isGenerateDescription()) {
appendKey(vap.getVascI18nDescription(bean, prop),prop);
}
if (isGenerateImage()) {
appendKey(vap.getVascI18nImage(bean, prop),"resources/images/models/"+prop1+"-"+prop+".png");
}
if (isGenerateHelpId()) {
appendKey(vap.getVascI18nHelpId(bean),prop);
}
}
}
private void appendKey(String key,String value) {
if (keys.containsKey(key)) {
return;
}
keys.put(key, value);
}
/**
* @return the generateName
*/
public boolean isGenerateName() {
return generateName;
}
/**
* @param generateName the generateName to set
*/
public void setGenerateName(boolean generateName) {
this.generateName = generateName;
}
/**
* @return the generateDescription
*/
public boolean isGenerateDescription() {
return generateDescription;
}
/**
* @param generateDescription the generateDescription to set
*/
public void setGenerateDescription(boolean generateDescription) {
this.generateDescription = generateDescription;
}
/**
* @return the generateImage
*/
public boolean isGenerateImage() {
return generateImage;
}
/**
* @param generateImage the generateImage to set
*/
public void setGenerateImage(boolean generateImage) {
this.generateImage = generateImage;
}
/**
* @return the generateHelpId
*/
public boolean isGenerateHelpId() {
return generateHelpId;
}
/**
* @param generateHelpId the generateHelpId to set
*/
public void setGenerateHelpId(boolean generateHelpId) {
this.generateHelpId = generateHelpId;
}
public List<String> getDubIdNameKeys() {
return dubId;
}
}