📄 echoproperties.java
字号:
String message = "srcfile is a directory!"; if (failonerror) { throw new BuildException(message, getLocation()); } else { log(message, Project.MSG_ERR); } return; } if (inFile.exists() && !inFile.canRead()) { String message = "Can not read from the specified srcfile!"; if (failonerror) { throw new BuildException(message, getLocation()); } else { log(message, Project.MSG_ERR); } return; } FileInputStream in = null; try { in = new FileInputStream(inFile); Properties props = new Properties(); props.load(in); allProps.putAll(props); } catch (FileNotFoundException fnfe) { String message = "Could not find file " + inFile.getAbsolutePath(); if (failonerror) { throw new BuildException(message, fnfe, getLocation()); } else { log(message, Project.MSG_WARN); } return; } catch (IOException ioe) { String message = "Could not read file " + inFile.getAbsolutePath(); if (failonerror) { throw new BuildException(message, ioe, getLocation()); } else { log(message, Project.MSG_WARN); } return; } finally { FileUtils.close(in); } } Enumeration e = propertySets.elements(); while (e.hasMoreElements()) { PropertySet ps = (PropertySet) e.nextElement(); allProps.putAll(ps.getProperties()); } OutputStream os = null; try { if (destfile == null) { os = new ByteArrayOutputStream(); saveProperties(allProps, os); log(os.toString(), Project.MSG_INFO); } else { if (destfile.exists() && destfile.isDirectory()) { String message = "destfile is a directory!"; if (failonerror) { throw new BuildException(message, getLocation()); } else { log(message, Project.MSG_ERR); } return; } if (destfile.exists() && !destfile.canWrite()) { String message = "Can not write to the specified destfile!"; if (failonerror) { throw new BuildException(message, getLocation()); } else { log(message, Project.MSG_ERR); } return; } os = new FileOutputStream(this.destfile); saveProperties(allProps, os); } } catch (IOException ioe) { if (failonerror) { throw new BuildException(ioe, getLocation()); } else { log(ioe.getMessage(), Project.MSG_INFO); } } finally { if (os != null) { try { os.close(); } catch (IOException ex) { //ignore } } } } /** * Send the key/value pairs in the hashtable to the given output stream. * Only those properties matching the <tt>prefix</tt> constraint will be * sent to the output stream. * The output stream will be closed when this method returns. * * @param allProps propfile to save * @param os output stream * @throws IOException on output errors * @throws BuildException on other errors */ protected void saveProperties(Hashtable allProps, OutputStream os) throws IOException, BuildException { final List keyList = new ArrayList(allProps.keySet()); Collections.sort(keyList); Properties props = new Properties() { private static final long serialVersionUID = 5090936442309201654L; public Enumeration keys() { return CollectionUtils.asEnumeration(keyList.iterator()); } public Set entrySet() { Set result = super.entrySet(); if (JavaEnvUtils.isKaffe()) { TreeSet t = new TreeSet(new Comparator() { public int compare(Object o1, Object o2) { String key1 = (String) ((Map.Entry) o1).getKey(); String key2 = (String) ((Map.Entry) o2).getKey(); return key1.compareTo(key2); } }); t.addAll(result); result = t; } return result; } }; for (int i = 0; i < keyList.size(); i++) { String name = keyList.get(i).toString(); String value = allProps.get(name).toString(); props.setProperty(name, value); } if ("text".equals(format)) { jdkSaveProperties(props, os, "Ant properties"); } else if ("xml".equals(format)) { xmlSaveProperties(props, os); } } /** * a tuple for the sort list. */ private static final class Tuple implements Comparable { private String key; private String value; private Tuple(String key, String value) { this.key = key; this.value = value; } /** * Compares this object with the specified object for order. * @param o the Object to be compared. * @return a negative integer, zero, or a positive integer as this object is * less than, equal to, or greater than the specified object. * @throws ClassCastException if the specified object's type prevents it * from being compared to this Object. */ public int compareTo(Object o) { Tuple that = (Tuple) o; return key.compareTo(that.key); } } private List sortProperties(Properties props) { //sort the list. Makes SCM and manual diffs easier. List sorted = new ArrayList(props.size()); Enumeration e = props.propertyNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); sorted.add(new Tuple(name, props.getProperty(name))); } Collections.sort(sorted); return sorted; } /** * Output the properties as xml output. * @param props the properties to save * @param os the output stream to write to (Note this gets closed) * @throws IOException on error in writing to the stream */ protected void xmlSaveProperties(Properties props, OutputStream os) throws IOException { // create XML document Document doc = getDocumentBuilder().newDocument(); Element rootElement = doc.createElement(PROPERTIES); List sorted = sortProperties(props); // output properties Iterator iten = sorted.iterator(); while (iten.hasNext()) { Tuple tuple = (Tuple) iten.next(); Element propElement = doc.createElement(PROPERTY); propElement.setAttribute(ATTR_NAME, tuple.key); propElement.setAttribute(ATTR_VALUE, tuple.value); rootElement.appendChild(propElement); } Writer wri = null; try { wri = new OutputStreamWriter(os, "UTF8"); wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); (new DOMElementWriter()).write(rootElement, wri, 0, "\t"); wri.flush(); } catch (IOException ioe) { throw new BuildException("Unable to write XML file", ioe); } finally { FileUtils.close(wri); } } /** * JDK 1.2 allows for the safer method * <tt>Properties.store(OutputStream, String)</tt>, which throws an * <tt>IOException</tt> on an output error. * *@param props the properties to record *@param os record the properties to this output stream *@param header prepend this header to the property output *@exception IOException on an I/O error during a write. */ protected void jdkSaveProperties(Properties props, OutputStream os, String header) throws IOException { try { props.store(os, header); } catch (IOException ioe) { throw new BuildException(ioe, getLocation()); } finally { if (os != null) { try { os.close(); } catch (IOException ioex) { log("Failed to close output stream"); } } } } /** * Uses the DocumentBuilderFactory to get a DocumentBuilder instance. * * @return The DocumentBuilder instance */ private static DocumentBuilder getDocumentBuilder() { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (Exception e) { throw new ExceptionInInitializerError(e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -