1 11 package org.eclipse.jdt.internal.launching.support; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.IOException ; 15 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 import javax.xml.transform.OutputKeys ; 20 import javax.xml.transform.Transformer ; 21 import javax.xml.transform.TransformerException ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.dom.DOMSource ; 24 import javax.xml.transform.stream.StreamResult ; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 29 34 public class SystemProperties { 35 36 public static void main(String [] args) { 37 try { 38 Document doc = newDocument(); 39 Element properties = doc.createElement("systemProperties"); doc.appendChild(properties); 41 for (int i = 0; i < args.length; i++) { 42 String name = args[i]; 43 String value = System.getProperty(name); 44 if (value != null) { 45 Element property = doc.createElement("property"); property.setAttribute("name", name); property.setAttribute("value", value); properties.appendChild(property); 49 } 50 } 51 String text = serializeDocument(doc); 52 System.out.print(text); 53 } catch (ParserConfigurationException e) { 54 } catch (IOException e) { 55 } catch (TransformerException e) { 56 } 57 } 58 59 64 private static Document newDocument() throws ParserConfigurationException { 65 DocumentBuilderFactory dfactory= DocumentBuilderFactory.newInstance(); 66 DocumentBuilder docBuilder= dfactory.newDocumentBuilder(); 67 Document doc= docBuilder.newDocument(); 68 return doc; 69 } 70 71 78 private static String serializeDocument(Document doc) throws IOException , TransformerException { 79 ByteArrayOutputStream s= new ByteArrayOutputStream (); 80 81 TransformerFactory factory= TransformerFactory.newInstance(); 82 Transformer transformer= factory.newTransformer(); 83 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 86 DOMSource source= new DOMSource (doc); 87 StreamResult outputTarget= new StreamResult (s); 88 transformer.transform(source, outputTarget); 89 90 return s.toString("UTF8"); } 92 } 93 | Popular Tags |