Ignore:
Timestamp:
2005-05-16T11:02:50+12:00 (19 years ago)
Author:
kjdon
Message:

merged from branch ant-install-branch: merge 1

File:
1 edited

Legend:

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

    r9288 r9874  
    2323// Greenstone classes
    2424//import org.greenstone.gdbm.*;
     25import org.greenstone.gsdl3.core.GSException;
    2526import org.greenstone.gsdl3.util.GSXML;
    2627import org.greenstone.gsdl3.util.GSPath;
     
    165166    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    166167    if (param_list == null) {
    167         System.err.println("AbstractDocumentRetrieve, DocumentMetadataRetrieve Error: missing paramList.\n");
    168         return result;  // Return the empty result
     168        GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     169        return result; 
    169170    }
    170171
     
    190191    }
    191192   
    192     Element node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    193     result.appendChild(node_list);
    194    
     193    // check that there has been some metadata specified
     194    if (!all_metadata && metadata_names_list.size()==0) {
     195        GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: no metadata names found in the "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     196        return result; 
     197    }
     198
    195199    // Get the documents
    196200    Element request_node_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    197201    if (request_node_list == null) {
    198         System.err.println("Error: DocumentMetadataRetrieve request had no "+GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
     202        GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: missing " +GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    199203        return result;
    200204    }
    201205   
    202     NodeList request_nodes = request_node_list.getChildNodes();
     206    // copy the request doc node list to the response
     207    Element response_node_list = (Element) this.doc.importNode(request_node_list, true);
     208    result.appendChild(response_node_list);
     209   
     210    // use the copied list so that we add the metadata into the copy
     211    // are we just adding metadata for the top level nodes? or can we accept a hierarchy here???
     212    NodeList request_nodes = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
     213    if (request_nodes.getLength()==0) {
     214        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);
     215        return result;
     216    }
     217   
     218    // Whew, now we have checked (almost) all the syntax of the request, now we can process it.
     219   
    203220    for (int i = 0; i < request_nodes.getLength(); i++) {
    204221        Element request_node = (Element) request_nodes.item(i);
    205222        String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
    206223       
    207         // Add the document to the results list
    208         Element new_node = (Element)this.doc.importNode(request_node, false);
    209         node_list.appendChild(new_node);
    210 
    211224        if (external_id) {
    212225        // can we have .pr etc extensions with external ids?
     
    219232        continue;
    220233        }
    221        
    222         Element metadata_list = getMetadataList(node_id, all_metadata, metadata_names_list);
    223         new_node.appendChild(metadata_list);
     234        try {
     235        Element metadata_list = getMetadataList(node_id, all_metadata, metadata_names_list);
     236        request_node.appendChild(metadata_list);
     237        } catch (GSException e) {       
     238        GSXML.addError(this.doc, result, e.getMessage(), e.getType());
     239        if (e.getType().equals(GSXML.ERROR_TYPE_SYSTEM)) {
     240            // there is no point trying any others
     241            return result;
     242        }
     243        }
    224244    }
    225245   
     
    240260
    241261    String lang = request.getAttribute(GSXML.LANG_ATT);
    242     Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    243     if (query_doc_list == null) {
    244         System.err.println("AbstractDocumentRetrieve Error: DocumentStructureRetrieve request specified no doc nodes.\n");
    245         return result;
    246     }
    247262
    248263    // Get the parameters of the request
    249264    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    250265    if (param_list == null) {
    251         System.err.println("AbstractDocumentRetrieve Error: DocumentStructureRetrieve request had no paramList.");
    252         return result;  // Return the empty result
    253     }
     266        GSXML.addError(this.doc, result, "DocumentStructureRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     267        return result; 
     268    }
     269   
     270    // get the documents of the request
     271    Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
     272    if (query_doc_list == null) {
     273        GSXML.addError(this.doc, result, "DocumentStructureRetrieve: missing " +GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     274        return result;
     275    }
     276   
     277    // copy the doc_list to the response
     278    Element response_node_list = (Element) this.doc.importNode(query_doc_list, true);
     279    result.appendChild(response_node_list);
     280   
     281    // check that we have some doc nodes specified
     282    NodeList node_list = GSXML.getChildrenByTagName(response_node_list, GSXML.DOC_NODE_ELEM);
     283    if (node_list.getLength()==0) {
     284        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);
     285        return result;
     286    }
     287
    254288    Element extid_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, EXTID_PARAM);
    255289    boolean external_id = false;
     
    308342    if (want_descendants)
    309343        want_children = false;
    310    
    311     // the document list to hold the results
    312     Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    313     result.appendChild(doc_list);
    314    
    315     // Get the documents
    316     String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
    317                                 GSXML.NODE_ID_ATT);
    318     for (int i = 0; i < doc_ids.length; i++) {
    319         // Add the document to the list
    320         Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
    321         doc_list.appendChild(doc);
    322        
    323         String doc_id = doc_ids[i];
    324         doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
     344
     345    for (int i=0; i < node_list.getLength(); i++) {
     346        Element doc = (Element) node_list.item(i);
     347        String doc_id = doc.getAttribute(GSXML.NODE_ID_ATT);
    325348        if (external_id) {
    326349        doc_id = translateExternalId(doc_id);
     
    433456    }
    434457
    435     // Get the parameters of the request - no parameters at this stage
     458    // Get the parameters of the request
    436459    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    437460    Element extid_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, EXTID_PARAM);
     
    473496        continue;
    474497        }
    475        
    476         Element node_content = getNodeContent(doc_id);
    477         doc.appendChild(node_content);
     498        try {
     499        Element node_content = getNodeContent(doc_id);
     500        doc.appendChild(node_content);
     501        } catch (GSException e) {
     502        GSXML.addError(this.doc, result, e.getMessage());
     503        return result;
     504       
     505        }
    478506    }
    479507    return result;
     
    594622    }
    595623
    596     /** if id ends in .fc, .pc etc, then translate it to the correct id */
    597     abstract protected String translateId(String id);
     624    /** if id ends in .fc, .pc etc, then translate it to the correct id
     625     * default implementation: just remove the suffix */
     626    protected String translateId(String id) {
     627    return id.substring(0,id.length());
     628    }
    598629   
    599630    /** if an id is not a greenstone id (an external id) then translate
    600     it to a greenstone one*/
    601     abstract protected String translateExternalId(String id);
     631     * it to a greenstone one
     632     * default implementation: return the id */
     633    protected String translateExternalId(String id) {
     634    return id;
     635    }
     636
    602637    /** returns the document type of the doc that the specified node
    603638    belongs to. should be one of
     
    605640    GSXML.DOC_TYPE_PAGED,
    606641    GSXML.DOC_TYPE_HIERARCHY
     642    default implementation: return DOC_TYPE_SIMPLE
    607643    */
    608     abstract protected String getDocType(String node_id);
    609 
    610     /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
    611     abstract protected String getRootId(String node_id);
    612     /** returns a list of the child ids in order, null if no children */
    613     abstract protected ArrayList getChildrenIds(String node_id);
    614     /** returns the node id of the parent node, null if no parent */
    615     abstract protected String getParentId(String node_id);
     644    protected String getDocType(String node_id) {
     645    return GSXML.DOC_TYPE_SIMPLE;
     646    }
     647   
     648
     649    /** returns the id of the root node of the document containing
     650     * node node_id. may be the same as node_id
     651     * default implemntation: return node_id
     652    */
     653    protected String getRootId(String node_id) {
     654    return node_id;
     655    }
     656    /** returns a list of the child ids in order, null if no children
     657     * default implementation: return null */
     658    protected ArrayList getChildrenIds(String node_id) {
     659    return null;
     660    }
     661    /** returns the node id of the parent node, null if no parent
     662     * default implementation: return null */
     663    protected String getParentId(String node_id) {
     664    return null;
     665    }
    616666
    617667    /** get the metadata for the doc node doc_id
     
    621671    abstract protected Element getMetadataList(String doc_id,
    622672                           boolean all_metadata,
    623                            ArrayList metadata_names);
     673                           ArrayList metadata_names) throws GSException;
    624674    /** returns the content of a node
    625      * shoudl return a nodeContent element:
     675     * should return a nodeContent element:
    626676     * <nodeContent>text content or other elements</nodeContent>
     677     * can return
    627678     */
    628     abstract protected Element getNodeContent(String doc_id);
     679    abstract protected Element getNodeContent(String doc_id) throws GSException;
    629680
    630681    /** returns the structural information asked for.
Note: See TracChangeset for help on using the changeset viewer.