001 /*
002 * $Id: DOMCategory.java,v 1.1 2004/04/27 02:25:29 spullara Exp $version Apr 25, 2004 5:18:30 PM $user Exp $
003 *
004 * Copyright 2003 (C) Sam Pullara. All Rights Reserved.
005 *
006 * Redistribution and use of this software and associated documentation
007 * ("Software"), with or without modification, are permitted provided that the
008 * following conditions are met: 1. Redistributions of source code must retain
009 * copyright statements and notices. Redistributions must also contain a copy of
010 * this document. 2. Redistributions in binary form must reproduce the above
011 * copyright notice, this list of conditions and the following disclaimer in the
012 * documentation and/or other materials provided with the distribution. 3. The
013 * name "groovy" must not be used to endorse or promote products derived from
014 * this Software without prior written permission of The Codehaus. For written
015 * permission, please contact info@codehaus.org. 4. Products derived from this
016 * Software may not be called "groovy" nor may "groovy" appear in their names
017 * without prior written permission of The Codehaus. "groovy" is a registered
018 * trademark of The Codehaus. 5. Due credit should be given to The Codehaus -
019 * http://groovy.codehaus.org/
020 *
021 * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
022 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
024 * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
025 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
030 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 *
032 */
033 package groovy.xml.dom;
034
035 import org.w3c.dom.Element;
036 import org.w3c.dom.NodeList;
037
038 /**
039 * @author sam
040 */
041 public class DOMCategory {
042
043 public static Object get(Element element, String elementName) {
044 return getAt(element, elementName);
045 }
046
047 public static Object getAt(Element element, int i) {
048 if (element.hasChildNodes()) {
049 NodeList nodeList = element.getChildNodes();
050 return nodeList.item(i);
051 }
052 return null;
053 }
054
055 public static Object getAt(Element element, String elementName) {
056 if (elementName.startsWith("@")) {
057 String attrName = elementName.substring(1);
058 return element.getAttribute(attrName);
059 }
060 if (element.hasChildNodes()) {
061 NodeList nodeList = element.getChildNodes();
062 for (int i = 0; i < nodeList.getLength(); i++) {
063 Object node = nodeList.item(i);
064 if (node instanceof Element) {
065 Element child = (Element) node;
066 child.hasChildNodes();
067 if(child.getTagName().equals(elementName)) {
068 return child;
069 }
070 }
071 }
072 }
073 return null;
074 }
075
076 }