ubuntu 下实现动态桌面壁纸 Dom4j 生成xml的实用脚本

package background;

import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class DynamicWallpapers {

	/**
	 * Generate XML
	 * 
	 * @param files
	 * @param filename
	 * @return
	 */
	public static int generateBackgroundXML(File[] files, String filename) {
		int returnValue = 0;
		Document document = DocumentHelper.createDocument();
		Element backgroundElement = document.addElement("background");
		backgroundElement.addComment("Dynamic Wallpaper Generator Tool");
		/**
		 * Set the current time <starttime> <year>210</year> <month>09</month>
		 * <day>10</day> <hour></hour> <minute></minute> <second></second>
		 * </starttime>
		 */
		Element starttimeElement = backgroundElement.addElement("starttime");
		Element yearElement = starttimeElement.addElement("year");
		yearElement.setText("2010");
		Element monthElement = starttimeElement.addElement("month");
		monthElement.setText("09");
		Element dayElement = starttimeElement.addElement("day");
		dayElement.setText("10");
		Element hourElement = starttimeElement.addElement("hour");
		hourElement.setText("15");
		Element minuteElement = starttimeElement.addElement("minute");
		minuteElement.setText("38");
		Element secondElement = starttimeElement.addElement("second");
		secondElement.setText("32");
		/**
		 * <author> <firstname>Echol</firstname> <lastname>Zhao</lastname>
		 * <blog></blog> <mail></mail> </author>
		 */
		Element authorElement = backgroundElement.addElement("author");
		Element firstnameElement = authorElement.addElement("firstname");
		firstnameElement.setText("Echol");
		Element lastnameElement = authorElement.addElement("lastname");
		lastnameElement.setText("Zhao");
		Element blogElement = authorElement.addElement("blog");
		blogElement.setText("http://hi.baidu.com/mcmlxxxviii");
		Element mailElement = authorElement.addElement("mail");
		mailElement.setText("mcmlxxxviii@126.com");
		// Template
		backgroundElement.addComment("This animation will start now.");
		/**
		 * <static> <duration></duration> <file></file> </static> <transition>
		 * <duration></duration> <from></from> <to></to> </transition>
		 */
		for (int i = 0; i < files.length; i++) {
			Element staticElement = backgroundElement.addElement("static");
			Element durationStaticElement = staticElement
					.addElement("duration");
			durationStaticElement.setText("1795.0");
			Element fileElement = staticElement.addElement("file");
			fileElement.setText(files[i].getAbsolutePath());
			Element transitionElement = backgroundElement
					.addElement("transition");
			Element durationTransitionElement = transitionElement
					.addElement("duration");
			durationTransitionElement.setText("5.0");
			Element fromElement = transitionElement.addElement("from");
			fromElement.setText(files[i].getAbsolutePath());
			Element toElement = transitionElement.addElement("to");
			// Determine whether a file is the last.
			if (i == (files.length - 1)) {
				toElement.setText(files[0].getAbsolutePath());
				break;
			}
			toElement.setText(files[i + 1].getAbsolutePath());
		}

		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter output = new XMLWriter(
					new FileWriter(new File(filename)), format);
			output.write(document);
			output.close();
			returnValue = 1;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	/**
	 * Get list of files
	 * 
	 * @param dir
	 * @param regex
	 * @return
	 */
	public static File[] fileList(File dir, final String regex) {
		return dir.listFiles(new FilenameFilter() {
			private Pattern pattern = Pattern.compile(regex);

			public boolean accept(File dir, String name) {
				return pattern.matcher(new File(name).getName()).matches();
			}
		});
	}

	public static File[] fileList(String path, final String regex) { // Overload
		return fileList(new File(path), regex);
	}

	public static void usage() {
		System.err
				.println("Usage:DynamicWallpapers /path/.../ \n"
						+ "(Default file name:background.xml) \n\n"
						+ "Usage:DynamicWallpapers /path/.../ /path/FileName(Example.xml) \n");
		System.exit(1);
	}

	public static void main(String[] args) {
		if (args.length < 1 || args.length >= 3)
			usage();
		else if (args.length == 1 && (new File(args[0])).isDirectory()) {
			int value = generateBackgroundXML(fileList(args[0], ".*\\.jpg"),
					args[0] + "/background.xml");
			if (value == 1)
				System.out.println(args[0] + "/background.xml\n"
						+ "Generate success...");
			else
				usage();
		} else if ((new File(args[0])).isDirectory()) {
			int value = generateBackgroundXML(fileList(args[0], ".*\\.jpg"),
					args[1]);
			if (value == 1)
				System.out.println(args[1] + "\n" + "Generate success...");
			else
				usage();
		}
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值