View Javadoc

    1   /*
    2    * Common Library for TBAS Softwares
    3    * Language: Java
    4    *
    5    * All Rights Reserved.
    6    * (c) Copyright 2002 by Tsukuba Bunko.
    7    *
    8    * $Id: ListDeserializer.java,v 1.1 2005/07/11 12:49:18 ppoi Exp $
    9    */
    10  package tsukuba_bunko.resource;
    11
    12  import	java.util.List;
    13
    14  import	org.xml.sax.Attributes;
    15  import	org.xml.sax.ContentHandler;
    16  import	org.xml.sax.SAXException;
    17
    18
    19  /***
    20   * List 構造を取得する <code>ValueDescerializer</code> です.
    21   * @author	$Author: ppoi $
    22   */
    23  public class ListDeserializer	extends BasicDeserializer	{
    24
    25  	/***
    26  	 * リスト
    27  	 */
    28  	private	List	_list = null;
    29
    30  	/***
    31  	 * 要素を解析するための ResourceDeserializer
    32  	 */
    33  	private ResourceDeserializer	_itemDeserializer = null;
    34
    35  	/***
    36  	 * 要素を解析するための ContentHandler
    37  	 */
    38  	private ContentHandler	_itemHandler = null;
    39
    40  	/***
    41  	 * リスト要素のデータ型
    42  	 */
    43  	private String	_itemType = null;
    44
    45  	/***
    46  	 * レベル
    47  	 */
    48  	private int	_level = 0;
    49
    50
    51  	/***
    52  	 * <code>ListDeserializer</code> のインスタンスを作成します.
    53  	 */
    54  	public ListDeserializer()
    55  	{
    56  		super();
    57  	}
    58
    59
    60  //
    61  //	BasicDeserialzier の実装
    62  //
    63  	public void setTypeName( String typeName )
    64  	{
    65  		super.setTypeName( typeName );
    66  		int	index = typeName.indexOf( '/' );
    67  		if( index == -1 )	{
    68  			throw new IllegalArgumentException( "illegal list item type. \"" + typeName + "\"" );
    69  		}
    70  		else	{
    71  			_itemType = typeName.substring( index + 1 );
    72  		}
    73  	}
    74
    75
    76  //
    77  //	ContentHandler の実装
    78  //
    79  	public void startDocument()
    80  	{
    81  		_list = null;
    82  		_level = 0;
    83  	}
    84
    85  	public void endDocument()
    86  	{
    87  		setValue( _list );
    88  		_list = null;
    89  	}
    90
    91  	public void startPrefixMapping( String namespaceURI, String prefix )
    92  		throws SAXException
    93  	{
    94  		if( _itemHandler != null )	{
    95  			_itemHandler.startPrefixMapping( namespaceURI, prefix );
    96  		}
    97  	}
    98
    99  	public void endPrefixMapping( String namespaceURI )
    100 		throws SAXException
    101 	{
    102 		if( _itemHandler != null )	{
    103 			_itemHandler.endPrefixMapping( namespaceURI );
    104 		}
    105 	}
    106
    107 	public void startElement( String namespaceURI, String localName, String qName, Attributes attrs )
    108 		throws SAXException
    109 	{
    110 		if( _list == null )	{
    111 			_list = new java.util.ArrayList();
    112 			_itemDeserializer = _mapping.getResourceDeserializer( _itemType );
    113 			if( _itemDeserializer == null )	{
    114 				throw new SAXException( "no deserializer for \"" + _itemType + "\"" );
    115 			}
    116 		}
    117 		else if( _itemHandler == null )	{
    118 			if( localName.equals("item") )	{
    119 				_level++;
    120 				_itemHandler = _itemDeserializer;
    121 				_itemHandler.startElement( namespaceURI, localName, qName, attrs );
    122 			}
    123 			else	{
    124 				throw new SAXException( "illegal structure. : non-item element \"" + qName + "\"" );
    125 			}
    126 		}
    127 		else	{
    128 			_level++;
    129 			_itemHandler.startElement( namespaceURI, localName, qName, attrs );
    130 		}
    131 	}
    132
    133 	public void endElement( String namespaceURI, String localName, String qName )
    134 		throws SAXException
    135 	{
    136 		try	{
    137 		if( _itemHandler != null )	{
    138 			_level--;
    139 			_itemHandler.endElement( namespaceURI, localName, qName );
    140 			if( _level == 0 )	{
    141 				_list.add( _itemDeserializer.getValue() );
    142 				_itemHandler = null;
    143 			}
    144 		}
    145 		}catch( Exception e	)	{
    146 			e.printStackTrace();
    147 		}
    148 	}
    149
    150 	public void processingInstruction( String target, String data )
    151 		throws SAXException
    152 	{
    153 		if( _itemHandler != null )	{
    154 			_itemHandler.processingInstruction( target, data );
    155 		}
    156 	}
    157
    158 	public void skippedEntity( String name )
    159 		throws SAXException
    160 	{
    161 		if( _itemHandler != null )	{
    162 			_itemHandler.skippedEntity( name );
    163 		}
    164 	}
    165
    166 	public void characters( char[] ch, int begin, int length )
    167 		throws SAXException
    168 	{
    169 		if( _itemHandler != null )	{
    170 			_itemHandler.characters( ch, begin, length );
    171 		}
    172 	}
    173
    174 	public void ignorableWhitespace( char[] ch, int begin, int length )
    175 		throws SAXException
    176 	{
    177 		if( _itemHandler != null )	{
    178 			_itemHandler.ignorableWhitespace( ch, begin, length );
    179 		}
    180 	}
    181 }