1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.gems;
|
---|
38 |
|
---|
39 | import org.apache.xerces.dom.*;// for new Documents
|
---|
40 | import org.w3c.dom.Document;
|
---|
41 | import org.w3c.dom.DocumentType;
|
---|
42 | import org.w3c.dom.Element;
|
---|
43 | import org.w3c.dom.Node;
|
---|
44 | import org.w3c.dom.Attr;
|
---|
45 | import org.w3c.dom.NamedNodeMap;
|
---|
46 | import org.w3c.dom.NodeList;
|
---|
47 |
|
---|
48 | import java.util.HashMap;
|
---|
49 | import java.util.ArrayList;
|
---|
50 |
|
---|
51 | import org.greenstone.gatherer.util.XMLTools;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
55 | */
|
---|
56 | public class MetadataSetInfo
|
---|
57 | extends AttributeContainer
|
---|
58 | {
|
---|
59 |
|
---|
60 | private String file_path="";
|
---|
61 |
|
---|
62 | private static ArrayList listeners = new ArrayList();
|
---|
63 |
|
---|
64 | private static MetadataSetModel metadata_model;
|
---|
65 |
|
---|
66 | private String current_language = GEMSConstants.DEFAULT_LANGUAGE;
|
---|
67 |
|
---|
68 | private boolean isNew = false;
|
---|
69 |
|
---|
70 | public MetadataSetInfo(){
|
---|
71 | super(GEMSConstants.SET_REQUIRED_ATTRIBUTES, GEMSConstants.SET_LANG_DEPEND_ATTR_NAMES);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public boolean isNamespaceAlreadyUsed(String namespace){
|
---|
75 | if(metadata_model != null) {
|
---|
76 | return metadata_model.getMetadataSetManager().isNamespaceAlreadyUsed(namespace);
|
---|
77 | }
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void setNew(boolean isNew){
|
---|
82 | this.isNew = isNew;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public boolean isNew(){
|
---|
86 | return isNew;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Document getMetadataSetDocument(){
|
---|
90 | return metadata_model.getMetadataSetDocument();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public MetadataSetModel getMetadataSetModel(){
|
---|
94 | return metadata_model;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void setMetadataSetModel(MetadataSetModel model){
|
---|
98 | metadata_model = model;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | public String getMetadataSetName(){
|
---|
103 | return getMetadataSetName(getCurrentLanguage());
|
---|
104 | }
|
---|
105 |
|
---|
106 | public String getMetadataSetName(String lang){
|
---|
107 | Attribute attr = getAttributeByNameAndLanguage(GEMSConstants.NAME_ATTRIBUTE,lang);
|
---|
108 | if (attr != null) {
|
---|
109 | return attr.getValue();
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | return GEMSConstants.UNKNOWN_NAME;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void setMetadataSetName(String name){
|
---|
117 | setMetadataSetName(name, getCurrentLanguage());
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void setMetadataSetName(String name,String lang){
|
---|
121 | Attribute attr = getAttributeByNameAndLanguage(GEMSConstants.NAME_ATTRIBUTE,lang);
|
---|
122 | if (attr != null){
|
---|
123 | attr.setValue(name);
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | attr = new Attribute(GEMSConstants.NAME_ATTRIBUTE, name, lang, true);
|
---|
127 | language_dependent_attributes.add(attr);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | public String getMetadataSetDescription(){
|
---|
134 | return getMetadataSetDescription(getCurrentLanguage());
|
---|
135 | }
|
---|
136 |
|
---|
137 | public String getMetadataSetDescription(String lang){
|
---|
138 | Attribute attr = getAttributeByNameAndLanguage(GEMSConstants.DESCRIPTION_ATTRIBUTE,lang);
|
---|
139 | if (attr != null) {
|
---|
140 | return attr.getValue();
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | return "";
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public void setMetadataSetDescription(String desc) {
|
---|
148 | setMetadataSetDescription(desc, getCurrentLanguage());
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void setMetadataSetDescription(String desc, String lang) {
|
---|
152 | Attribute attr = getAttributeByNameAndLanguage(GEMSConstants.DESCRIPTION_ATTRIBUTE,lang);
|
---|
153 | if (attr != null){
|
---|
154 | attr.setValue(desc);
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE, desc, lang, true);
|
---|
158 | language_dependent_attributes.add(attr);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public String getCurrentLanguage(){
|
---|
163 | return current_language;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void setCurrentLanguage(String lang){
|
---|
167 | current_language = lang;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public String getNamespace(){
|
---|
171 | Attribute attr = getAttributeByName(GEMSConstants.NAMESPACE_ATTRIBUTE);
|
---|
172 | if (attr != null){
|
---|
173 | return attr.getValue();
|
---|
174 | }
|
---|
175 | else{
|
---|
176 | return "";
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void setNamespace(String namespace){
|
---|
181 | Attribute attr = getAttributeByName(GEMSConstants.NAMESPACE_ATTRIBUTE);
|
---|
182 | if (attr != null){
|
---|
183 | attr.setValue(namespace);
|
---|
184 | }
|
---|
185 | else {
|
---|
186 | attr = new Attribute(GEMSConstants.NAMESPACE_ATTRIBUTE, namespace, true);
|
---|
187 | attributes.add(attr);
|
---|
188 | }
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | public String getFilePath(){
|
---|
194 | return file_path;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | public void infoChanged(){
|
---|
199 | metadata_model.valueChanged();
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void setFilePath(String path){
|
---|
203 | file_path = path;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | public void addMetadataSetListener(MetadataSetListener msl){
|
---|
208 | if (!listeners.contains(msl))
|
---|
209 | listeners.add(msl);
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void removeMetadataSetListener(MetadataSetListener msl){
|
---|
213 | listeners.remove(msl);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void removeAllMetadataSetListeners(){
|
---|
217 | listeners.clear();
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void deleteMetadataSet(){
|
---|
221 | metadata_model.getMetadataSetManager().deleteMetadataSet(this);
|
---|
222 |
|
---|
223 | if (metadata_model.getMetadataSetInfo() != null
|
---|
224 | && metadata_model.getMetadataSetInfo().getFilePath().equals(this.getFilePath())){
|
---|
225 | metadata_model.metadataSetDeleted();
|
---|
226 | MetadataSetEvent mse = new MetadataSetEvent(null);
|
---|
227 | for(int i=0;i<listeners.size();i++){
|
---|
228 | MetadataSetListener msl = (MetadataSetListener) listeners.get(i);
|
---|
229 | msl.metadataSetChanged(mse);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public void notifyListeners(){
|
---|
235 | MetadataSetEvent mse = new MetadataSetEvent(this);
|
---|
236 | for(int i=0;i<listeners.size();i++){
|
---|
237 | MetadataSetListener msl = (MetadataSetListener) listeners.get(i);
|
---|
238 | msl.metadataSetChanged(mse);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | public void save(){
|
---|
243 | Document doc = metadata_model.getMetadataSetDocument();
|
---|
244 |
|
---|
245 | //new metadata set
|
---|
246 | if (doc == null){
|
---|
247 | doc = new DocumentImpl();
|
---|
248 | DocumentType docType = new DocumentTypeImpl((DocumentImpl)doc,GEMSConstants.MATADATASET_NAME,null,GEMSConstants.SYSTEM_ID);
|
---|
249 | doc.appendChild(doc.createElement(GEMSConstants.METADATASET_ELEMENT));
|
---|
250 | doc.appendChild(docType);
|
---|
251 | metadata_model.setMetadataSetDocument(doc);
|
---|
252 | }
|
---|
253 |
|
---|
254 | Element doc_element = doc.getDocumentElement();
|
---|
255 | //remove all attributes
|
---|
256 | NamedNodeMap attrs = doc.getDocumentElement().getAttributes();
|
---|
257 | for (int i=0;i<attrs.getLength();i++){
|
---|
258 | Attr item = (Attr)attrs.item(i);
|
---|
259 | doc_element.removeAttribute(item.getName());
|
---|
260 | }
|
---|
261 |
|
---|
262 | //add attributes
|
---|
263 | for (int i=0;i<attributes.size();i++){
|
---|
264 | Attribute attr = (Attribute)attributes.get(i);
|
---|
265 | if (attr.getName() == null || attr.getName().trim().equals("")) continue;
|
---|
266 | doc_element.setAttribute(attr.getName(),attr.getValue());
|
---|
267 | }
|
---|
268 |
|
---|
269 | //remove all language dependent name and description elements
|
---|
270 | NodeList setLanguages = doc_element.getElementsByTagName(GEMSConstants.SET_LANGUAGE_ELEMENT);
|
---|
271 |
|
---|
272 | for (int i=setLanguages.getLength()-1;i>=0;--i){
|
---|
273 | Node set_language = setLanguages.item(i);
|
---|
274 | doc_element.removeChild(set_language);
|
---|
275 | }
|
---|
276 |
|
---|
277 | ArrayList names = XMLTools.getChildElementsByTagName(doc_element,GEMSConstants.NAME_ELEMENT);
|
---|
278 | for(int i=0;i< names.size();i++){
|
---|
279 | Element name_element = (Element)names.get(i);
|
---|
280 | doc_element.removeChild(name_element);
|
---|
281 | }
|
---|
282 |
|
---|
283 | ArrayList deses = XMLTools.getChildElementsByTagName(doc_element,GEMSConstants.DESCRIPTION_ELEMENT);
|
---|
284 | for(int i=0;i< deses.size();i++){
|
---|
285 | Element des_element = (Element)deses.get(i);
|
---|
286 | doc_element.removeChild(des_element);
|
---|
287 | }
|
---|
288 |
|
---|
289 | Node first_child = doc_element.getFirstChild();
|
---|
290 |
|
---|
291 | Node previous_element = null;
|
---|
292 |
|
---|
293 | //add setLanguage elements
|
---|
294 | ArrayList langs = getAllLanguages();
|
---|
295 | for (int i=0;i<langs.size();i++){
|
---|
296 | String lang = (String)langs.get(i);
|
---|
297 | Attribute name = getAttributeByNameAndLanguage(GEMSConstants.NAME_ATTRIBUTE,lang);
|
---|
298 | if (name !=null){
|
---|
299 | Element set_element = doc.createElement(GEMSConstants.SET_LANGUAGE_ELEMENT);
|
---|
300 | set_element.setAttribute(GEMSConstants.CODE_ATTRIBUTE,lang);
|
---|
301 | Element name_element = doc.createElement(GEMSConstants.NAME_ELEMENT);
|
---|
302 | name_element.appendChild(doc.createTextNode(name.getValue()));
|
---|
303 | set_element.appendChild(name_element);
|
---|
304 | Attribute des = getAttributeByNameAndLanguage(GEMSConstants.DESCRIPTION_ATTRIBUTE,lang);
|
---|
305 | if (des !=null){
|
---|
306 | Element des_element = doc.createElement(GEMSConstants.DESCRIPTION_ELEMENT);
|
---|
307 | des_element.appendChild(doc.createTextNode(des.getValue()));
|
---|
308 | set_element.appendChild(des_element);
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (first_child == null){
|
---|
312 |
|
---|
313 | if (previous_element !=null && previous_element.getNextSibling() !=null){
|
---|
314 | doc_element.insertBefore(set_element,previous_element.getNextSibling());
|
---|
315 | }
|
---|
316 | else{
|
---|
317 | doc_element.appendChild(set_element);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | else {
|
---|
321 | doc_element.insertBefore(set_element,first_child);
|
---|
322 | first_child = null;
|
---|
323 | previous_element = set_element;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 |
|
---|
333 | }
|
---|