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("[email protected]");
		// 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();
		}
	}
}
 

你可能感兴趣的:(xml,脚本,ubuntu,Blog)