KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > support > SystemProperties


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching.support;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import javax.xml.transform.OutputKeys JavaDoc;
20 import javax.xml.transform.Transformer JavaDoc;
21 import javax.xml.transform.TransformerException JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.dom.DOMSource JavaDoc;
24 import javax.xml.transform.stream.StreamResult JavaDoc;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Evaluates system properties passed as program arguments.
31  *
32  * @since 3.2
33  */

34 public class SystemProperties {
35
36     public static void main(String JavaDoc[] args) {
37         try {
38             Document JavaDoc doc = newDocument();
39             Element JavaDoc properties = doc.createElement("systemProperties"); //$NON-NLS-1$
40
doc.appendChild(properties);
41             for (int i = 0; i < args.length; i++) {
42                 String JavaDoc name = args[i];
43                 String JavaDoc value = System.getProperty(name);
44                 if (value != null) {
45                     Element JavaDoc property = doc.createElement("property"); //$NON-NLS-1$
46
property.setAttribute("name", name); //$NON-NLS-1$
47
property.setAttribute("value", value); //$NON-NLS-1$
48
properties.appendChild(property);
49                 }
50             }
51             String JavaDoc text = serializeDocument(doc);
52             System.out.print(text);
53         } catch (ParserConfigurationException JavaDoc e) {
54         } catch (IOException JavaDoc e) {
55         } catch (TransformerException JavaDoc e) {
56         }
57     }
58     
59     /**
60      * Returns a a new XML document
61      * @return document
62      * @throws ParserConfigurationException if an exception occurs creating the document builder
63      */

64     private static Document JavaDoc newDocument() throws ParserConfigurationException JavaDoc {
65         DocumentBuilderFactory JavaDoc dfactory= DocumentBuilderFactory.newInstance();
66         DocumentBuilder JavaDoc docBuilder= dfactory.newDocumentBuilder();
67         Document JavaDoc doc= docBuilder.newDocument();
68         return doc;
69     }
70     
71     /**
72      * Serializes a XML document into a string - encoded in UTF8 format,
73      * with platform line separators.
74      *
75      * @param doc document to serialize
76      * @return the document as a string
77      */

78     private static String JavaDoc serializeDocument(Document JavaDoc doc) throws IOException JavaDoc, TransformerException JavaDoc {
79         ByteArrayOutputStream JavaDoc s= new ByteArrayOutputStream JavaDoc();
80         
81         TransformerFactory JavaDoc factory= TransformerFactory.newInstance();
82         Transformer JavaDoc transformer= factory.newTransformer();
83         transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
84
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
85

86         DOMSource JavaDoc source= new DOMSource JavaDoc(doc);
87         StreamResult JavaDoc outputTarget= new StreamResult JavaDoc(s);
88         transformer.transform(source, outputTarget);
89         
90         return s.toString("UTF8"); //$NON-NLS-1$
91
}
92 }
93
Popular Tags