001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email ian.dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 04-Dec-2003
009: * Filename $RCSfile: SimpleXMLPathAttr.java,v $
010: * Revision $Revision: 1.7 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:09:42 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * [See end of file]
018: *****************************************************************************/package com.hp.hpl.jena.util.xml;
019:
020: // Imports
021: ///////////////
022: import java.util.*;
023:
024: import org.w3c.dom.*;
025:
026: /**
027: * <p>
028: * An implementation of a simple XML path component that handles named attributes
029: * </p>
030: *
031: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
032: * @version CVS $Id: SimpleXMLPathAttr.java,v 1.7 2008/01/02 12:09:42 andy_seaborne Exp $
033: */
034: public class SimpleXMLPathAttr implements SimpleXMLPathComponent {
035: // Constants
036: //////////////////////////////////
037:
038: // Static variables
039: //////////////////////////////////
040:
041: /** The element name we are evaluating */
042: protected String m_attrName;
043:
044: // Constructors
045: //////////////////////////////////
046:
047: /**
048: * <p>Construct a simple XML path component that selects the value
049: * of a named attribute.</p>
050: * @param attrName The name of the attribute to select
051: */
052: public SimpleXMLPathAttr(String attrName) {
053: m_attrName = attrName;
054: }
055:
056: // External signature methods
057: //////////////////////////////////
058:
059: /**
060: * <p>Answer an iterator over all of the values of this path component when
061: * evaluated with respect to the given node.</p>
062: * @param node The parent node to evaluate against
063: * @return An iterator over all of the objects that correspond to evaluating
064: * this path against the given node.
065: */
066: public Iterator getAll(Node node) {
067: // elements should occur within elements
068: if (!(node instanceof Element)) {
069: throw new IllegalArgumentException(
070: "Tried to get attribute " + m_attrName
071: + " from a parent node of type "
072: + node.getClass().getName());
073: }
074:
075: List attr = new ArrayList();
076: Element e = (Element) node;
077: if (e.hasAttribute(m_attrName)) {
078: attr.add(e.getAttribute(m_attrName));
079: }
080:
081: return attr.iterator();
082: }
083:
084: /**
085: * <p>Answer the first value for this path expression against the given node.</p>
086: * @param node The parent node to evalauate against
087: * @return The first object that corresponds to evaluating
088: * this path against the given node, or null if there is no such value
089: */
090: public Object getFirst(Node node) {
091: return ((Element) node).getAttribute(m_attrName);
092: }
093:
094: // Internal implementation methods
095: //////////////////////////////////
096:
097: //==============================================================================
098: // Inner class definitions
099: //==============================================================================
100:
101: }
102:
103: /*
104: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
105: * All rights reserved.
106: *
107: * Redistribution and use in source and binary forms, with or without
108: * modification, are permitted provided that the following conditions
109: * are met:
110: * 1. Redistributions of source code must retain the above copyright
111: * notice, this list of conditions and the following disclaimer.
112: * 2. Redistributions in binary form must reproduce the above copyright
113: * notice, this list of conditions and the following disclaimer in the
114: * documentation and/or other materials provided with the distribution.
115: * 3. The name of the author may not be used to endorse or promote products
116: * derived from this software without specific prior written permission.
117: *
118: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
119: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
120: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
121: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
122: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
123: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
124: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
125: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
126: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
127: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128: */
|