Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » cursor » [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.cursor;
   16   
   17   import statement.StatementDocument;
   18   import statement.StatementDocument.Statement;
   19   import statement.Transaction;
   20   import java.io.File;
   21   
   22   import org.apache.xmlbeans.XmlCursor;
   23   
   24   import javax.xml.namespace.QName;
   25   
   26   public class OrderMatters
   27   {
   28       private static QName deposit = new QName( "http://statement", "deposit" );
   29   
   30       public static void main ( String[] args ) throws Exception
   31       {
   32           // load the xml instance into the store and return a
   33           // strongly typed instance of StatementDocument
   34           StatementDocument stmtDoc = StatementDocument.Factory.parse( new File( args[ 0 ] ) );
   35   
   36           System.out.println( "Valid statement instance? " + stmtDoc.validate() );
   37   
   38           float balance = balanceOutOfOrder(stmtDoc);
   39   
   40           System.out.println( "Ending balance: $" + balance );
   41   
   42           balance = balanceInOrder(stmtDoc);
   43   
   44           System.out.println( "Ending balance: $" + balance );
   45       }
   46   
   47       /**
   48        * Out of order balance: the ease of stronly-typed XmlObjects!
   49        */
   50       public static float balanceOutOfOrder(StatementDocument stmtDoc)
   51       {
   52           Statement stmt = stmtDoc.getStatement();
   53   
   54           float balance = 0;
   55   
   56           Transaction[] deposits    = stmt.getDepositArray();
   57           Transaction[] withdrawals = stmt.getWithdrawalArray();
   58   
   59           for ( int i = 0 ; i < deposits.length ; i++ )
   60               balance += deposits[ i ].getAmount();
   61   
   62           for ( int i = 0 ; i < withdrawals.length ; i++ )
   63               balance -= withdrawals[ i ].getAmount();
   64   
   65           return balance;
   66       }
   67   
   68       /**
   69        * In order balance: the power of XmlCursor!
   70        */
   71       public static float balanceInOrder(StatementDocument stmtDoc)
   72       {
   73           float balance = 0;
   74   
   75           XmlCursor cursor = stmtDoc.newCursor();
   76   
   77           // use xpath to select elements
   78           cursor.selectPath( "*/*" );
   79   
   80           // iterate over the selection
   81           while ( cursor.toNextSelection() )
   82           {
   83               // two views of the same data:
   84               // move back and forth between XmlObject <-> XmlCursor
   85               Transaction trans = (Transaction) cursor.getObject();
   86   
   87               float amount = trans.getAmount();
   88   
   89               if (cursor.getName().equals( deposit ))
   90                   balance += amount;
   91               else if ((balance -= amount) < 0)
   92               {
   93                   // doh!
   94                   System.out.println( "Negative balance: $" + balance );
   95                   balance -= 50;
   96               }
   97           }
   98   
   99           return balance;
  100       }
  101   
  102   }

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