Project

General

Profile

« Previous | Next » 

Revision 832

Added support for multiple user interfaces by enabling style sheet
transformations to be configured from within metacat rather than
from in the database. Now, metacat uses the qformat parameter to determine
a "style set", which is mapped to an xml configuration file that
provides a mapping between a doctype and the stylesheet to use for that
doctype. In the absence of a qformat param, or if qformat is set to
"html", then the document is transformed using a default style-set that
is set in metacat.properties. See Bugzilla bug #280 for details of
the xml configuration file format.

View differences:

lib/metacat.properties
11 11
initialConnections=5
12 12
incrementConnections=5
13 13
maximumConnections=10
14
resultStyleURL=@result-style@
14
config-dir=@config-dir@
15
default-style=@default-style@
15 16
xmlcatalogfile=/opt/tomcat/webapps/xmltodb/catalog.txt
16 17
sitecode=nceas
17 18
accNumSeparator=.
lib/knb.xml
1
<?xml version="1.0"?>
2
<style-set name="knb">
3
  <default-style>http://dev.nceas.ucsb.edu/jones/style/generic.xsl</default-style>
4
  <doctype publicid="-//NCEAS//resultset//EN">
5
    <target publicid="-//W3C//HTML//EN">http://dev.nceas.ucsb.edu/jones/style/resultset.xsl</target>
6
  </doctype>
7
  <doctype publicid="-//NCEAS//login//EN">
8
    <target publicid="-//W3C//HTML//EN">http://dev.nceas.ucsb.edu/jones/style/login.xsl</target>
9
  </doctype>
10
  <doctype publicid="-//NCEAS//eml-dataset-2.0//EN">
11
    <target publicid="-//W3C//HTML//EN">http://dev.nceas.ucsb.edu/jones/style/eml-dataset-display.xsl</target>
12
  </doctype>
13
  <doctype publicid="-//NCEAS//eml-dataset//EN">
14
    <target publicid="-//W3C//HTML//EN">http://dev.nceas.ucsb.edu/jones/style/eml-dataset-display.xsl</target>
15
  </doctype>
16
  <doctype publicid="-//NCEAS//eml-resource//EN">
17
    <target publicid="-//W3C//HTML//EN">http://dev.nceas.ucsb.edu/jones/style/eml-dataset-display.xsl</target>
18
  </doctype>
19
</style-set>
0 20

  
src/edu/ucsb/nceas/metacat/DBTransform.java
36 36

  
37 37
import oracle.xml.parser.v2.XSLStylesheet;
38 38
import oracle.xml.parser.v2.XSLException;
39
import oracle.xml.parser.v2.XMLParseException;
39 40
import oracle.xml.parser.v2.XSLProcessor;
40 41
import oracle.xml.parser.v2.XMLDocument;
41 42
import oracle.xml.parser.v2.DOMParser;
43
import org.w3c.dom.Document;
44
import org.w3c.dom.Node;
45
import org.w3c.dom.Element;
46
import org.xml.sax.SAXException;
42 47

  
43 48
/** 
44 49
 * A Class that transforms XML documents utitlizing XSL style sheets
......
46 51
public class DBTransform {
47 52

  
48 53
  private Connection	conn = null;
54
  private MetaCatUtil   util = null;
55
  private String 	configDir = null;
56
  private String	defaultStyle = null;
49 57

  
50 58
  /**
51 59
   * construct a DBTransform instance.
......
60 68
                         ClassNotFoundException
61 69
  {
62 70
    this.conn = conn;
71
    util = new MetaCatUtil();
72
    configDir = util.getOption("config-dir");
73
    defaultStyle = util.getOption("default-style");
63 74
  }
64 75
  
65 76
  /**
......
68 79
   * @param doc the document to be transformed
69 80
   * @param sourcetype the document type of the source
70 81
   * @param targettype the target document type
82
   * @param qformat the name of the style set to use
83
   * @param pw the PrintWriter to which output is printed
71 84
   */
72 85
  public void transformXMLDocument(String doc, String sourcetype, 
73
                String targettype, PrintWriter pw) {
86
                String targettype, String qformat, PrintWriter pw) {
74 87
    
75 88
    // Look up the stylesheet for this type combination
76
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
89
    String xsl_system_id = getStyleSystemId(qformat, sourcetype, targettype);
77 90

  
78 91
    if (xsl_system_id != null) {
79 92
      // Create a stylesheet from the system id that was found
......
98 111
  /**
99 112
   * Lookup a stylesheet reference from the db catalog
100 113
   *
114
   * @param qformat    the named style-set format
115
   * @param sourcetype the document type of the source
116
   * @param targettype the document type of the target
117
   */
118
  public String getStyleSystemId(String qformat, String sourcetype, 
119
                String targettype) {
120
    String systemId = null;
121

  
122
    if ((qformat == null) || (qformat.equals("html"))) {
123
      qformat = defaultStyle;
124
    }
125

  
126
    // Load the style-set map for this qformat into a DOM
127
    try {
128
      String filename = configDir + "/" + qformat + ".xml";       
129
      util.debugMessage("Trying style-set file: " + filename);
130

  
131
      DOMParser dp = new DOMParser();
132
      dp.setValidationMode(false);
133
      dp.parse((Reader)(new FileReader(filename)));
134
      Document doc = dp.getDocument();
135
      Element root = doc.getDocumentElement();
136
      String styleName = root.getAttribute("name");
137
      util.debugMessage("Root style-set element is: " + styleName);
138
      
139
      Element currentElement = (Element)root.getFirstChild();
140
      systemId = ((Node)currentElement.getFirstChild()).getNodeValue();
141
      util.debugMessage("Default is: " + systemId);
142

  
143
      while ((currentElement = 
144
              (Element)currentElement.getNextSibling()) != null) {
145
        String tagName = currentElement.getTagName();
146
        util.debugMessage("Processing element: " + tagName);
147
        if (tagName.equals("doctype")) {
148
          String doctype = currentElement.getAttribute("publicid");
149
          util.debugMessage("Processing publicid: " + doctype);
150
          util.debugMessage("Comparing to source: " + sourcetype);
151
          if (doctype.equals(sourcetype)) {
152
            Element currentChild = (Element)currentElement.getFirstChild();
153
            while (currentChild != null) {
154
              String target = currentChild.getAttribute("publicid");
155
              util.debugMessage("Processing target publicid: " + target);
156
              util.debugMessage("Comparing to target: " + targettype);
157
              if (target.equals(targettype)) {
158
                Node styleText = currentChild.getFirstChild();
159
                systemId = styleText.getNodeValue();
160
                break; 
161
              }
162
              currentChild = (Element)currentChild.getNextSibling();
163
            }
164
          }
165
        }
166
      }
167
    } catch (IOException ioe) {
168
      util.debugMessage("Caught IOException while opening style-set config.");
169
    } catch (XMLParseException xpe) {
170
      util.debugMessage("Error parsing style-set file");
171
    } catch (SAXException se) {
172
      util.debugMessage("SAX error parsing style-set file");
173
    }
174

  
175
    // Return the system ID for this particular source document type
176
    return systemId;
177
  }
178

  
179
  /**
180
   * Lookup a stylesheet reference from the db catalog
181
   *
101 182
   * @param objecttype the type of the object we want to retrieve
102 183
   * @param sourcetype the document type of the source
103 184
   * @param targettype the document type of the target
......
185 266
          dbt.transformXMLDocument(testdoc.toString(), 
186 267
                                   "-//NCEAS//eml-dataset//EN", 
187 268
                                   "-//W3C//HTML//EN", 
269
                                   "knb",
188 270
                                   new PrintWriter(System.out));
189 271

  
190 272
        } catch (Exception e) {
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
410 410
    boolean isValid = sess.authenticate(request, un, pw);
411 411

  
412 412
    // format and transform the output
413
    if (qformat.equals("html")) {
413
    if (qformat.equals("xml")) {
414
      response.setContentType("text/xml");
415
      out.println(sess.getMessage()); 
416
    } else {
414 417
      Connection conn = null;
415 418
      try {
416 419
        conn = util.getConnection();
417 420
        DBTransform trans = new DBTransform(conn);
418 421
        response.setContentType("text/html");
419 422
        trans.transformXMLDocument(sess.getMessage(), "-//NCEAS//login//EN",
420
                                   "-//W3C//HTML//EN", out);
423
                                   "-//W3C//HTML//EN", qformat, out);
421 424
        util.returnConnection(conn); 
422 425
      } catch(Exception e) {
423 426
        util.returnConnection(conn); 
424 427
      } 
425 428
      
426 429
    // any output is returned  
427
    } else {
428
      response.setContentType("text/xml");
429
      out.println(sess.getMessage()); 
430 430
    }
431 431
  }    
432 432

  
......
450 450
    output.append("</logout>");
451 451

  
452 452
    //format and transform the output
453
    if (qformat.equals("html")) {
453
    if (qformat.equals("xml")) {
454
      response.setContentType("text/xml");
455
      out.println(output.toString()); 
456
    } else {
454 457
      Connection conn = null;
455 458
      try {
456 459
        conn = util.getConnection();
457 460
        DBTransform trans = new DBTransform(conn);
458 461
        response.setContentType("text/html");
459 462
        trans.transformXMLDocument(output.toString(), "-//NCEAS//login//EN", 
460
                                   "-//W3C//HTML//EN", out);
463
                                   "-//W3C//HTML//EN", qformat, out);
461 464
        util.returnConnection(conn); 
462 465
      } catch(Exception e) {
463 466
        util.returnConnection(conn); 
464 467
      } 
465
    // any output is returned  
466
    } else {
467
      response.setContentType("text/xml");
468
      out.println(output.toString()); 
469 468
    }
470 469
  }
471 470
  // END OF LOGIN & LOGOUT SECTION
......
492 491
    resultdoc = createResultDocument(doclist, transformQuery(xmlquery));
493 492
    
494 493
    //format and transform the results                                        
495
    if(qformat.equals("html")) {
496
      transformResultset(resultdoc, response, out);
497
    } else if(qformat.equals("xml")) {
494
    if(qformat.equals("xml")) {
498 495
      response.setContentType("text/xml");
499 496
      out.println(resultdoc);
500 497
    } else {
501
      out.println("invalid qformat: " + qformat); 
498
      transformResultset(resultdoc, response, out, qformat);
502 499
    }
503 500
  }
504 501
  
......
522 519
    resultdoc = createResultDocument(doclist, transformQuery(params));
523 520

  
524 521
    //format and transform the results                                        
525
    if(qformat.equals("html")) {
526
      transformResultset(resultdoc, response, out);
527
    } else if(qformat.equals("xml")) {
522
    if(qformat.equals("xml")) {
528 523
      response.setContentType("text/xml");
529 524
      out.println(resultdoc);
530 525
    } else { 
531
      out.println("invalid qformat: " + qformat); 
526
      transformResultset(resultdoc, response, out, qformat);
532 527
    }
533 528
  }
534 529
  
......
598 593
   * to be transformed.
599 594
   * @param response the HttpServletResponse object bound to the client.
600 595
   * @param out the output stream to the client
596
   * @param qformat the name of the style-set to use for transformations
601 597
   */ 
602 598
  protected void transformResultset(String resultdoc, 
603 599
                                    HttpServletResponse response,
604
                                    PrintWriter out)
600
                                    PrintWriter out, String qformat)
605 601
  {
606 602
    Connection conn = null;
607 603
    try {
......
609 605
      DBTransform trans = new DBTransform(conn);
610 606
      response.setContentType("text/html");
611 607
      trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN", 
612
                                 "-//W3C//HTML//EN", out);
608
                                 "-//W3C//HTML//EN", qformat, out);
613 609
      util.returnConnection(conn); 
614 610
    }
615 611
    catch(Exception e)
......
824 820

  
825 821
      } else {
826 822
        // this is metadata doc
827
        if ( qformat.equals("html") ) { 
823
        if ( qformat.equals("xml") ) { 
824
          // set content type first
825
          response.setContentType("text/xml");   //MIME type
826
          PrintWriter out = response.getWriter();
827
          doc.toXml(out);
828
        } else {
828 829
          response.setContentType("text/html");  //MIME type
829 830
          PrintWriter out = response.getWriter();
830 831
    
......
833 834
          // Transform the document to the new doctype
834 835
          DBTransform dbt = new DBTransform(conn);
835 836
          dbt.transformXMLDocument(doc.toString(),
836
                                   doctype,"-//W3C//HTML//EN",out);
837
        } else {
838
          // set content type first
839
          response.setContentType("text/xml");   //MIME type
840
          PrintWriter out = response.getWriter();
841
          doc.toXml(out);
837
                                   doctype,"-//W3C//HTML//EN", qformat, out);
842 838
        }
843 839
      
844 840
      }
src/edu/ucsb/nceas/metacat/marine/marineServlet.java
48 48
      DBTransform trans = new DBTransform(conn);
49 49
      response.setContentType("text/html");
50 50
      trans.transformXMLDocument(resultdoc, "-//NCEAS//marineresultset//EN", 
51
                                 "-//W3C//HTML//EN", out);
51
                                 "-//W3C//HTML//EN", null, out);
52 52
      util.returnConnection(conn);
53 53
      util.closeConnections();
54 54
    }
build.xml
48 48
      <property name="replication-log" value="/tmp/metacatreplication.log"/>
49 49
      <property name="user" value="jones"/>
50 50
      <property name="password" value="your-pw-goes-here"/>
51
      <property name="result-style" 
52
         value="file:///opt/tomcat/webapps/${html-path}/style/resultset.xsl" />
51
      <property name="config-dir" value="${installdir}" />
52
      <property name="default-style" value="knb" />
53 53

  
54 54
      <filter token="jdbc-connect" value="${jdbc-connect}"/>
55 55
      <filter token="install-dir" value="${installdir}"/>
......
64 64
      <filter token="style-path" value="${style-path}"/>
65 65
      <filter token="web-base-url" value="${web-base-url}"/>
66 66
      <filter token="replication-path" value="${replication-path}"/>
67
      <filter token="result-style" value="${result-style}"/>
67
      <filter token="config-dir" value="${config-dir}"/>
68
      <filter token="default-style" value="${default-style}"/>
68 69
      <property name="srcdir" value="./src" />
69 70

  
70 71
      <property name="build.dir" value="./build"/>

Also available in: Unified diff