View Javadoc

    1   /*
    2    * "Peko" Visual Novel System
    3    *
    4    * All Rights Reserved.
    5    * Copyright (c) 1999-2003 Tsukuba Bunko.
    6    *
    7    * $Id: HeadHandler.java,v 1.1 2005/07/11 12:49:19 ppoi Exp $
    8    */
    9   package tsukuba_bunko.peko.scenario.structure;
    10
    11  import	java.io.InputStream;
    12
    13  import	java.net.URL;
    14
    15  import	java.util.Iterator;
    16  import	java.util.Properties;
    17
    18  import	org.xml.sax.Attributes;
    19
    20  import	tsukuba_bunko.peko.Logger;
    21  import	tsukuba_bunko.peko.PekoSystem;
    22
    23  import	tsukuba_bunko.peko.resource.ResourceManager;
    24
    25
    26  import tsukuba_bunko.peko.scenario.ElementHandler;
    27  import	tsukuba_bunko.peko.scenario.NextSceneMapping;
    28  import tsukuba_bunko.peko.scenario.PSMLUtil;
    29  import tsukuba_bunko.peko.scenario.SceneContext;
    30
    31
    32  /***
    33   * <samp>head</samp> 要素を処理する ElementHandler です。
    34   * @author	$Author: ppoi $
    35   * @version	$Revision: 1.1 $
    36   */
    37  public class HeadHandler extends ElementHandler {
    38
    39  	/***
    40  	 * 文字列キャッシュ
    41  	 */
    42  	protected StringBuffer	_text = null;
    43
    44  	/***
    45  	 * 遷移先表
    46  	 */
    47  	protected NextSceneMapping	_nextSceneMapping = null;
    48
    49  	/***
    50  	 */
    51  	protected String	_condition = null;
    52
    53
    54  	/***
    55  	 * <code>HeadHandler</code> のインスタンスを生成します。
    56  	 */
    57  	public HeadHandler()
    58  	{
    59  		super();
    60  	}
    61
    62
    63  //
    64  //	ElementHandler の実装
    65  //
    66  	public void endDocument()
    67  	{
    68  		SceneContext	context = getSceneContext();
    69  		if( context.getSceneTitle() == null )	{
    70  			Logger.warn( MessageIDs.SCN5001W );
    71  			context.setSceneTitle( context.getSceneName() );
    72  		}
    73  		ResourceManager	resources = ResourceManager.getInstance();
    74  		PekoSystem.getInstance().getMainWindow().setTitle( context.getSceneTitle() + " - " + resources.getResource("game-info.title") );
    75  	}
    76
    77  	public void startElement( String namespaceURI, String localName, String qName, Attributes attrs )
    78  	{
    79  		if( localName.equals("property") )	{
    80  			String	name = PSMLUtil.getAttributeValue( attrs, "name" );
    81  			String	file = PSMLUtil.getAttributeValue( attrs, "file" );
    82  			if( (name != null) && (name.length() > 0) )	{
    83  				getSceneContext().setProperty( name, PSMLUtil.getAttributeValue(attrs, "value") );
    84  			}
    85  			else if( (file != null) && (file.length() > 0) )	{
    86  				ResourceManager	resources = ResourceManager.getInstance();
    87  				URL	sceneDir = resources.getLocationResources().getScenesDirecotryURL();
    88  				URL	fileURL = null;
    89  				try	{
    90  					fileURL = new URL( sceneDir, file );
    91  				}
    92  				catch( Exception e )	{
    93  					Logger.warn( MessageIDs.SCN5002W, new Object[]{getSceneContext().getCurrentPath()}, e );
    94  					return;
    95  				}
    96
    97  				Properties	properties = new Properties();
    98  				InputStream	is = null;
    99  				try	{
    100 					is = fileURL.openStream();
    101 					properties.load( is );
    102 				}
    103 				catch( Exception e )	{
    104 					Logger.warn( MessageIDs.SCN5002W, new Object[]{getSceneContext().getCurrentPath()}, e );
    105 					return;
    106 				}
    107 				finally	{
    108 					if( is != null )	{
    109 						try	{
    110 							is.close();
    111 						}
    112 						catch( Exception e )	{
    113 						}
    114 					}
    115 				}
    116
    117 				SceneContext	context = getSceneContext();
    118 				Iterator	itr = properties.keySet().iterator();
    119 				String	key = null;
    120 				while( itr.hasNext() )	{
    121 					key = (String)itr.next();
    122 					context.setProperty( key, properties.getProperty(key) );
    123 				}
    124 			}
    125 		}
    126 		else if( "next-scene".equals(localName) )	{
    127 			_nextSceneMapping = getSceneContext().getNextSceneMapping();
    128 		}
    129 		else if( localName.equals("title") )	{
    130 			_text = new StringBuffer();
    131 		}
    132 		else if( localName.equals("scene-ref") )	{
    133 			_text = new StringBuffer();
    134 			_condition = PSMLUtil.getAttributeValue( attrs, "if" );
    135 			if( _condition != null )	{
    136 				_condition = _condition.trim();
    137 			}
    138 		}
    139 	}
    140
    141 	public void endElement( String namespaceURI, String localName, String qName )
    142 	{
    143 		if( localName.equals("title") )	{
    144 			if( _text.length() == 0 )	{
    145 				Logger.warn( MessageIDs.SCN5001W );
    146 				getSceneContext().setSceneTitle( getSceneContext().getSceneName() );
    147 			}
    148 			else	{
    149 				getSceneContext().setSceneTitle( new String(_text) );
    150 			}
    151 		}
    152 		else if( localName.equals("scene-ref") )	{
    153 			if( _text.length() > 0 )	{
    154 				NextSceneMapping	mapping = getSceneContext().getNextSceneMapping();
    155 				String	sceneName = new String(_text).trim();
    156 				if( (_condition == null) || (_condition.length() == 0) )	{
    157 					mapping.setDefaultSceneMapping( sceneName );
    158 				}
    159 				else	{
    160 					mapping.addNextSceneMapping( _condition, sceneName );
    161 				}
    162 				_condition = null;
    163 			}
    164 		}
    165 		else if( localName.equals("next-scene") )	{
    166 			_nextSceneMapping = null;
    167 		}
    168 		_text = null;
    169 	}
    170
    171 	public void characters( char[] ch, int begin, int length )
    172 	{
    173 		if( _text != null )	{
    174 			_text.append( ch, begin, length );
    175 		}
    176 	}
    177 }