View Javadoc

1   /*
2    * "Peko" Visual Novel System
3    *
4    * All Rights Reserved.
5    * Copyright (c) 1999-2003 Tsukuba Bunko.
6    *
7    * $Id: SelectHandler.java,v 1.2 2005/07/12 10:00:36 ppoi Exp $
8    */
9   package tsukuba_bunko.peko.scenario.select;
10  
11  import	java.util.Map;
12  
13  import	org.xml.sax.Attributes;
14  import	org.xml.sax.SAXException;
15  
16  import	tsukuba_bunko.peko.Logger;
17  
18  import	tsukuba_bunko.peko.scenario.ElementHandler;
19  import	tsukuba_bunko.peko.scenario.FlagScope;
20  import	tsukuba_bunko.peko.scenario.PSMLUtil;
21  
22  
23  /***
24   * <samp>select</samp> 要素を処理する <code>ElementHandler</code> です。
25   * @author	$Author: ppoi $
26   * @version	$Revision: 1.2 $
27   */
28  public class SelectHandler	extends ElementHandler	{
29  
30  	/***
31  	 * id - 属性値リスト
32  	 */
33  	private Map	_attributes = new java.util.HashMap( 89 );
34  
35  	/***
36  	 * 現在パース中の選択肢 ID
37  	 */
38  	private String	_id = null;
39  
40  	/***
41  	 * テキストバッファ
42  	 */
43  	private StringBuffer	_text = null;
44  
45  	/***
46  	 * 選択肢名
47  	 */
48  	private String	_name = null;
49  
50  	/***
51  	 * スコープ
52  	 */
53  	private FlagScope	_scope = null;
54  
55  
56  	/***
57  	 * <code>SelectHandler</code> のインスタンスを作成します。
58  	 */
59  	public SelectHandler()
60  	{
61  		super();
62  	}
63  
64  
65  	/***
66  	 * SelectCanvas コーディネータを取得します。
67  	 * @return	SelectCanvas コーディネータ
68  	 */
69  	protected SelectCoordinator getSelectCoordinator()
70  	{
71  		return getSceneContext().getSceneProcessor().getSelectCoordinator();
72  	}
73  
74  
75  //
76  //	ContentHandler の実装
77  //
78  	public void startDocument()
79  	{
80  		_attributes.clear();
81  		setEndOfScene( false );
82  	}
83  
84  	public void endDocument()
85  	{
86  		SelectCoordinator	coordinator = getSelectCoordinator();
87  		
88  		String	id = coordinator.select();
89  		if( id == null )	{
90  			//	キャンセルされた
91  			return;
92  		}
93  		else	{
94  			getSceneContext().declareFlag( _name + ":" + id, _scope );
95  		}
96  
97  		coordinator.end();
98  	}
99  
100 	public void startElement( String namespaceURI, String localName, String qName, Attributes attrs )
101 		throws SAXException
102 	{
103 		if( localName.equals("item") )	{
104 			if( _id != null )	{
105 				Logger.error( "[scenario.select] BUG! Invalid PSML Structure. location: " + getSceneContext().getCurrentPath() );
106 				throw new SAXException( "Invalid PSML Structure." );
107 			}
108 
109 			String	id = PSMLUtil.getAttributeValue( attrs, "id" );
110 			if( (id == null) || (id.length() == 0) )	{
111 				Logger.warn( MessageIDs.SCN4002W, new Object[]{getSceneContext().getCurrentPath()} );
112 				_id = null;
113 			}
114 			else	{
115 				_id = id;
116 				_text = new StringBuffer();
117 			}
118 		}
119 		else if( "select".equals(localName) )	{
120 			String	name = PSMLUtil.getAttributeValue( attrs, "name" );
121 			if( (name == null) || (name.length() == 0) )	{
122 				Logger.error( MessageIDs.SCN4001E, new Object[]{getSceneContext().getCurrentPath()} );
123 				throw new SAXException( "invalid select element appeared." );
124 			}
125 			_name = PSMLUtil.getAttributeValue( attrs, "name" );
126 
127 			String	flagScope = PSMLUtil.getAttributeValue( attrs, "scope" );
128 			if( (flagScope == null) || flagScope.equals("session") )	{
129 				_scope = FlagScope.SESSION;
130 			}
131 			else if( flagScope.equals("scene") )	{
132 				_scope = FlagScope.SCENE;
133 			}
134 			else if( flagScope.equals("system") )	{
135 				_scope = FlagScope.SYSTEM;
136 			}
137 			else	{
138 				Logger.warn( MessageIDs.SCN4004W, new Object[]{"session", getSceneContext().getCurrentPath()} );
139 				_scope = FlagScope.SESSION;
140 			}
141 
142 			getSelectCoordinator().begin();
143 		}
144 	}
145 
146 	public void endElement( String namespaceURI, String localName, String qName )
147 	{
148 		if( (_id != null) && localName.equals("item") )	{
149 			if( _text.length() == 0 )	{
150 				Logger.warn( MessageIDs.SCN4003W, new Object[]{getSceneContext().getCurrentPath()} );
151 			}
152 			else	{
153 				SelectCoordinator	coordinator = getSceneContext().getSceneProcessor().getSelectCoordinator();
154 				coordinator.addSelectItem( _id, new String(_text) );
155 			}
156 			_id = null;
157 			_text = null;
158 		}
159 	}
160 
161 	public void characters( char[] ch, int begin, int length )
162 	{
163 		if( _text != null )	{
164 			_text.append( ch, begin, length );
165 		}
166 	}
167 }