You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by bc...@locus.apache.org on 2000/11/21 08:16:54 UTC

cvs commit: jakarta-slide/src/clients/webdav/src/org/apache/webdav/lib/util DOMUtils.java

bcholmes    00/11/20 23:16:53

  Added:       src/clients/webdav/src/org/apache/webdav/lib/util
                        DOMUtils.java
  Log:
  Revisions to the client library for:
  - removing unnecessary import statements in methods
  - parse the response from PROPFIND
  - create an Ant Dav-aware GET task
  
  Revision  Changes    Path
  1.1                  jakarta-slide/src/clients/webdav/src/org/apache/webdav/lib/util/DOMUtils.java
  
  Index: DOMUtils.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  package org.apache.webdav.lib.util;
  
  import java.lang.reflect.Method;
  
  import java.io.InputStream;
  import java.io.IOException;
  
  import java.util.Enumeration;
  import java.util.StringTokenizer;
  
  import org.w3c.dom.Attr;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.NodeList;
  import org.w3c.dom.Text;
  
  
  /** This class provides some basic utility methods for working with
   *  XML Document objects.
   */
  public class DOMUtils {
  
      protected static Class[] getElementsByNSParameterTypes = 
          { String.class, String.class };
  
      public static boolean isDOM2Compliant(Document document) {
          boolean isDOM2 = false;
          try {
              Class documentClass = document.getClass();
              Method method = documentClass.getMethod(
                  "getElementsByTagNameNS", 
                  getElementsByNSParameterTypes );
  
              // can method ever be null?
              if (method != null) {  
                  isDOM2 = true;
              }
          } catch (NoSuchMethodException e) {
          }
          return isDOM2;
      }
  
  
      /**
       *  Determine the namespace prefix being used for DAV.
       *  Generally, DAV responses say something like:
       *
       *  <PRE>
       *  &lt;D:multistatus xmlns:D="DAV:"&gt;
       *  </PRE>
       *
       *  <P>  In this case, the "D:" is the prefix for DAV.
       */
      public static String findDavPrefix(Document document) {
          Element multistatus = document.getDocumentElement();
          NamedNodeMap list = multistatus.getAttributes();
          String prefix = "DAV:";
          for (int i = 0; i < list.getLength(); i++) {
              try {
                  Attr attr = (Attr) list.item(i);
                  if (attr.getName() != null &&
                      attr.getName().startsWith("xmlns") &&
                      attr.getValue().equals("DAV:")) {
                          int indx = attr.getName().indexOf(":");
                          if ((indx >= 0) && (indx < attr.getName().length())) {
                              prefix = attr.getName().substring(indx + 1) + ":";
                          } else {
                              prefix = "";
                          }
                  }
              } catch (ClassCastException e) {
              }
          }
          return prefix;
      }
  
  
      /**
       *  Scan all immediate children of a node, and append all
       *  text nodes into a string.  Consider the following example
       *
       *  <PRE>
       *  &lt;customer&gt;Joe Schmoe&lt;/customer&gt;
       *  </PRE>
       *
       *  <P>  In this case, calling this method on the 
       *  <CODE>customer</CODE> element returns "Joe Schmoe".
       */
      public static String getTextValue(Element element) {
  
          // I *thought* that I should be able to use element.getNodeValue()...
  
          String text = "";
          NodeList textList = element.getChildNodes();
          for (int i = 0; i < textList.getLength(); i++) {
              try {
                  text += ((Text) textList.item(i)).getData();
              } catch (ClassCastException e) {
                  // we don't care about non-Text nodes
              }
          }
          return text;
      }
  
      /**
       *  Get the status code out of the normal status response.
       *  
       *  <P>  Each <code>DAV:propstat</code> node contains a
       *  status line, such as:
       *
       *  <PRE>
       *  &lt;DAV:status&gt;HTTP/1.1 200 OK&lt;/DAV:status&gt;
       *  </PRE>
       *
       *  <P>  In this case, calling this method on the 
       *  text string returns 200.
       */
       public static int parseStatus(String statusString) {
          int status = -1;
          if (statusString != null) {
              StringTokenizer tokenizer = new StringTokenizer(statusString);
              if (tokenizer.countTokens() >= 2) {
                  Object dummy = tokenizer.nextElement();
                  String statusCode = tokenizer.nextElement().toString();
                  try {
                      status = Integer.parseInt(statusCode);
                  } catch (NumberFormatException e) {
                      throw new IllegalArgumentException(
                          "Status code is not numeric");
                  }
              } else {
                  throw new IllegalArgumentException(
                      "There aren't enough words in the input argument");
              }
          }
          return status;
      }
  }