use of org.dom4j.Attribute in project eweb4j-framework by laiweiwei.
the class BeanXMLReader method readRecursion.
@SuppressWarnings("unchecked")
private <T> T readRecursion(Element bean) throws Exception {
Class<T> clazz = (Class<T>) this.classes.get(bean.getName());
T o = clazz.newInstance();
ReflectUtil ru = new ReflectUtil(o);
Field[] fields = ru.getFields();
for (Field f : fields) {
String n = f.getName();
Method m = ru.getSetter(n);
if (m == null)
continue;
Skip skip = f.getAnnotation(Skip.class);
if (skip != null)
continue;
AttrTag attrTag = f.getAnnotation(AttrTag.class);
Writeonly writeonly = f.getAnnotation(Writeonly.class);
if (writeonly != null)
continue;
if (attrTag != null) {
if ("clazz".equals(n))
n = "class";
Attribute a = bean.attribute(n);
if (a != null)
m.invoke(o, a.getData());
} else if (ClassUtil.isPojo(f.getType())) {
Element el = bean.element(n);
if (el == null)
continue;
String cls = this.classes.get(el.getName()).getName();
Object object = Thread.currentThread().getContextClassLoader().loadClass(cls).newInstance();
// 递归
object = readRecursion(el);
m.invoke(o, object);
} else if (ClassUtil.isListClass(f)) {
List<?> eList = bean.elements(n);
if (eList == null)
continue;
List<Object> list = new ArrayList<Object>();
for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
Element e = (Element) it.next();
// 递归
list.add(readRecursion(e));
}
m.invoke(o, list);
} else if (ClassUtil.isListString(f)) {
List<?> eList = bean.elements(n);
if (eList == null)
continue;
List<String> list = new ArrayList<String>();
for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
Element e = (Element) it.next();
list.add(e.getText());
}
m.invoke(o, list);
} else {
if ("clazz".equals(n))
n = "class";
Element a = bean.element(n);
if (a == null)
continue;
m.invoke(o, String.valueOf(a.getData()));
}
}
return o;
}
use of org.dom4j.Attribute in project CodeUtils by boredream.
the class TempUtils method extractAllString.
public static void extractAllString() {
List<File> files = FileUtils.getAllFiles("D:\\work\\BusinessCMT2.0");
// List<File> files =
// FileUtils.getAllFiles("D:\\work\\BusinessCMT2.0\\src\\com\\imohoo\\BusinessCMT\\view\\activity\\pay");
Map<String, String> stringIdValueMap = new TreeMap<String, String>();
for (File file : files) {
if (file.getName().endsWith(".java") || file.getName().endsWith(".xml")) {
FileReader fr;
StringBuilder newFileContent = new StringBuilder();
/**
* 是否是页面类
*/
boolean isActivity = false;
/**
* 是否有替换string操作
*/
boolean isReplace = false;
/**
* 代码行数
*/
int totoalLine = 0;
/**
* string抽取的id后缀
*/
int stringIdEnd = 0;
try {
fr = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(fr);
String line;
// 是否属于多行注释
boolean multiLineAnno = false;
while ((line = bufferedreader.readLine()) != null) {
totoalLine++;
// 空行
if (line.trim().equals("")) {
newFileContent.append(line + "\n");
continue;
}
// 单行注释,//开头
if (line.trim().startsWith("//")) {
newFileContent.append(line + "\n");
continue;
}
// 如果还是在多行注释中
if (multiLineAnno) {
// 如果本行包含多行注释结束符,结束
if (line.contains("*/")) {
multiLineAnno = false;
}
newFileContent.append(line + "\n");
continue;
}
// 多行注释开始(包括/*和/**)
if (line.contains("/*")) {
multiLineAnno = true;
newFileContent.append(line + "\n");
continue;
}
// 判断是否为页面类
if (line.contains("extends") && line.contains("Activity")) {
isActivity = true;
}
// 中文字符"blablabla"
String regexEmoji = "\"([\\s\\S]+?)\"";
Pattern pattern = Pattern.compile(regexEmoji);
Matcher matcher = pattern.matcher(line);
String regexChinese = "[一-龥]+";
Pattern patternChiese = Pattern.compile(regexChinese);
// 如果该行包含字符串"blablabla"
while (matcher.find()) {
String chinese = matcher.group(1);
Matcher matcherChinese = patternChiese.matcher(chinese);
// TODO 如果包含的内容中没有中文,跳过
if (!matcherChinese.find()) {
continue;
}
// TODO 如果是日志内容,跳过
if (line.trim().startsWith("Log") || line.trim().startsWith("showLog")) {
continue;
}
if (file.getName().endsWith(".java")) {
// id规则,文件名小写_递增数字
String stringId = FileUtils.getName(file.getName().toLowerCase(Locale.CHINA)) + "_" + stringIdEnd++;
if (isActivity) {
// 如果是页面类,直接getResource
line = line.replace("\"" + chinese + "\"", "getResources().getString(R.string." + stringId + ")");
} else {
// 非页面类,用application去getResource
line = line.replace("\"" + chinese + "\"", "BusinessCMTApplication.getInstance().getResources().getString(R.string." + stringId + ")");
}
stringIdValueMap.put(stringId, chinese);
isReplace = true;
} else if (file.getName().endsWith(".xml")) {
// id规则,文件名小写_递增数字
String stringId = FileUtils.getName(file.getName().toLowerCase(Locale.CHINA)) + "_" + stringIdEnd++;
// xml用@string/blabla替换,注意,不替换""
line = line.replace(chinese, "@string/" + stringId);
stringIdValueMap.put(stringId, chinese);
isReplace = true;
}
}
newFileContent.append(line + "\n");
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (isReplace) {
// TODO 写入回文件,如果有替换操作
// FileUtils.writeString2File(newFileContent.toString(),
// file, "UTF-8");
}
}
}
System.out.println(stringIdValueMap);
// save to
File file = new File("D:\\work\\BusinessCMT2.0\\res\\values\\string.xml");
Document valuesDoc = XmlUtil.read(file);
Element rootElement = valuesDoc.getRootElement();
List<Element> elements = rootElement.elements();
for (Map.Entry<String, String> entry : stringIdValueMap.entrySet()) {
// 是否在values/xx.xml对应文件下下已有某个抽取过的值
boolean hasElement = false;
for (Element element : elements) {
Attribute nameAtt = element.attribute("name");
if (nameAtt.getValue().equals(entry.getKey())) {
hasElement = true;
break;
}
}
if (!hasElement) {
// <string name="app_name">Stone Chat</string>
Element element = rootElement.addElement("string");
element.addAttribute("name", entry.getKey());
element.setText(entry.getValue());
}
}
// TODO save string文件
// XmlUtil.write2xml(file, valuesDoc);
}
use of org.dom4j.Attribute in project CodeUtils by boredream.
the class AndroidUtils method parseElementFromXml.
/**
* 递归获取layout中全部控件
*
* @param layoutXml 布局文件的绝对路径,如xxx/res/layout/main.xml
* @param include 是否将include引用的布局中内容也获取到
*/
public static void parseElementFromXml(String layoutXml, boolean include) {
Document document = XmlUtil.read(layoutXml);
List<Element> docElements = XmlUtil.getAllElements(document);
// 将view名称和对应的id名封装为一个实体类,并存至集合中
for (Element element : docElements) {
Attribute attrID = element.attribute("id");
// 如果包含include并且需要获取其中内容则进行递归获取
if (element.getName().equals("include") && include) {
Attribute attribute = element.attribute("layout");
// 原布局路径和include中的布局拼成新的路径
String includeLayoutXml = layoutXml.substring(0, layoutXml.lastIndexOf("\\") + 1) + attribute.getValue().substring(attribute.getValue().indexOf("/") + 1) + ".xml";
// 继续递归获取include的布局中控件
parseElementFromXml(includeLayoutXml, include);
}
// 保存有id的控件信息
if (attrID != null) {
String value = attrID.getValue();
String idName = value.substring(value.indexOf("/") + 1);
IdNamingBean bean = new IdNamingBean(element.getName(), idName, element);
if (!idNamingBeans.contains(bean)) {
idNamingBeans.add(bean);
}
}
}
}
use of org.dom4j.Attribute in project CodeUtils by boredream.
the class AndroidUtils method createSelector.
/**
* 生成sel的document
*
* @param normalName normal普通状态的图片名
* @param specialName 特殊状态(pressed按下/checked选中)的图片名
* @param end 特殊状态(pressed按下/checked选中)后缀名
* @return
*/
public static Document createSelector(String normalName, String specialName, String end) {
Document doc = XmlUtil.read("res\\drawable\\sel.xml");
Element rootElement = doc.getRootElement();
List<Element> elements = XmlUtil.getAllElements(doc);
for (Element element : elements) {
Attribute attr = element.attribute("drawable");
if (attr == null) {
continue;
}
String value = attr.getStringValue();
if (value.contains(end)) {
// 替换特殊状态(pressed/checked)的item加后缀
value = value.replace(end, specialName);
attr.setValue(value);
} else if (element.attributeCount() > 1) {
// 移除不需要的element
rootElement.remove(element);
} else {
// normal状态的item不加后缀
value = value.replace("normal", normalName);
attr.setValue(value);
}
}
return doc;
}
use of org.dom4j.Attribute in project pentaho-platform by pentaho.
the class BiPlatformRepositoryClientNavigationService method createCmisObjectFromElement.
private CmisObject createCmisObjectFromElement(Element element, int depth) {
CmisObject object = new CmisObjectImpl();
CmisProperties properties = new CmisProperties();
List<CmisProperty> propList = properties.getProperties();
// is this a folder or a file?
boolean isDirectory = false;
// $NON-NLS-1$
Attribute attr = element.attribute("isDirectory");
if (attr != null) {
// $NON-NLS-1$
isDirectory = "true".equalsIgnoreCase(attr.getText());
}
// set the base properties
String objectId = getObjectId(element);
Calendar lastModifiedDate = getLastModifiedDate(element);
String name = getName(element);
String localizedName = getLocalizedName(element);
String extension = getExtension(element);
boolean visible = getVisible(element);
propList.add(new PropertyId(PropertiesBase.OBJECTID, objectId));
propList.add(new PropertyDateTime(PropertiesBase.LASTMODIFICATIONDATE, lastModifiedDate));
if (isDirectory) {
propList.add(new PropertyString(PropertiesBase.OBJECTTYPEID, CmisObject.OBJECT_TYPE_FOLDER));
} else {
propList.add(new PropertyString(PropertiesBase.OBJECTTYPEID, extension));
propList.add(new PropertyBoolean(PropertiesDocument.CONTENTSTREAMALLOWED, true));
}
propList.add(new PropertyString(CmisObject.NAME, name));
propList.add(new PropertyString(CmisObject.LOCALIZEDNAME, localizedName));
propList.add(new PropertyBoolean(CmisObject.VISIBLE, visible));
object.setProperties(properties);
return object;
}
Aggregations