use of org.dom4j.Document 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.Document 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.Document in project CodeUtils by boredream.
the class AndroidUtils method extract2valuesByJsoup.
/**
* 将参数值抽取到values文件夹下
* <p>如textColor="#ff00aa",将#ff00aa这样的具体值替换为@color/colorname
* <br>并在color.xml文件内创建一个对应颜色item
*
* @param proPath 项目绝对路径
* @param valuesXml values文件名称,如strings.xml dimens.xml等
* @param type 抽取内容的前缀,如@color/,则type参数就输入"color"
* @param itemName values文件内item的名称
* @param itemAttrName values文件内item的参数,一般都是name
* @param itemAttrValue values文件内item的参数值,也是抽取值后替换的名称
* @param itemValue values文件内item的值,即替换后的值
* @param values 被替换的内容,支持模糊替换,只要匹配集合里中其中一个就会被抽取替换,最终抽取成一个值itemValue
* @param matchAttr 匹配参数名,即只会替换该参数名对应的值
*/
@SuppressWarnings("unchecked")
public static void extract2valuesByJsoup(String proPath, String valuesXml, String type, String itemName, String itemAttrName, String itemAttrValue, String itemValue, List<String> values, String matchAttr) {
List<File> files = FileUtils.getAllFiles(new File(proPath));
String valuesPath = proPath + "/res/values/" + valuesXml;
File valuesFile = new File(valuesPath);
if (!valuesFile.exists()) {
System.out.println("文件不存在,请确定文件[" + valuesXml + "]位于/res/values/文件夹下,且文件名称正确");
return;
}
int extractFileCount = 0;
for (File file : files) {
if (!file.getName().endsWith(".xml")) {
continue;
}
if (file.getName().equals(valuesXml)) {
continue;
}
Document tarDoc = XmlUtil.read(file);
// 是否有替换操作
boolean isReplace = XmlUtil.replaceAttrValue(tarDoc, values, "@" + type + "/" + itemAttrValue, matchAttr);
if (!isReplace) {
continue;
}
XmlUtil.write2xml(file, tarDoc);
Document valuesDoc = XmlUtil.read(valuesFile);
Element rootElement = valuesDoc.getRootElement();
List<Element> elements = rootElement.elements();
// 是否在values/xx.xml对应文件下下已有某个抽取过的值
boolean hasInValues = false;
for (Element element : elements) {
String attrValue = element.attributeValue(itemAttrName);
if (attrValue.equals(itemAttrValue)) {
hasInValues = true;
break;
}
}
if (!hasInValues) {
Element element = rootElement.addElement(itemName);
element.addAttribute(itemAttrName, itemAttrValue);
element.setText(itemValue);
XmlUtil.write2xml(valuesFile, valuesDoc);
}
extractFileCount++;
}
System.out.println("将" + values + "抽取为[" + valuesXml + "]文件下的[" + itemAttrValue + "=" + itemValue + "]");
System.out.println("共抽取了" + extractFileCount + "个文件下的内容");
System.out.println("-------------------------");
}
use of org.dom4j.Document in project CodeUtils by boredream.
the class TempUtils method exportXml.
public static void exportXml() {
// save to
File file = new File("D:\\workproject\\businessCMT\\src\\main\\res\\values\\strings.xml");
Document valuesDoc = XmlUtil.read(file);
Element rootElement = valuesDoc.getRootElement();
String regexChinese = "[一-龥]+";
Pattern patternChiese = Pattern.compile(regexChinese);
List<Element> elements = rootElement.elements();
List<String> values = new ArrayList<String>();
for (Element e : elements) {
String text = e.getText();
Matcher matcher = patternChiese.matcher(text);
if (matcher.find() && !values.contains(text)) {
values.add(text);
System.out.println(text);
}
}
}
use of org.dom4j.Document in project pentaho-platform by pentaho.
the class EmailService method setEmailConfig.
public void setEmailConfig(final IEmailConfiguration emailConfiguration) {
if (emailConfiguration == null) {
throw new IllegalArgumentException(messages.getErrorString("EmailService.ERROR_0002_NULL_CONFIGURATION"));
}
final Document document = EmailConfigurationXml.getDocument(emailConfiguration);
try {
emailConfigFile.createNewFile();
final FileOutputStream fileOutputStream = new FileOutputStream(emailConfigFile);
XmlDom4JHelper.saveDom(document, fileOutputStream, "UTF-8");
fileOutputStream.close();
} catch (IOException e) {
logger.error(messages.getErrorString("EmailService.ERROR_0003_ERROR_CREATING_EMAIL_CONFIG_FILE", e.getLocalizedMessage()));
}
}
Aggregations