Changeset 28963 for main/trunk


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

moved a method here from XMLConverter. Removed Document param from methods that take an Element - now we get the Element's owner document and use that to create nodes. So now no chance that you can pass in the wrong Document for the Element thatyou are adding nodes to

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r28958 r28963  
    5959    public static final String PARAM_ELEM = "param";
    6060    public static final String PARAM_OPTION_ELEM = "option";
     61        public static final String LIBRARY_PARAM_ELEM = "libraryParam";
    6162    public static final String CONTENT_ELEM = "content";
    6263    public static final String RESOURCE_ELEM = "resource";
     
    460461
    461462    /** add text to a document/subsection element */
    462     public static boolean addDocText(Document owner, Element doc, String text)
    463     {
    464 
    465         Element content = owner.createElement(NODE_CONTENT_ELEM);
    466         Text t = owner.createTextNode(text);
     463    public static boolean addDocText(Element doc, String text)
     464    {
     465     
     466      Element content = doc.getOwnerDocument().createElement(NODE_CONTENT_ELEM);
     467        Text t = doc.getOwnerDocument().createTextNode(text);
    467468        content.appendChild(t);
    468469        doc.appendChild(content);
     
    471472
    472473    /** add an error message, unknown error type */
    473     public static boolean addError(Document owner, Element doc, String text)
    474     {
    475         return addError(owner, doc, text, ERROR_TYPE_OTHER);
     474  public static boolean addError(Element doc, String text)
     475    {
     476        return addError(doc, text, ERROR_TYPE_OTHER);
    476477    }
    477478
    478479    /** add an error message */
    479     public static boolean addError(Document owner, Element doc, String text, String error_type)
    480     {
    481 
    482         Element content = owner.createElement(ERROR_ELEM);
     480    public static boolean addError(Element doc, String text, String error_type)
     481    {
     482
     483        Element content = doc.getOwnerDocument().createElement(ERROR_ELEM);
    483484        content.setAttribute(ERROR_TYPE_ATT, error_type);
    484         Text t = owner.createTextNode(text);
     485        Text t = doc.getOwnerDocument().createTextNode(text);
    485486        content.appendChild(t);
    486487        doc.appendChild(content);
     
    489490
    490491    /** add an error message */
    491     public static boolean addError(Document owner, Element doc, Throwable error)
    492     {
    493         return addError(owner, doc, error, ERROR_TYPE_OTHER);
     492    public static boolean addError(Element doc, Throwable error)
     493    {
     494        return addError(doc, error, ERROR_TYPE_OTHER);
    494495    }
    495496
    496497    /** add an error message */
    497     public static boolean addError(Document owner, Element doc, Throwable error, String error_type)
     498    public static boolean addError(Element doc, Throwable error, String error_type)
    498499    {
    499500        error.printStackTrace();
    500         return addError(owner, doc, error.toString(), error_type);
     501        return addError(doc, error.toString(), error_type);
    501502    }
    502503
     
    518519
    519520    /** adds a metadata elem to a list */
    520     public static boolean addMetadata(Document owner, Element list, String meta_name, String meta_value)
     521    public static boolean addMetadata(Element list, String meta_name, String meta_value)
    521522    {
    522523        if (meta_value == null || meta_value.equals(""))
     
    524525            return false;
    525526        }
     527        Document owner = list.getOwnerDocument();
    526528        Element data = owner.createElement(METADATA_ELEM);
    527529        data.setAttribute(NAME_ATT, meta_name);
     
    818820      }
    819821    }
     822
     823    /**
     824     * Given a Node representing an Element or Document, will return the
     825     * Element/docroot Element. Returns null if the Node was not an element.
     826     */
     827    public static Element nodeToElement(Node node)
     828    {
     829        if (node == null)
     830        {
     831            return null;
     832        }
     833        short nodeType = node.getNodeType();
     834
     835        if (nodeType == Node.DOCUMENT_NODE)
     836        {
     837            Document docNode = (Document) node;
     838            return docNode.getDocumentElement();
     839        }
     840        else if (nodeType == Node.ELEMENT_NODE)
     841        {
     842            return (Element) node;
     843        }
     844        else
     845        {
     846            String message = "Expecting Document or Element node type but got " + node.getNodeName() + "\nReturning null";
     847            System.err.println(message);
     848            logger.warn(message);
     849            return null;
     850        }
     851    }
     852 
    820853
    821854    /** returns a basic request message */
     
    907940    }
    908941
    909     public static void addParametersToList(Document owner, Element param_list, HashMap params)
     942    public static void addParametersToList(Element param_list, HashMap params)
    910943    {
    911944        if (params == null)
     
    916949        Set items = params.entrySet();
    917950        Iterator i = items.iterator();
     951        Document owner = param_list.getOwnerDocument();
    918952        while (i.hasNext())
    919953        {
Note: See TracChangeset for help on using the changeset viewer.