1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 34 import org.apache.lenya.xml.DocumentHelper; 35 import org.apache.lenya.xml.NamespaceHelper; 36 import org.apache.log4j.Category; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.NodeList ; 40 import org.xml.sax.SAXException ; 41 42 46 public class DublinCoreImpl { 47 private static final Category log = Category.getInstance(DublinCoreImpl.class); 48 private Document cmsdocument; 49 private File infofile; 50 51 private Map elements = new HashMap (); 52 private Map terms = new HashMap (); 53 54 private static final String META = "meta"; 55 56 private static final String DC_NAMESPACE = "http://purl.org/dc/elements/1.1/"; 57 private static final String DC_PREFIX = "dc"; 58 59 public static final String [] ELEMENTS = 60 { 61 DublinCore.ELEMENT_TITLE, 62 DublinCore.ELEMENT_CREATOR, 63 DublinCore.ELEMENT_SUBJECT, 64 DublinCore.ELEMENT_DESCRIPTION, 65 DublinCore.ELEMENT_PUBLISHER, 66 DublinCore.ELEMENT_CONTRIBUTOR, 67 DublinCore.ELEMENT_DATE, 68 DublinCore.ELEMENT_TYPE, 69 DublinCore.ELEMENT_FORMAT, 70 DublinCore.ELEMENT_IDENTIFIER, 71 DublinCore.ELEMENT_SOURCE, 72 DublinCore.ELEMENT_LANGUAGE, 73 DublinCore.ELEMENT_RELATION, 74 DublinCore.ELEMENT_COVERAGE, 75 DublinCore.ELEMENT_RIGHTS }; 76 77 79 private static final String DCTERMS_NAMESPACE = "http://purl.org/dc/terms/"; 80 private static final String DCTERMS_PREFIX = "dcterms"; 81 82 public static final String [] TERMS = 83 { 84 DublinCore.TERM_AUDIENCE, 85 DublinCore.TERM_ALTERNATIVE, 86 DublinCore.TERM_TABLEOFCONTENTS, 87 DublinCore.TERM_ABSTRACT, 88 DublinCore.TERM_CREATED, 89 DublinCore.TERM_VALID, 90 DublinCore.TERM_EXTENT, 91 DublinCore.TERM_AVAILABLE, 92 DublinCore.TERM_ISSUED, 93 DublinCore.TERM_MODIFIED, 94 DublinCore.TERM_EXTENT, 95 DublinCore.TERM_MEDIUM, 96 DublinCore.TERM_ISVERSIONOF, 97 DublinCore.TERM_HASVERSION, 98 DublinCore.TERM_ISREPLACEDBY, 99 DublinCore.TERM_REPLACES, 100 DublinCore.TERM_ISREQUIREDBY, 101 DublinCore.TERM_REQUIRES, 102 DublinCore.TERM_ISPARTOF, 103 DublinCore.TERM_HASPART, 104 DublinCore.TERM_ISREFERENCEDBY, 105 DublinCore.TERM_REFERENCES, 106 DublinCore.TERM_ISFORMATOF, 107 DublinCore.TERM_HASFORMAT, 108 DublinCore.TERM_CONFORMSTO, 109 DublinCore.TERM_SPATIAL, 110 DublinCore.TERM_TEMPORAL, 111 DublinCore.TERM_MEDIATOR, 112 DublinCore.TERM_DATEACCEPTED, 113 DublinCore.TERM_DATECOPYRIGHTED, 114 DublinCore.TERM_DATESUBMITTED, 115 DublinCore.TERM_EDUCATIONLEVEL, 116 DublinCore.TERM_ACCESSRIGHTS, 117 DublinCore.TERM_BIBLIOGRAPHICCITATION }; 118 119 126 protected DublinCoreImpl(Document aDocument) throws DocumentException { 127 this.cmsdocument = aDocument; 128 infofile = 129 cmsdocument.getPublication().getPathMapper().getFile( 130 cmsdocument.getPublication(), 131 cmsdocument.getArea(), 132 cmsdocument.getId(), 133 cmsdocument.getLanguage()); 134 loadValues(); 135 } 136 137 141 protected void loadValues() throws DocumentException { 142 143 if (infofile.exists()) { 144 org.w3c.dom.Document doc = null; 145 try { 146 doc = DocumentHelper.readDocument(infofile); 147 } catch (Exception e) { 148 throw new DocumentException("Parsing file [" + infofile + "] failed: ", e); 149 } 150 151 Element metaElement = getMetaElement(doc); 154 155 String [] namespaces = { DC_NAMESPACE, DCTERMS_NAMESPACE }; 156 String [] prefixes = { DC_PREFIX, DCTERMS_PREFIX }; 157 String [][] arrays = { ELEMENTS, TERMS }; 158 Map [] maps = { elements, terms }; 159 160 for (int type = 0; type < 2; type++) { 161 NamespaceHelper helper = new NamespaceHelper(namespaces[type], prefixes[type], doc); 162 String [] elementNames = arrays[type]; 163 for (int i = 0; i < elementNames.length; i++) { 164 Element [] children = helper.getChildren(metaElement, elementNames[i]); 165 String [] values = new String [children.length]; 166 for (int valueIndex = 0; valueIndex < children.length; valueIndex++) { 167 values[valueIndex] = 168 DocumentHelper.getSimpleElementText(children[valueIndex]); 169 } 170 maps[type].put(elementNames[i], values); 171 } 172 } 173 } 174 175 } 176 177 182 public void save() throws DocumentException { 183 org.w3c.dom.Document doc = null; 184 try { 185 doc = DocumentHelper.readDocument(infofile); 186 } catch (ParserConfigurationException e) { 187 throw new DocumentException(e); 188 } catch (SAXException e) { 189 throw new DocumentException(e); 190 } catch (IOException e) { 191 throw new DocumentException(e); 192 } 193 194 Element metaElement = getMetaElement(doc); 195 196 String [] namespaces = { DC_NAMESPACE, DCTERMS_NAMESPACE }; 197 String [] prefixes = { DC_PREFIX, DCTERMS_PREFIX }; 198 String [][] arrays = { ELEMENTS, TERMS }; 199 Map [] maps = { elements, terms }; 200 201 List childNodes = new ArrayList (); 202 NodeList nodes = metaElement.getChildNodes(); 203 for (int i = 0; i < nodes.getLength(); i++) { 204 if (nodes.item(i).getParentNode() == metaElement) { 205 childNodes.add(nodes.item(i)); 206 } 207 } 208 Node [] children = (Node [])childNodes.toArray(new Node [childNodes.size()]); 209 for (int i = 0; i < children.length; i++){ 210 metaElement.removeChild(children[i]); 211 } 212 213 for (int type = 0; type < 2; type++) { 214 NamespaceHelper helper = new NamespaceHelper(namespaces[type], prefixes[type], doc); 215 String [] elementNames = arrays[type]; 216 for (int i = 0; i < elementNames.length; i++) { 217 String [] values = (String []) maps[type].get(elementNames[i]); 218 for (int valueIndex = 0; valueIndex < values.length; valueIndex++) { 219 Element valueElement = 220 helper.createElement(elementNames[i], values[valueIndex]); 221 metaElement.appendChild(valueElement); 222 } 223 } 224 } 225 226 try { 227 DocumentHelper.writeDocument(doc, infofile); 228 } catch (TransformerConfigurationException e) { 229 throw new DocumentException(e); 230 } catch (TransformerException e) { 231 throw new DocumentException(e); 232 } catch (IOException e) { 233 throw new DocumentException(e); 234 } 235 236 } 237 238 243 protected Element getMetaElement(org.w3c.dom.Document doc) throws DocumentException { 244 NamespaceHelper namespaceHelper = 245 new NamespaceHelper(PageEnvelope.NAMESPACE, PageEnvelope.DEFAULT_PREFIX, doc); 246 Element documentElement = doc.getDocumentElement(); 247 Element metaElement = namespaceHelper.getFirstChild(documentElement, META); 248 249 if (metaElement == null) { 250 metaElement = namespaceHelper.createElement(META); 251 Element [] children = DocumentHelper.getChildren(documentElement); 252 if (children.length == 0) { 253 documentElement.appendChild(metaElement); 254 } else { 255 documentElement.insertBefore(metaElement, children[0]); 256 } 257 } 258 259 return metaElement; 260 } 261 262 267 public String getFirstValue(String key) throws DocumentException { 268 String value = null; 269 String [] values = getElementOrTerm(key); 270 if (values.length > 0) { 271 value = values[0]; 272 } 273 return value; 274 } 275 276 281 protected String [] getElementOrTerm(String key) throws DocumentException { 282 String [] values; 283 284 List elementList = Arrays.asList(ELEMENTS); 285 List termList = Arrays.asList(TERMS); 286 if (elementList.contains(key)) { 287 values = (String []) elements.get(key); 288 } else if (termList.contains(key)) { 289 values = (String []) terms.get(key); 290 } else { 291 throw new DocumentException( 292 "The key [" + key + "] does not refer to a dublin core element or term!"); 293 } 294 if (values == null) { 295 values = new String [0]; 296 } 297 return values; 298 } 299 300 303 public String [] getValues(String key) throws DocumentException { 304 return getElementOrTerm(key); 305 } 306 307 310 public void addValue(String key, String value) throws DocumentException { 311 String [] existingValues = getElementOrTerm(key); 312 List list = new ArrayList (Arrays.asList(existingValues)); 313 list.add(value); 314 String [] newValues = (String []) list.toArray(new String [list.size()]); 315 316 List elementList = Arrays.asList(ELEMENTS); 317 List termList = Arrays.asList(TERMS); 318 if (elementList.contains(key)) { 319 elements.put(key, newValues); 320 } else if (termList.contains(key)) { 321 terms.put(key, newValues); 322 } else { 323 throw new DocumentException( 324 "The key [" + key + "] does not refer to a dublin core element or term!"); 325 } 326 } 327 328 331 public void setValue(String key, String value) throws DocumentException { 332 String [] newValues = { value }; 333 334 List elementList = Arrays.asList(ELEMENTS); 335 List termList = Arrays.asList(TERMS); 336 if (elementList.contains(key)) { 337 elements.put(key, newValues); 338 } else if (termList.contains(key)) { 339 terms.put(key, newValues); 340 } else { 341 throw new DocumentException( 342 "The key [" + key + "] does not refer to a dublin core element or term!"); 343 } 344 } 345 346 349 public void addValues(String key, String [] values) throws DocumentException { 350 for (int i = 0; i < values.length; i++) { 351 addValue(key,values[i]); 352 } 353 } 354 355 358 public void removeValue(String key, String value) throws DocumentException { 359 String [] existingValues = getElementOrTerm(key); 360 List list = new ArrayList (Arrays.asList(existingValues)); 361 362 if (!list.contains(value)) { 363 throw new DocumentException( 364 "The key [" + key + "] does not contain the value [" + value + "]!"); 365 } 366 367 list.remove(value); 368 String [] newValues = (String []) list.toArray(new String [list.size()]); 369 370 List elementList = Arrays.asList(ELEMENTS); 371 List termList = Arrays.asList(TERMS); 372 if (elementList.contains(key)) { 373 elements.put(key, newValues); 374 } else if (termList.contains(key)) { 375 terms.put(key, newValues); 376 } else { 377 throw new DocumentException( 378 "The key [" + key + "] does not refer to a dublin core element or term!"); 379 } 380 } 381 382 385 public void removeAllValues(String key) throws DocumentException { 386 List elementList = Arrays.asList(ELEMENTS); 387 List termList = Arrays.asList(TERMS); 388 if (elementList.contains(key)) { 389 elements.put(key, new String [0]); 390 } else if (termList.contains(key)) { 391 terms.put(key, new String [0]); 392 } else { 393 throw new DocumentException( 394 "The key [" + key + "] does not refer to a dublin core element or term!"); 395 } 396 } 397 398 401 public void replaceBy(DublinCore other) throws DocumentException { 402 for (int i = 0; i < ELEMENTS.length; i++) { 403 String key = ELEMENTS[i]; 404 removeAllValues(key); 405 addValues(key, other.getValues(key)); 406 } 407 for (int i = 0; i < TERMS.length; i++) { 408 String key = TERMS[i]; 409 removeAllValues(key); 410 addValues(key, other.getValues(key)); 411 } 412 } 413 414 } 415 | Popular Tags |