1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.netbeans.modules.office.actions;
25 
26 import java.io.*;
27 import java.util.*;
28 import javax.swing.event.ChangeListener;
29 import javax.swing.event.ChangeEvent;
30 
31 import org.w3c.dom.Document;
32 import org.w3c.dom.NodeList;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
35 
36 import org.openide.filesystems.*;
37 import org.openide.xml.XMLUtil;
38 
39 public class ParcelDescriptorParserSupport
40     implements ParcelDescriptorParserCookie, FileChangeListener
41 {
42     private FileObject fo;
43     private Document document;
44     private Set listeners;
45 
ParcelDescriptorParserSupport(FileObject fo)46     public ParcelDescriptorParserSupport(FileObject fo)
47     {
48         this.fo = fo;
49         fo.addFileChangeListener(this);
50     }
51 
parseFile()52     private synchronized void parseFile()
53     {
54         File file = FileUtil.toFile(fo);
55         InputSource is;
56 
57         try {
58             is = new InputSource(new FileInputStream(file));
59         }
60         catch (FileNotFoundException fnfe) {
61             System.out.println("Couldn't find file: " + file.getName());
62             return;
63         }
64 
65         document = null;
66         try {
67             document = XMLUtil.parse(is, false, false, null, null);
68         }
69         catch (IOException ioe) {
70             System.out.println("IO Error parsing file: " + file.getName());
71         }
72         catch (SAXException se) {
73             System.out.println("Sax Error parsing file: " + file.getName());
74         }
75     }
76 
getScriptElements()77     public synchronized NodeList getScriptElements()
78     {
79         if (document == null)
80             parseFile();
81 
82         if (document != null)
83             return document.getElementsByTagName("script");
84         return null;
85     }
86 
addChangeListener(ChangeListener cl)87     public void addChangeListener(ChangeListener cl) {
88         if (listeners == null)
89             listeners = new HashSet();
90 
91         listeners.add(cl);
92     }
93 
removeChangeListener(ChangeListener cl)94     public void removeChangeListener(ChangeListener cl) {
95         if (listeners == null)
96             return;
97 
98         listeners.remove(cl);
99     }
100 
fileChanged(FileEvent fe)101     public void fileChanged(FileEvent fe) {
102         parseFile();
103 
104         if (listeners != null) {
105             Iterator iter = listeners.iterator();
106 
107             while (iter.hasNext())
108                 ((ChangeListener)iter.next()).stateChanged(new ChangeEvent(this));
109         }
110     }
111 
fileAttributeChanged(FileAttributeEvent fe)112     public void fileAttributeChanged(FileAttributeEvent fe) {}
fileDataCreated(FileEvent fe)113     public void fileDataCreated(FileEvent fe) {}
fileDeleted(FileEvent fe)114     public void fileDeleted(FileEvent fe) {}
fileFolderCreated(FileEvent fe)115     public void fileFolderCreated(FileEvent fe) {}
fileRenamed(FileRenameEvent fe)116     public void fileRenamed(FileRenameEvent fe) {}
117 }
118