Changeset 23489


Ignore:
Timestamp:
12/14/10 17:15:30 (10 years ago)
Author:
sjb48
Message:

Working on saving a format statement to the config file. This looks like it should work but changes in FormatAction have caused major issues with Document/Node violations. This is because saving required the format statement as a Document rather than a string (which did work). Added a check to GSXML for xmlNodeToString to check that attrs is not null (attrs will be null unless the node is an element).

Location:
main/trunk/greenstone3
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/FormatAction.java

    r23405 r23489  
    5555   
    5656    String coll = (String)params.get(GSParams.COLLECTION); //SYSTEM_CLUSTER);
     57    //String subaction = (String)params.get(GSParams.SUBACTION);
    5758    String service = (String)params.get(GSParams.SERVICE);
    5859    String classifier = (String)params.get("cl");
     
    6061
    6162    logger.error("Collection="+coll);
     63    logger.error("Subaction="+subaction);
    6264    logger.error("Service="+service);
    6365    logger.error("Classifier="+classifier);
     
    7274    Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT_STRING, to, lang, uid);
    7375
    74     request.setAttribute("service", service);
    75     if(classifier != null)
    76         request.setAttribute("classifier", classifier);
     76    mr_request.setAttribute("service", service);
     77    mr_request.setAttribute("subaction", subaction);
     78    //if(classifier != null)
     79    mr_request.setAttribute("classifier", classifier);
    7780
    7881    mr_request_message.appendChild(mr_request);
     
    98101            InputSource is = new InputSource( new StringReader( input ) );
    99102            Document d = (Document) builder.parse( is );
    100             //Element e = d.getDocumentElement();
    101             //Node d2 = this.doc.importNode(e,true);
     103            //Node n1 = d.getFirstChild();           
     104            //Document d2 = (Document) this.doc.importNode(e, true);
    102105
    103             //Document format_doc = this.converter.newDOM();
    104             //doc.appendChild(d2);
    105 
    106             //File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
    107             //if (!interface_config_file.exists()) {
    108             //  logger.error(" interface config file: "+interface_config_file.getPath()+" not found!");
    109             //}
    110             //Document config_doc = this.converter.getDOM(interface_config_file, "utf-8");
    111             //if (config_doc == null) {
    112             //  logger.error(" could not parse interface config file: "+interface_config_file.getPath());
    113             //}
    114        
    115             //Element config_elem = config_doc.getDocumentElement();
    116             //String base_interface = config_elem.getAttribute("baseInterface");
    117 
     106            //Element format_statement = this.doc.importNode(d, true);
    118107
    119108            // Call XSLT to transform document to xml format string
    120109            XMLTransformer transformer = new XMLTransformer();
    121             //String stylesheet = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), coll, (String)this.config_params.get(GSConstants.INTERFACE_NAME), "oran", "FormatAction");
    122             //logger.error(stylesheet);
     110            // HOW DO I DO THIS PROPERLY?
    123111            Document style_doc = this.converter.getDOM(new File("/home/sam/greenstone3/web/interfaces/oran/transform/formatString.xsl"), "UTF-8");
    124112
     
    127115
    128116            // not sure what to do here - some code from Transforming Receptionist
    129             String transformed = transformer.transformToString(style_doc, d);
    130             //Node transformed = transformer.transform(style_doc, d);
    131            
     117            //String transformed = transformer.transformToString(style_doc, d);
     118            logger.error("About to transform");
     119            Node transformed = (Node) transformer.transform(style_doc, d);  // Failing org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. ; SystemID: file:///home/sam/greenstone3/packages/tomcat/bin/dummy.xsl
     120
     121            logger.error("Transform successful?");
     122         
     123            if(transformed==null)  // not null
     124                logger.error("TRANSFORMED IS NULL");
     125
     126            logger.error("begin import");
     127            Node imported = this.doc.importNode(transformed, true); // There was an exception org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.
     128            logger.error("finished import");
     129
     130            String format_string2 = GSXML.xmlNodeToString(imported); // null pointer exception occuring here
     131            logger.error("format string="+format_string2);
     132 
    132133            Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
    133             GSXML.setNodeText(format, transformed);
    134             //format.appendChild(this.doc.createTextNode(transformed));
     134            //GSXML.setNodeText(format, transformed);
     135            format.appendChild(transformed);
    135136            //format.setNodeValue(transformed);
    136137            mr_request.appendChild(format);
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java

    r21771 r23489  
    3030import org.w3c.dom.NodeList;
    3131
     32import java.io.*;
    3233import java.io.File;
    3334import java.util.HashMap;
     
    299300    }
    300301
     302 /** handles requests made to the ServiceCluster itself
     303     *
     304     * @param req - the request Element- <request>
     305     * @return the result Element - should be <response>
     306     */
     307    protected Element processMessage(Element request) {
     308
     309    Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     310    response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     311    String type = request.getAttribute(GSXML.TYPE_ATT);
     312    String lang = request.getAttribute(GSXML.LANG_ATT);
     313    response.setAttribute(GSXML.TYPE_ATT, type);
     314
     315    if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
     316        logger.error("Received format string request");
     317
     318        String subaction = request.getAttribute("subaction");
     319        logger.error("Subaction is " + subaction);
     320
     321        String service = request.getAttribute("service");
     322        logger.error("Service is " + service);
     323
     324        String classifier = null;
     325        if(service.equals("ClassifierBrowse"))
     326        {
     327            classifier = request.getAttribute("classifier");
     328            logger.error("Classifier is " + classifier);
     329        }
     330
     331        Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     332        //String format_string = GSXML.getNodeText(format_element);
     333        Element format_statement = (Element) format_element.getFirstChild();
     334
     335        //logger.error("Format string: " + format_string);
     336        logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
     337
     338        // check for version file
     339
     340        String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
     341        logger.error("Directory is " + directory);
     342
     343        String version_filename = "";
     344        if(service.equals("ClassifierBrowse"))
     345            version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
     346        else
     347            version_filename = directory + "query_format_statement_version.txt";
     348
     349        File version_file = new File(version_filename);
     350        logger.error("Version filename is " + version_filename);
     351
     352
     353        if(subaction.equals("update"))
     354        {
     355            String version_number = "1";
     356            BufferedWriter writer;
     357
     358            try{
     359
     360                if(version_file.exists())
     361                {
     362                    // Read version
     363                    BufferedReader reader = new BufferedReader(new FileReader(version_filename));
     364                    version_number = reader.readLine();
     365                    int aInt = Integer.parseInt(version_number) + 1;
     366                    version_number = Integer.toString(aInt);
     367                    reader.close();
     368                }
     369                else{
     370                    // Create
     371                    version_file.createNewFile();
     372                    writer = new BufferedWriter(new FileWriter(version_filename));
     373                    writer.write(version_number);
     374                    writer.close();
     375                }
     376
     377                // Write version file
     378                String format_statement_filename = "";
     379
     380                if(service.equals("ClassifierBrowse"))
     381                    format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
     382                else
     383                    format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
     384
     385                logger.error("Format statement filename is " + format_statement_filename);
     386
     387                // Write format statement
     388                String format_string = GSXML.xmlNodeToString(format_statement);
     389                writer = new BufferedWriter(new FileWriter(format_statement_filename));
     390                writer.write(format_string);
     391                writer.close();
     392
     393                // Update version number
     394                writer = new BufferedWriter(new FileWriter(version_filename));
     395                writer.write(version_number);
     396                writer.close();
     397
     398            } catch (IOException e) {
     399                logger.error("IO Exception "+e);
     400            }
     401        }
     402
     403        if(subaction.equals("save"))
     404        {
     405            logger.error("SAVE format statement");
     406
     407            // open collectionConfig.xml and read in to w3 Document
     408            String collection_config = directory + "collectionConfig.xml";
     409            Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
     410           
     411            //String tag_name = "";
     412            int k;
     413            int index;
     414            Element elem;
     415            Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
     416            NodeList current_node_list;
     417
     418            if(service.equals("ClassifierBrowse"))
     419            {
     420                //tag_name = "browse";
     421                // if CLX then need to look in <classifier> X then <format>
     422                // default is <browse><format>
     423
     424                current_node = GSXML.getChildByTagName(current_node, "browse");
     425
     426                // find CLX
     427                if(classifier != null)
     428                {
     429                    current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
     430                    index = Integer.parseInt(classifier.substring(2)) - 1;
     431                    // index should be given by X-1
     432                    current_node = current_node_list.item(index);
     433                    current_node = GSXML.getChildByTagName(current_node, "format");
     434                }
     435                else{
     436                    current_node = GSXML.getChildByTagName(current_node, "format");
     437                }
     438            }
     439            else
     440            {
     441                // look in <format> with no attributes
     442           
     443                current_node_list = GSXML.getChildrenByTagName(current_node, "search");
     444                for(k=0; k<current_node_list.getLength(); k++)
     445                {
     446                    current_node = current_node_list.item(k);
     447                    // if current_node has no attributes then break
     448                    elem = (Element) current_node;
     449                    if(elem.hasAttribute("name")==false)
     450                        break;
     451                }
     452            }
     453
     454            // Current_node should be a format tag
     455            elem = (Element) current_node;
     456
     457            logger.error("Current_node = " + elem.getNodeName());
     458
     459            // seems we want to remove current child/ren and replace with format_statement's child/ren?
     460
     461            // remove existing
     462            current_node_list = elem.getChildNodes();
     463            for(k=0; k<current_node_list.getLength(); k++)
     464            {
     465                current_node = elem.removeChild(current_node_list.item(k));
     466            }
     467
     468            // append new
     469            current_node_list = format_statement.getChildNodes();
     470            for(k=0; k<current_node_list.getLength(); k++)
     471            {
     472                current_node = elem.appendChild(current_node_list.item(k));
     473            }
     474
     475            //String text = GSXML.getNodeText(elem);
     476            //logger.error(text);
     477            //text = text.replaceAll("_httpsite_", http_site);
     478            //text = text.replaceAll("_httpcollection_", http_collection);
     479            //GSXML.setNodeText(d, text);
     480
     481            // Now convert config document to string for writing to file
     482            String new_config = GSXML.xmlNodeToString(config);
     483
     484            // Write to file (not original! for now)
     485            try{
     486                BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config+".new"));
     487                writer.write(new_config);
     488                writer.close();
     489            } catch (IOException e) {
     490                logger.error("IO Exception "+e);
     491            }
     492        }
     493    }
     494    else { // unknown type
     495        super.processMessage(request);
     496
     497    }
     498    return response;
     499    }
     500
    301501}
    302502
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/ServiceCluster.java

    r23417 r23489  
    3434import java.io.*;
    3535import java.io.File;
     36import java.io.RandomAccessFile;
    3637import java.util.HashMap;
    3738import java.util.Iterator;
     
    510511        return response;
    511512    }
    512 
     513    /*
    513514    if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
    514515        logger.error("Received format string request");
     
    528529        // check for version file
    529530
    530         String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + "/";
     531        String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.pathSeparator;
    531532        logger.error("Directory is " + directory);
    532533
     
    540541        File version_file = new File(version_filename);
    541542        logger.error("Version filename is " + version_filename);
     543
    542544        String version_number = "1";
     545        BufferedWriter writer; // = new BufferedWriter(new FileWriter(version_filename));
     546        //RandomAccessFile version_file_random_access;
    543547
    544548        try{
     
    548552                // Read version
    549553                 BufferedReader reader = new BufferedReader(new FileReader(version_filename));
     554                 //version_file_random_access = new RandomAccessFile(version_file, "r");
     555                 //logger.error("
     556                 //version_number = version_file_random_access.readInt();
    550557                 version_number = reader.readLine();
    551558                 int aInt = Integer.parseInt(version_number) + 1;
    552559                 version_number = Integer.toString(aInt);
    553560                 reader.close();
     561                 //version_file_random_access.close();
    554562            }
    555563            else{
     
    557565                version_file.createNewFile();
    558566                // write 1 to file
    559                 BufferedWriter writer = new BufferedWriter(new FileWriter(version_filename));
     567                writer = new BufferedWriter(new FileWriter(version_filename));
     568                //version_file_random_access = new RandomAccessFile(version_file, "w");
     569                //version_file_random_access.writeInt(version_number);
    560570                writer.write(version_number);
    561571                writer.close();
     572                //version_file_random_access.close();
    562573            }
    563574
     
    572583            logger.error("Format statement filename is " + format_statement_filename);
    573584
    574             BufferedWriter writer = new BufferedWriter(new FileWriter(format_statement_filename));
     585            writer = new BufferedWriter(new FileWriter(format_statement_filename));
    575586            writer.write(format_string);
    576587            writer.close();
    577588
    578589            // Update version number
     590            //version_file_random_access = new RandomAccessFile(version_file, "w");
     591            //version_file_random_access.writeInt(version_number);
     592            //version_file_random_access.close();
     593
     594            writer = new BufferedWriter(new FileWriter(version_filename));
     595            //version_file_random_access = new RandomAccessFile(version_file, "w");
     596            //version_file_random_access.writeInt(version_number);
     597            writer.write(version_number);
     598            writer.close();
     599
     600
    579601
    580602        } catch (IOException e) {
     
    584606
    585607
    586     }
     608    }*/
    587609    if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
    588610        response = processSystemRequest(request);
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r23360 r23489  
    921921    sb.append(e.getNodeName());
    922922    NamedNodeMap attrs = e.getAttributes();
    923     for (int i = 0; i < attrs.getLength(); i++) {
    924       Node attr = attrs.item(i);
    925       sb.append(' ');
    926       sb.append(attr.getNodeName());
    927       sb.append("=\"");
    928       sb.append(attr.getNodeValue());
    929       sb.append('"');
     923    if(attrs != null)
     924    {
     925        for (int i = 0; i < attrs.getLength(); i++) {
     926            Node attr = attrs.item(i);
     927            sb.append(' ');
     928            sb.append(attr.getNodeName());
     929            sb.append("=\"");
     930            sb.append(attr.getNodeValue());
     931            sb.append('"');
     932        }
    930933    }
    931934   
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLTransformer.java

    r23405 r23489  
    177177            // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
    178178            Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
     179            logger.error("XMLTransformer transformer is " + transformer);
    179180            transformer.setErrorListener(new TransformErrorListener());
    180181            if (parameters != null) {
  • main/trunk/greenstone3/web/interfaces/oran/js/gui_div.js

    r23405 r23489  
    44var initialised_iframe = "false";
    55
    6 function createFormatStatement()
    7 {
    8 
     6//function createFormatStatement()
     7//{
     8
     9    //var formatDiv = document.getElementById('formatStatement');
     10    //var formatStatement = innerXHTML(formatDiv);
     11    //console.log(formatStatement);
     12
     13    // find collection name
     14
     15    //var myurl = document.URL;
     16    //console.log(myurl);
     17    //var first = myurl.indexOf("&c")+3;
     18    //var last = myurl.indexOf("&", first);
     19   
     20    //var collection_name = getSubstring(myurl, "&c", "&"); //myurl.substring(first,last); ///&c=(.*)&/.exec(myurl);
     21    //console.log(collection_name);
     22
     23    //first = myurl.indexOf("&s")+3;
     24    //last = myurl.indexOf("&", first);
     25
     26    //var service_name = myurl.substring(first,last);
     27    //console.log(service_name);
     28
     29    //var classifier_name = null;
     30
     31    //if(service_name == "ClassifierBrowse")
     32    //{
     33    //    first = myurl.indexOf("&cl")+4;
     34    //    last = myurl.indexOf("&", first);
     35
     36    //    classifier_name = myurl.substring(first,last);
     37    //    console.log(classifier_name);
     38    //}
     39
     40    //var myurl = 'http://localhost:8080/greenstone3/format?a=s&sa=s&t='+formatStatement;
     41
     42    //jQuery.post( url, [ data ], [ success(data, textStatus, XMLHttpRequest) ], [ dataType ] )
     43
     44    // How do I find out my collection name?
     45
     46function getSubstring(str, first, last)
     47{
     48    var first_index = str.indexOf(first)+first.length+1;
     49    var last_index = str.indexOf(last, first_index);
     50
     51    var substring = str.substring(first_index, last_index);
     52
     53    console.log(substring);
     54
     55    return substring;
     56}
     57   
     58function getFormatStatement()
     59{
    960    var formatDiv = document.getElementById('formatStatement');
    1061    var formatStatement = innerXHTML(formatDiv);
    11     //console.log(formatStatement);
    12 
    13     // find collection name
     62    return formatStatement;
     63}
     64
     65function updateFormatStatement()
     66{
     67    var formatStatement = getFormatStatement();
    1468
    1569    var myurl = document.URL;
    16     console.log(myurl);
    17     var first = myurl.indexOf("&c")+3;
    18     var last = myurl.indexOf("&", first);
    19    
    20     var collection_name = myurl.substring(first,last); ///&c=(.*)&/.exec(myurl);
    21     console.log(collection_name);
    22 
    23     first = myurl.indexOf("&s")+3;
    24     last = myurl.indexOf("&", first);
    25 
    26     var service_name = myurl.substring(first,last);
    27     console.log(service_name);
    28 
     70
     71    var collection_name = getSubstring(myurl, "&c", "&");
     72    var service_name = getSubstring(myurl, "&s", "&");
    2973    var classifier_name = null;
    3074
    3175    if(service_name == "ClassifierBrowse")
    32     {
    33         first = myurl.indexOf("&cl")+4;
    34         last = myurl.indexOf("&", first);
    35 
    36         classifier_name = myurl.substring(first,last);
    37         console.log(classifier_name);
    38     }
    39 
    40     //var myurl = 'http://localhost:8080/greenstone3/format?a=s&sa=s&t='+formatStatement;
    41 
    42     //jQuery.post( url, [ data ], [ success(data, textStatus, XMLHttpRequest) ], [ dataType ] )
    43 
    44     // How do I find out my collection name?
    45     var post_url = "http://localhost:8080/greenstone3/format?a=f&c=" + collection_name +"&s=" + service_name;
     76        classifier_name = getSubstring(myurl, "&cl", "&");
     77
     78    var post_url = "http://localhost:8080/greenstone3/format?a=f&sa=update&c=" + collection_name +"&s=" + service_name;
    4679
    4780    if(classifier_name != null)
     
    5386        console.log(data);
    5487        }, 'html');
     88}
     89
     90function saveFormatStatement()
     91{
     92    var formatStatement = getFormatStatement();
     93
     94    var myurl = document.URL;
     95
     96    var collection_name = getSubstring(myurl, "&c", "&");
     97    var service_name = getSubstring(myurl, "&s", "&");
     98    var classifier_name = null;
     99
     100    if(service_name == "ClassifierBrowse")
     101        classifier_name = getSubstring(myurl, "&cl", "&");
     102
     103    var post_url = "http://localhost:8080/greenstone3/format?a=f&sa=save&c=" + collection_name +"&s=" + service_name;
     104
     105    if(classifier_name != null)
     106        post_url = post_url + "&cl=" + classifier_name;
     107
     108    $.post(post_url, {data: formatStatement}, function(data) {
     109        //$('.result').innerHTML = data; //html(data);
     110        console.log("Success, we have received data");
     111        console.log(data);
     112        }, 'html');
     113}
    55114
    56115/*
     
    104163        */
    105164    //}
    106 }
     165//}
    107166
    108167function traverse(node, formatstring)
  • main/trunk/greenstone3/web/interfaces/oran/transform/layouts/main.xsl

    r23336 r23489  
    177177    <td width="75%">
    178178
    179     <button id="updateFormatStatement" type="button" onclick="createFormatStatement()">Update Format Statement</button>
     179    <button id="updateFormatStatement" type="button" onclick="updateFormatStatement()">Update Format Statement</button>
     180    <button id="saveFormatStatement" type="button" onclick="saveFormatStatement()">Save Format Statement</button>
    180181
    181182    <div id="formatStatement">
Note: See TracChangeset for help on using the changeset viewer.