Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xmltree » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   package org.apache.xmlbeans.samples.xmltree;
   16   
   17   import org.apache.xmlbeans.XmlCursor;
   18   import org.apache.xmlbeans.XmlObject;
   19   
   20   import javax.xml.namespace.QName;
   21   
   22   /**
   23    * Represents the data for a single node in the XmlTree. This class (known as a
   24    * "user object" from the JTree perspective) provides a way to get information
   25    * about the node by essentially wrapping the XmlObject instance that the node
   26    * represents. The {@link XmlModel}class represents the XML data model to the
   27    * tree by calling methods of this object.
   28    */
   29   public final class XmlEntry
   30   {
   31       private XmlObject[] m_children = new XmlObject[0];
   32   
   33       private final XmlObject m_currentXml;
   34   
   35       private String m_label;
   36   
   37       /**
   38        * Constructs a entry using <em>xml</em> as the data source.
   39        * 
   40        * @param xml The XML this entry will represent.
   41        */
   42       public XmlEntry(XmlObject xml)
   43       {
   44           m_currentXml = xml;
   45           m_children = collectChildren(xml);
   46   
   47           // Add a cursor and use it to extract information to display in the
   48           // tree.
   49           XmlCursor cursor = xml.newCursor();
   50           if (!cursor.currentTokenType().isStart())
   51           {
   52               cursor.toFirstChild();
   53           }
   54           m_label = cursor.getAttributeText(new QName("label"));
   55           if (m_label == null || m_label.equals(""))
   56           {
   57               m_label = cursor.getName().getLocalPart();
   58           }
   59           cursor.dispose();
   60       }
   61   
   62       /**
   63        * Collects the children of the <em>xml</em> element.
   64        * 
   65        * @param xml The XML element whose children should be collected.
   66        * @return An array of <em>xml</em>'s children.
   67        */
   68       private XmlObject[] collectChildren(XmlObject xml)
   69       {
   70           return xml.selectPath("./*");
   71       }
   72   
   73       /**
   74        * Gets the number of children of the XML this entry represents.
   75        * 
   76        * @return The number of children.
   77        */
   78       public int getChildCount()
   79       {
   80           return m_children.length;
   81       }
   82   
   83       /**
   84        * Gets the child at <em>index</em> from among the children of the XML
   85        * this entry represents.
   86        * 
   87        * @param index The index number for the child to get.
   88        * @return An entry representing the child.
   89        */
   90       public XmlEntry getChild(int index)
   91       {
   92           XmlEntry childEntry = new XmlEntry(m_children[index]);
   93           return childEntry;
   94       }
   95   
   96       /**
   97        * Gets the children of the XML this entry represents.
   98        * 
   99        * @return An entry array representing the children.
  100        */
  101       public XmlEntry[] getChildren()
  102       {
  103           XmlEntry[] entryChildren = new XmlEntry[getChildCount()];
  104           for (int i = 0; i < getChildCount(); i++)
  105           {
  106               entryChildren[i] = new XmlEntry(m_children[i]);
  107           }
  108           return entryChildren;
  109       }
  110   
  111       /**
  112        * Returns a name that can be used as a tree node label.
  113        * 
  114        * @return The name of the element or attribute this entry represents.
  115        */
  116       public String toString()
  117       {
  118           return m_label;
  119       }
  120   
  121       /**
  122        * Gets the XML that this instance represents.
  123        * 
  124        * @return An XmlObject instance representing the XML.
  125        */
  126       public XmlObject getXml()
  127       {
  128           return m_currentXml;
  129       }
  130   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xmltree » [javadoc | source]