Ignore:
Timestamp:
2014-04-10T14:39:33+12:00 (10 years ago)
Author:
kjdon
Message:

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractDocumentRetrieve.java

    r26198 r28966  
    3131import org.greenstone.gsdl3.util.OID;
    3232import org.greenstone.gsdl3.util.GSConstants;
     33import org.greenstone.gsdl3.util.XMLConverter;
    3334
    3435// XML classes
     
    104105        if (does_structure)
    105106        {
    106             Element dsr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     107            Element dsr_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
    107108            dsr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    108109            dsr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
     
    112113        if (does_metadata)
    113114        {
    114             Element dmr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     115            Element dmr_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
    115116            dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    116117            dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
     
    120121        if (does_content)
    121122        {
    122             Element dcr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     123            Element dcr_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
    123124            dcr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    124125            dcr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
     
    131132        if (display_format != null)
    132133        {
    133             this.format_info_map.put(DOCUMENT_CONTENT_RETRIEVE_SERVICE, this.doc.importNode(display_format, true));
     134            this.format_info_map.put(DOCUMENT_CONTENT_RETRIEVE_SERVICE, this.desc_doc.importNode(display_format, true));
    134135            // should we keep a copy?
    135136            // check for docType option.
     
    171172
    172173        // Base line for document (might be overriden by sub-classes)
    173         gs_doc = new BasicDocument(this.doc, this.default_document_type);
     174        gs_doc = new BasicDocument(this.default_document_type);
    174175
    175176        return true;
    176177    }
    177178
    178     protected Element getServiceDescription(String service_id, String lang, String subset)
     179  protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
    179180    {
    180181
    181182        // these ones are probably never called, but put them here just in case
    182         Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
     183        Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
    183184        service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    184185        service_elem.setAttribute(GSXML.NAME_ATT, service_id);
     
    190191
    191192        // Create a new (empty) result message
    192         Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     193      Document result_doc = XMLConverter.newDOM();
     194        Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
    193195        String lang = request.getAttribute(GSXML.LANG_ATT);
    194196        result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
     
    204206        if (param_list == null)
    205207        {
    206             GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     208            GSXML.addError(result, "DocumentMetadataRetrieve: missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    207209            return result;
    208210        }
     
    232234        if (!all_metadata && metadata_names_list.size() == 0)
    233235        {
    234             GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: no metadata names found in the " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     236            GSXML.addError(result, "DocumentMetadataRetrieve: no metadata names found in the " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    235237            return result;
    236238        }
     
    240242        if (request_node_list == null)
    241243        {
    242             GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: missing " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     244            GSXML.addError(result, "DocumentMetadataRetrieve: missing " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    243245            return result;
    244246        }
    245247
    246248        // copy the request doc node list to the response
    247         Element response_node_list = (Element) this.doc.importNode(request_node_list, true);
     249        Element response_node_list = (Element) result_doc.importNode(request_node_list, true);
    248250        result.appendChild(response_node_list);
    249251
    250252        // use the copied list so that we add the metadata into the copy
    251253        // are we just adding metadata for the top level nodes? or can we accept a hierarchy here???
    252         NodeList request_nodes = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
    253         if (request_nodes.getLength() == 0)
    254         {
    255             GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     254        NodeList doc_nodes = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
     255        if (doc_nodes.getLength() == 0)
     256        {
     257            GSXML.addError(result, "DocumentMetadataRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    256258            return result;
    257259        }
    258260
    259261        // Whew, now we have checked (almost) all the syntax of the request, now we can process it.
    260         for (int i = 0; i < request_nodes.getLength(); i++)
    261         {
    262             Element request_node = (Element) request_nodes.item(i);
    263             String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
     262        for (int i = 0; i < doc_nodes.getLength(); i++)
     263        {
     264            Element doc_node = (Element) doc_nodes.item(i);
     265            String node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
    264266            boolean is_href_id = false;
    265267            if (node_id.equals(""))
    266268            {
    267                 node_id = getGreenstoneIdFromHref(request_node);
     269                node_id = getGreenstoneIdFromHref(doc_node);
    268270                if (node_id == null)
    269271                {
    270272                    // **** TODO, is this good enough???
    271                     request_node.setAttribute("external_link", "true");
     273                    doc_node.setAttribute("external_link", "true");
    272274                    continue;
    273275                }
     
    287289            try
    288290            {
    289               Element metadata_list = getMetadataList(node_id, all_metadata, metadata_names_list, lang);
     291              Element metadata_list = getMetadataList(result_doc, node_id, all_metadata, metadata_names_list, lang);
    290292                if (metadata_list != null)
    291293                {
    292                     request_node.appendChild(metadata_list);
     294                    doc_node.appendChild(metadata_list);
    293295                }
    294296            }
    295297            catch (GSException e)
    296298            {
    297                 GSXML.addError(this.doc, result, e.getMessage(), e.getType());
     299                GSXML.addError(result, e.getMessage(), e.getType());
    298300                if (e.getType().equals(GSXML.ERROR_TYPE_SYSTEM))
    299301                {
     
    312314
    313315        // Create a new (empty) result message
    314         Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     316      Document result_doc = XMLConverter.newDOM();
     317        Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
    315318        result.setAttribute(GSXML.FROM_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
    316319        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     
    328331        if (param_list == null)
    329332        {
    330             GSXML.addError(this.doc, result, "DocumentStructureRetrieve: missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     333            GSXML.addError(result, "DocumentStructureRetrieve: missing " + GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    331334            return result;
    332335        }
     
    336339        if (query_doc_list == null)
    337340        {
    338             GSXML.addError(this.doc, result, "DocumentStructureRetrieve: missing " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     341            GSXML.addError(result, "DocumentStructureRetrieve: missing " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    339342            return result;
    340343        }
    341344
    342345        // copy the doc_list to the response
    343         Element response_node_list = (Element) this.doc.importNode(query_doc_list, true);
     346        Element response_node_list = (Element) result_doc.importNode(query_doc_list, true);
    344347        result.appendChild(response_node_list);
    345348
     
    348351        if (node_list.getLength() == 0)
    349352        {
    350             GSXML.addError(this.doc, result, "DocumentStructureRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     353            GSXML.addError(result, "DocumentStructureRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    351354            return result;
    352355        }
     
    407410            want_children = false;
    408411
     412        // for each of the doc nodes, get the required info
     413        // these nodes are part of result_doc - can use that to create Elements
    409414        for (int i = 0; i < node_list.getLength(); i++)
    410415        {
    411             Element doc = (Element) node_list.item(i);
    412 
    413             String doc_id = doc.getAttribute(GSXML.NODE_ID_ATT);
     416            Element doc_node = (Element) node_list.item(i);
     417
     418            String doc_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
    414419            boolean is_href_id = false;
    415420            if (doc_id.equals(""))
    416421            {
    417                 doc_id = getGreenstoneIdFromHref(doc);
     422                doc_id = getGreenstoneIdFromHref(doc_node);
    418423                if (doc_id == null)
    419424                {
    420425                    // **** TODO, is this good enough???
    421                     doc.setAttribute("external_link", "true");
     426                    doc_node.setAttribute("external_link", "true");
    422427                    continue;
    423428                }
    424                 doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
     429                doc_node.setAttribute(GSXML.NODE_ID_ATT, doc_id);
    425430            }
    426431
     
    428433            {
    429434                doc_id = translateId(doc_id);
    430                 doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
     435                doc_node.setAttribute(GSXML.NODE_ID_ATT, doc_id);
    431436            }
    432437
     
    438443            if (want_info)
    439444            {
    440                 Element node_info_elem = this.doc.createElement("nodeStructureInfo");
    441                 doc.appendChild(node_info_elem);
     445              Element node_info_elem = result_doc.createElement("nodeStructureInfo");
     446                doc_node.appendChild(node_info_elem);
    442447
    443448                for (int j = 0; j < info_types.size(); j++)
     
    447452                    if (info_value != null)
    448453                    {
    449                         Element info_elem = this.doc.createElement("info");
     454                        Element info_elem = result_doc.createElement("info");
    450455                        info_elem.setAttribute(GSXML.NAME_ATT, info_type);
    451456                        info_elem.setAttribute(GSXML.VALUE_ATT, info_value);
     
    458463            {
    459464                // all structure info goes into a nodeStructure elem
    460                 Element structure_elem = this.doc.createElement(GSXML.NODE_STRUCTURE_ELEM);
    461                 doc.appendChild(structure_elem);
     465                Element structure_elem = result_doc.createElement(GSXML.NODE_STRUCTURE_ELEM);
     466                doc_node.appendChild(structure_elem);
    462467
    463468                if (want_entire_structure)
    464469                {
    465470                    String root_id = getRootId(doc_id);
    466                     Element root_node = createDocNode(root_id); //, true, false);
     471                    Element root_node = createDocNode(result_doc, root_id); //, true, false);
    467472                    addDescendants(root_node, root_id, true);
    468473                    structure_elem.appendChild(root_node);
     
    471476
    472477                // Add the requested structure information
    473                 Element base_node = createDocNode(doc_id); //, false, false);
     478                Element base_node = createDocNode(result_doc, doc_id); //, false, false);
    474479
    475480                //Ancestors: continually add parent nodes until the root is reached
     
    484489                        if (parent_id == null)
    485490                            break; // no parent
    486                         Element parent_node = createDocNode(parent_id);
     491                        Element parent_node = createDocNode(result_doc, parent_id);
    487492                        parent_node.appendChild(top_node);
    488493                        current_id = parent_id;//.getAttribute(GSXML.NODE_ID_ATT);
     
    496501                    if (parent_id != null)
    497502                    {
    498                         Element parent_node = createDocNode(parent_id);
     503                      Element parent_node = createDocNode(result_doc, parent_id);
    499504                        parent_node.appendChild(base_node);
    500505                        top_node = parent_node;
     
    541546    {
    542547        // Create a new (empty) result message
    543         Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     548      Document result_doc = XMLConverter.newDOM();
     549        Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
    544550        result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
    545551        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     
    564570
    565571        // copy the request doc node list to the response
    566         Element response_node_list = (Element) this.doc.importNode(query_doc_list, true);
     572        Element response_node_list = (Element) result_doc.importNode(query_doc_list, true);
    567573        result.appendChild(response_node_list);
    568574
    569         NodeList request_nodes = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
    570         if (request_nodes.getLength() == 0)
    571         {
    572             GSXML.addError(this.doc, result, "DocumentContentRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    573             return result;
    574         }
    575 
    576         //Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
     575        NodeList doc_nodes = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
     576        if (doc_nodes.getLength() == 0)
     577        {
     578            GSXML.addError(result, "DocumentContentRetrieve: no " + GSXML.DOC_NODE_ELEM + " found in the " + GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     579            return result;
     580        }
     581
     582        //Element doc_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
    577583        //result.appendChild(doc_list);
    578584
     
    583589        //String[] is_externals = GSXML.getAttributeValuesFromList(query_doc_list, "externalURL");
    584590
    585         for (int i = 0; i < request_nodes.getLength(); i++)
    586         {
    587             Element request_node = (Element) request_nodes.item(i);
    588             String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
     591        for (int i = 0; i < doc_nodes.getLength(); i++)
     592        {
     593            Element doc_node = (Element) doc_nodes.item(i);
     594            String node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
    589595           
    590596            if (node_id.equals(""))
    591597            {
    592                 node_id = getGreenstoneIdFromHref(request_node);
     598                node_id = getGreenstoneIdFromHref(doc_node);
    593599                if (node_id == null)
    594600                {
    595601                    // **** TODO, is this good enough???
    596                     request_node.setAttribute("external_link", "true");
     602                    doc_node.setAttribute("external_link", "true");
    597603                    continue;
    598604                }
    599                 request_node.setAttribute(GSXML.NODE_ID_ATT, node_id);
     605                doc_node.setAttribute(GSXML.NODE_ID_ATT, node_id);
    600606            }
    601607
     
    612618            try
    613619            {
    614                 Element node_content = getNodeContent(node_id, lang);
     620              Element node_content = getNodeContent(result_doc, node_id, lang);
    615621                if (node_content != null)
    616622                {
    617                     request_node.appendChild(node_content);
     623                  doc_node.appendChild(node_content);
    618624                }
    619625            }
    620626            catch (GSException e)
    621627            {
    622                 GSXML.addError(this.doc, result, e.getMessage());
     628                GSXML.addError(result, e.getMessage());
    623629                return result;
    624630            }
     
    631637     * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'/>
    632638     */
    633     protected Element createDocNode(String node_id)
    634     {
    635         return this.gs_doc.createDocNode(node_id);
     639  protected Element createDocNode(Document doc, String node_id)
     640    {
     641      return this.gs_doc.createDocNode(doc, node_id);
    636642    }
    637643
     
    759765     * <metadataList><metadata name="xxx">value</metadata></metadataList>
    760766     */
    761   abstract protected Element getMetadataList(String doc_id, boolean all_metadata, ArrayList<String> metadata_names, String lang) throws GSException;
     767  abstract protected Element getMetadataList(Document doc, String doc_id, boolean all_metadata, ArrayList<String> metadata_names, String lang) throws GSException;
    762768
    763769    /**
     
    765771     * <nodeContent>text content or other elements</nodeContent> can return
    766772     */
    767     abstract protected Element getNodeContent(String doc_id, String lang) throws GSException;
     773  abstract protected Element getNodeContent(Document doc, String doc_id, String lang) throws GSException;
    768774
    769775    /**
Note: See TracChangeset for help on using the changeset viewer.