KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > processors > xml > FieldGetString


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes.processors.xml;
11 import org.mmbase.datatypes.processors.Processor;
12 import org.mmbase.bridge.*;
13 import org.mmbase.util.*;
14 import javax.xml.transform.dom.DOMSource JavaDoc;
15 import javax.xml.transform.stream.StreamResult JavaDoc;
16 import org.w3c.dom.Document JavaDoc;
17 import org.mmbase.util.logging.*;
18
19
20 /**
21  * @see FieldSetString
22  * @author Michiel Meeuwissen
23  * @version $Id: FieldGetString.java,v 1.3 2005/12/10 14:33:36 michiel Exp $
24  * @since MMBase-1.8
25  */

26
27 public class FieldGetString implements Processor {
28     private static final Logger log = Logging.getLoggerInstance(FieldGetString.class);
29     private static final long serialVersionUID = 1L;
30
31     public Object JavaDoc process(Node node, Field field, Object JavaDoc value) {
32
33         Object JavaDoc realValue = node.getObjectValue(field.getName());
34         if (realValue == null || value == null) return "";
35
36
37         if (! (realValue instanceof Document JavaDoc)) {
38             throw new RuntimeException JavaDoc("FieldGetString should only be defined for XML fields. " + node.getNumber() + "/" + field.getName() + " returned a " + realValue.getClass() + "'" + realValue + "'");
39         }
40         Document JavaDoc document = (Document JavaDoc) realValue;
41
42
43         if (value instanceof Document JavaDoc) {
44             // requested XML, give it!
45
return document;
46         } else {
47             // requested something else, String, probably
48
try {
49                 switch(Modes.getMode("" + node.getCloud().getProperty(Cloud.PROP_XMLMODE))) {
50                 case Modes.WIKI:
51                 case Modes.KUPU:
52                 case Modes.FLAT: {
53                     // unXMLize first, then cast back to wanted type.
54
String JavaDoc string;
55                     try {
56                         java.net.URL JavaDoc u = ResourceLoader.getConfigurationRoot().getResource("xslt/text.xslt");
57                         java.io.StringWriter JavaDoc res = new java.io.StringWriter JavaDoc();
58                         XSLTransformer.transform(new DOMSource JavaDoc((Document JavaDoc) realValue), u, new StreamResult JavaDoc(res), null);
59                         string = res.toString();
60                     } catch (Exception JavaDoc e) {
61                         log.warn(e);
62                         string = e.getMessage();
63                     }
64                     return Casting.toType(value.getClass(), node.getCloud(), string);
65                 }
66                 default: {
67                     return value;
68                 }
69
70                 }
71             } catch (Exception JavaDoc e) {
72                 log.error(e.getMessage(), e);
73                 return value;
74             }
75         }
76     }
77 }
78
Popular Tags