KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > config > sync > Synchronizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  * $Id: Synchronizer.java,v 1.1 2007/01/10 12:03:36 dlipin Exp $
19  */

20
21 package org.netbeans.modules.j2ee.websphere6.config.sync;
22
23 import java.io.*;
24 import java.util.LinkedList JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.transform.*;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import org.openide.filesystems.FileChangeAdapter;
32 import org.openide.filesystems.FileEvent;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.w3c.dom.Document JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  *
40  * @author Dmitry Lipin
41  */

42 public abstract class Synchronizer {
43     
44     protected static final String JavaDoc BINDING_SEPARATOR = "__Bnd__";
45     
46     protected void saveDocument(Document JavaDoc document, File file) throws TransformerException, FileNotFoundException, IOException {
47         OutputStream outputStream = null;
48         
49         try {
50             TransformerFactory tf = TransformerFactory.newInstance();
51             Transformer transformer = tf.newTransformer();
52             outputStream = new FileOutputStream(file);
53             DOMSource JavaDoc ds = new DOMSource JavaDoc(document);
54             StreamResult JavaDoc sr = new StreamResult JavaDoc(outputStream);
55             transformer.transform(ds, sr);
56         } finally {
57             if (outputStream != null) {
58                 outputStream.close();
59             }
60         }
61     }
62     protected Document JavaDoc loadDocument(File file) {
63         Document JavaDoc res = null;
64         try {
65             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
66             // factory.setNamespaceAware(true);
67
DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
68             res = builder.parse(file);
69         } catch (SAXException JavaDoc ex) {
70         } catch (IOException ex) {
71         } catch (ParserConfigurationException JavaDoc ex) {
72         }
73         return res;
74     }
75     public abstract void syncDescriptors();
76     
77     private LinkedList JavaDoc <File> list = new LinkedList JavaDoc <File> ();
78     
79     public void addSyncFile(File file) {
80         FileObject obj = FileUtil.toFileObject(file);
81         if(!list.contains(file)) {
82             list.add(file);
83             obj.addFileChangeListener(new FileChangeAdapter() {
84                 public void fileChanged(FileEvent event) {
85                     syncDescriptors();
86                 }
87             });
88         }
89     }
90 }
91
Popular Tags