Source Code Cross Referenced for NMRStatisticsDataWriter.java in  » ESB » open-esb » com » sun » esb » management » common » data » helper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » open esb » com.sun.esb.management.common.data.helper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BEGIN_HEADER - DO NOT EDIT
003:         *
004:         * The contents of this file are subject to the terms
005:         * of the Common Development and Distribution License
006:         * (the "License").  You may not use this file except
007:         * in compliance with the License.
008:         *
009:         * You can obtain a copy of the license at
010:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011:         * See the License for the specific language governing
012:         * permissions and limitations under the License.
013:         *
014:         * When distributing Covered Code, include this CDDL
015:         * HEADER in each file and include the License file at
016:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017:         * If applicable add the following below this CDDL HEADER,
018:         * with the fields enclosed by brackets "[]" replaced with
019:         * your own identifying information: Portions Copyright
020:         * [year] [name of copyright owner]
021:         */
022:
023:        /*
024:         * @(#)NMRStatisticsDataWriter.java
025:         * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026:         *
027:         * END_HEADER - DO NOT EDIT
028:         */
029:        package com.sun.esb.management.common.data.helper;
030:
031:        import java.io.BufferedWriter;
032:        import java.io.File;
033:        import java.io.FileNotFoundException;
034:        import java.io.FileWriter;
035:        import java.io.IOException;
036:        import java.io.Serializable;
037:        import java.io.StringWriter;
038:        import java.io.Writer;
039:        import java.net.MalformedURLException;
040:        import java.net.URISyntaxException;
041:        import java.util.Map;
042:
043:        import javax.xml.parsers.DocumentBuilder;
044:        import javax.xml.parsers.DocumentBuilderFactory;
045:        import javax.xml.parsers.ParserConfigurationException;
046:        import javax.xml.transform.OutputKeys;
047:        import javax.xml.transform.Transformer;
048:        import javax.xml.transform.TransformerConfigurationException;
049:        import javax.xml.transform.TransformerException;
050:        import javax.xml.transform.TransformerFactory;
051:        import javax.xml.transform.dom.DOMSource;
052:        import javax.xml.transform.stream.StreamResult;
053:
054:        import org.w3c.dom.Document;
055:        import org.w3c.dom.Element;
056:        import org.xml.sax.SAXException;
057:
058:        import com.sun.esb.management.common.data.NMRStatisticsData;
059:
060:        /**
061:         * @author graj
062:         *
063:         */
064:        public class NMRStatisticsDataWriter implements 
065:                NMRStatisticsDataXMLConstants, Serializable {
066:
067:            static final long serialVersionUID = -1L;
068:
069:            static final String FILE_NAME_KEY = "NMRStatisticsData.xml";
070:
071:            /** Constructor - Creates an NMRStatisticsDataWriter */
072:            public NMRStatisticsDataWriter() {
073:            }
074:
075:            /**
076:             * 
077:             * @param document
078:             * @param directoryPath
079:             * @throws TransformerConfigurationException
080:             * @throws TransformerException
081:             * @throws Exception
082:             */
083:            public static void writeToFile(Document document,
084:                    String directoryPath)
085:                    throws TransformerConfigurationException,
086:                    TransformerException, Exception {
087:                File file = new File(directoryPath);
088:                if ((file.isDirectory() == false) || (file.exists() == false)) {
089:                    throw new Exception("Directory Path: " + directoryPath
090:                            + " is invalid.");
091:                }
092:                String fileLocation = file.getAbsolutePath() + File.separator
093:                        + FILE_NAME_KEY;
094:                System.out.println("Writing out to file: " + fileLocation);
095:                File outputFile = new File(fileLocation);
096:                // Use a Transformer for aspectOutput
097:                TransformerFactory tFactory = TransformerFactory.newInstance();
098:                Transformer transformer = tFactory.newTransformer();
099:                DOMSource source = new DOMSource(document);
100:                StreamResult result = new StreamResult(outputFile);
101:
102:                // indent the Output to make it more legible...
103:                transformer.setOutputProperty(OutputKeys.METHOD, "xml");
104:                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
105:                transformer
106:                        .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
107:                transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
108:                transformer.setOutputProperty(
109:                        "{http://xml.apache.org/xslt}indent-amount", "4");
110:                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
111:
112:                transformer.transform(source, result);
113:            }
114:
115:            /**
116:             * Change the contents of text file in its entirety, overwriting any
117:             * existing text. This style of implementation throws all exceptions to the
118:             * caller.
119:             * 
120:             * @param aFile
121:             *            is an existing file which can be written to.
122:             * @throws IllegalArgumentException
123:             *             if param does not comply.
124:             * @throws FileNotFoundException
125:             *             if the file does not exist.
126:             * @throws IOException
127:             *             if problem encountered during write.
128:             */
129:            public static void setContents(File aFile, String aContents)
130:                    throws FileNotFoundException, IOException {
131:                if (aFile == null) {
132:                    throw new IllegalArgumentException(
133:                            "File should not be null.");
134:                }
135:                if (!aFile.exists()) {
136:                    aFile.createNewFile();
137:                }
138:                if (!aFile.isFile()) {
139:                    throw new IllegalArgumentException(
140:                            "Should not be a directory: " + aFile);
141:                }
142:                if (!aFile.canWrite()) {
143:                    throw new IllegalArgumentException(
144:                            "File cannot be written: " + aFile);
145:                }
146:
147:                // declared here only to make visible to finally clause; generic
148:                // reference
149:                Writer output = null;
150:                try {
151:                    // use buffering
152:                    // FileWriter always assumes default encoding is OK!
153:                    output = new BufferedWriter(new FileWriter(aFile));
154:                    output.write(aContents);
155:                } finally {
156:                    // flush and close both "aspectOutput" and its underlying FileWriter
157:                    if (output != null) {
158:                        output.close();
159:                    }
160:                }
161:            }
162:
163:            /**
164:             * 
165:             * @param NMRStatisticsData
166:             *            data 
167:             * @return XML string
168:             * @throws ParserConfigurationException
169:             * @throws TransformerException
170:             */
171:            public static String serialize(
172:                    Map<String /* instanceName */, NMRStatisticsData> dataMap)
173:                    throws ParserConfigurationException, TransformerException {
174:                Document document = null;
175:                NMRStatisticsDataWriter writer = new NMRStatisticsDataWriter();
176:                if (dataMap != null) {
177:                    DocumentBuilderFactory factory = DocumentBuilderFactory
178:                            .newInstance();
179:                    DocumentBuilder builder = factory.newDocumentBuilder();
180:                    document = builder.newDocument(); // Create from whole cloth
181:
182:                    // ////////////////////////////////
183:                    // <NMRStatisticsDataList>
184:                    Element root = (Element) document
185:                            .createElement(NMR_STATISTICS_DATA_LIST_KEY);
186:                    // xmlns="http://java.sun.com/xml/ns/esb/management/NMRStatisticsDataList"
187:                    root.setAttribute(NAMESPACE_KEY, NAMESPACE_VALUE);
188:                    // version = "1.0"
189:                    root.setAttribute(VERSION_KEY, VERSION_VALUE);
190:
191:                    for (String instanceName : dataMap.keySet()) {
192:                        NMRStatisticsData data = dataMap.get(instanceName);
193:                        // ////////////////////////////////
194:                        // <NMRStatisticsData>
195:                        Element nmrStatisticsDataElementChild = writer
196:                                .createNMRStatisticsDataElement(document, data);
197:                        // </NMRStatisticsData>
198:                        root.appendChild(nmrStatisticsDataElementChild);
199:                        // ////////////////////////////////
200:                    }
201:                    // ////////////////////////////////
202:                    // </NMRStatisticsDataList>
203:                    document.appendChild(root);
204:                    // ////////////////////////////////
205:
206:                }
207:                return writer.writeToString(document);
208:            }
209:
210:            /**
211:             * 
212:             * @param document
213:             * @param data
214:             * @return
215:             */
216:            protected Element createNMRStatisticsDataElement(Document document,
217:                    NMRStatisticsData data) {
218:                Element nmrStatisticsDataElement = null;
219:                if ((document != null) && (data != null)) {
220:
221:                    // <NMRStatisticsData>
222:                    nmrStatisticsDataElement = document
223:                            .createElement(NMR_STATISTICS_DATA_KEY);
224:
225:                    // <InstanceName>
226:                    Element instanceNameElementChild = document
227:                            .createElement(INSTANCE_NAME_KEY);
228:                    if (instanceNameElementChild != null) {
229:                        instanceNameElementChild.setTextContent(data
230:                                .getInstanceName());
231:                    }
232:                    // </InstanceName>
233:                    nmrStatisticsDataElement
234:                            .appendChild(instanceNameElementChild);
235:
236:                    // <ActiveChannelsList>
237:                    Element activeChannelsListElementChild = createActiveChannelsList(
238:                            document, data);
239:                    if (activeChannelsListElementChild != null) {
240:                        nmrStatisticsDataElement
241:                                .appendChild(activeChannelsListElementChild);
242:                    }
243:                    // </ActiveChannelsList>
244:
245:                    // <ActiveEndpointsList>
246:                    Element activeEndpointsListElementChild = createActiveEndpointsList(
247:                            document, data);
248:                    if (activeEndpointsListElementChild != null) {
249:                        nmrStatisticsDataElement
250:                                .appendChild(activeEndpointsListElementChild);
251:                    }
252:                    // </ActiveEndpointsList>
253:                }
254:                return nmrStatisticsDataElement;
255:            }
256:
257:            /**
258:             * Create Active Channels List
259:             * 
260:             * @param document
261:             * @param data
262:             * @return
263:             */
264:            protected static Element createActiveChannelsList(
265:                    Document document, NMRStatisticsData data) {
266:                Element activeChannelsListElement = null;
267:                if ((document != null) && (data != null)) {
268:                    // <ActiveChannelsList>
269:                    activeChannelsListElement = document
270:                            .createElement(ACTIVE_CHANNELS_LIST_KEY);
271:                    if ((data.getActiveChannelsList() != null)
272:                            && (data.getActiveChannelsList().size() > 0)) {
273:                        for (String channel : data.getActiveChannelsList()) {
274:                            // <ActiveChannel>
275:                            Element activeChannelElementChild = document
276:                                    .createElement(ACTIVE_CHANNEL_KEY);
277:                            if (activeChannelElementChild != null) {
278:                                activeChannelElementChild
279:                                        .setTextContent(channel);
280:                            }
281:                            // </ActiveChannel>
282:                            activeChannelsListElement
283:                                    .appendChild(activeChannelElementChild);
284:                        }
285:                    }
286:                }
287:                return activeChannelsListElement;
288:            }
289:
290:            /**
291:             * Create Active Channels List
292:             * 
293:             * @param document
294:             * @param data
295:             * @return
296:             */
297:            protected static Element createActiveEndpointsList(
298:                    Document document, NMRStatisticsData data) {
299:                Element activeEndpointsListElement = null;
300:                if ((document != null) && (data != null)) {
301:                    // <ActiveEndpointsList>
302:                    activeEndpointsListElement = document
303:                            .createElement(ACTIVE_ENDPOINTS_LIST_KEY);
304:                    if ((data.getActiveEndpointsList() != null)
305:                            && (data.getActiveEndpointsList().size() > 0)) {
306:                        for (String endpoint : data.getActiveEndpointsList()) {
307:                            // <ActiveEndpoint>
308:                            Element activeEndpointElementChild = document
309:                                    .createElement(ACTIVE_ENDPOINT_KEY);
310:                            if (activeEndpointElementChild != null) {
311:                                activeEndpointElementChild
312:                                        .setTextContent(endpoint);
313:                            }
314:                            // </ActiveEndpoint>
315:                            activeEndpointsListElement
316:                                    .appendChild(activeEndpointElementChild);
317:                        }
318:                    }
319:                }
320:                return activeEndpointsListElement;
321:            }
322:
323:            /**
324:             * @param document
325:             * @return
326:             * @throws TransformerException
327:             */
328:            protected String writeToString(Document document)
329:                    throws TransformerException {
330:                // Use a Transformer for aspectOutput
331:                TransformerFactory tFactory = TransformerFactory.newInstance();
332:                Transformer transformer = tFactory.newTransformer();
333:                DOMSource source = new DOMSource(document);
334:                StringWriter writer = new StringWriter();
335:                StreamResult result = new StreamResult(writer);
336:
337:                transformer.setOutputProperty(OutputKeys.METHOD, "xml");
338:                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
339:                transformer
340:                        .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
341:                transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
342:
343:                // indent the aspectOutput to make it more legible...
344:                transformer.setOutputProperty(
345:                        "{http://xml.apache.org/xslt}indent-amount", "4");
346:                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
347:                transformer.transform(source, result);
348:
349:                return result.getWriter().toString();
350:            }
351:
352:            /**
353:             * @param args
354:             */
355:            public static void main(String[] args) {
356:                String uri = "C:/test/schema/nmrstatistics/NMRStatisticsData.xml";
357:                try {
358:                    Map<String /* instanceName */, NMRStatisticsData> map = null;
359:                    map = NMRStatisticsDataReader.parseFromFile(uri);
360:                    for (String instanceName : map.keySet()) {
361:                        System.out.println(map.get(instanceName)
362:                                .getDisplayString());
363:                    }
364:
365:                    String content = NMRStatisticsDataWriter.serialize(map);
366:                    System.out.println(content);
367:                    NMRStatisticsDataWriter.setContents(new File(uri), content);
368:
369:                } catch (MalformedURLException e) {
370:                    e.printStackTrace();
371:                } catch (FileNotFoundException e) {
372:                    e.printStackTrace();
373:                } catch (ParserConfigurationException e) {
374:                    e.printStackTrace();
375:                } catch (SAXException e) {
376:                    e.printStackTrace();
377:                } catch (URISyntaxException e) {
378:                    e.printStackTrace();
379:                } catch (IOException e) {
380:                    e.printStackTrace();
381:                } catch (TransformerException e) {
382:                    e.printStackTrace();
383:                }
384:            }
385:
386:        }
w_w__w.___j__a_v__a___2__s___._c_o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.