📄 antstructure.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.IntrospectionHelper;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.TaskContainer;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.Reference;/** * Creates a partial DTD for Ant from the currently known tasks. * * * @since Ant 1.1 * * @ant.task category="xml" */public class AntStructure extends Task { private static final String LINE_SEP = System.getProperty("line.separator"); private File output; private StructurePrinter printer = new DTDPrinter(); /** * The output file. * @param output the output file */ public void setOutput(File output) { this.output = output; } /** * The StructurePrinter to use. * @param p the printer to use. * @since Ant 1.7 */ public void add(StructurePrinter p) { printer = p; } /** * Build the antstructure DTD. * * @exception BuildException if the DTD cannot be written. */ public void execute() throws BuildException { if (output == null) { throw new BuildException("output attribute is required", getLocation()); } PrintWriter out = null; try { try { out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF8")); } catch (UnsupportedEncodingException ue) { /* * Plain impossible with UTF8, see * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html * * fallback to platform specific anyway. */ out = new PrintWriter(new FileWriter(output)); } printer.printHead(out, getProject(), getProject().getTaskDefinitions(), getProject().getDataTypeDefinitions()); printer.printTargetDecl(out); Enumeration dataTypes = getProject().getDataTypeDefinitions().keys(); while (dataTypes.hasMoreElements()) { String typeName = (String) dataTypes.nextElement(); printer.printElementDecl( out, getProject(), typeName, (Class) getProject().getDataTypeDefinitions().get(typeName)); } Enumeration tasks = getProject().getTaskDefinitions().keys(); while (tasks.hasMoreElements()) { String tName = (String) tasks.nextElement(); printer.printElementDecl(out, getProject(), tName, (Class) getProject().getTaskDefinitions().get(tName)); } printer.printTail(out); } catch (IOException ioe) { throw new BuildException("Error writing " + output.getAbsolutePath(), ioe, getLocation()); } finally { if (out != null) { out.close(); } } } /** * Writes the actual structure information. * * <p>{@link #printHead}, {@link #printTargetDecl} and {@link #printTail} * are called exactly once, {@link #printElementDecl} once for * each declared task and type.</p> */ public static interface StructurePrinter { /** * Prints the header of the generated output. * * @param out PrintWriter to write to. * @param p Project instance for the current task * @param tasks map (name to implementing class) * @param types map (name to implementing class) * data types. */ void printHead(PrintWriter out, Project p, Hashtable tasks, Hashtable types); /** * Prints the definition for the target element. * @param out PrintWriter to write to. */ void printTargetDecl(PrintWriter out); /** * Print the definition for a given element. * * @param out PrintWriter to write to. * @param p Project instance for the current task * @param name element name. * @param element class of the defined element. */ void printElementDecl(PrintWriter out, Project p, String name, Class element); /** * Prints the trailer. * @param out PrintWriter to write to. */ void printTail(PrintWriter out); } private static class DTDPrinter implements StructurePrinter { private static final String BOOLEAN = "%boolean;"; private static final String TASKS = "%tasks;"; private static final String TYPES = "%types;"; private Hashtable visited = new Hashtable(); public void printTail(PrintWriter out) { visited.clear(); } public void printHead(PrintWriter out, Project p, Hashtable tasks, Hashtable types) { printHead(out, tasks.keys(), types.keys()); } /** * Prints the header of the generated output. * * <p>Basically this prints the XML declaration, defines some * entities and the project element.</p> */ private void printHead(PrintWriter out, Enumeration tasks, Enumeration types) { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); out.println("<!ENTITY % boolean \"(true|false|on|off|yes|no)\">"); out.print("<!ENTITY % tasks \""); boolean first = true; while (tasks.hasMoreElements()) { String tName = (String) tasks.nextElement(); if (!first) { out.print(" | "); } else { first = false; } out.print(tName); } out.println("\">"); out.print("<!ENTITY % types \""); first = true; while (types.hasMoreElements()) { String typeName = (String) types.nextElement(); if (!first) { out.print(" | "); } else { first = false; } out.print(typeName); } out.println("\">"); out.println(""); out.print("<!ELEMENT project (target | "); out.print(TASKS); out.print(" | "); out.print(TYPES); out.println(")*>"); out.println("<!ATTLIST project"); out.println(" name CDATA #IMPLIED"); out.println(" default CDATA #IMPLIED");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -