📄 typeconversionparser.java
字号:
package org.jawin.browser.codegen;
import org.xml.sax.ext.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import org.jawin.browser.config.ConfigManager;
import org.jawin.browser.log.Log;
/**
* SAX parse out the type conversion config file
*
* <p>Title: Jawin Code Generation GUI</p>
* <p>Description: GUI for exploring type libraries and generating Java code</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Open Source Incentive</p>
*
* @author Josh Passenger
* @version 1.0
*/
public class TypeConversionParser extends DefaultHandler
{
/**
* The Sax parser class to use
*/
private String parserName = ConfigManager.getInstance().getString("sax.parser", "org.apache.xerces.parsers.SAXParser");
/**
* The parser that will actually pasre the document
*/
private XMLReader xmlReader = null;
/**
* The current config item
*/
private TypeConversionConfig currentConfig = null;
public TypeConversionParser()
{
try
{
xmlReader = XMLReaderFactory.createXMLReader(parserName);
xmlReader.setContentHandler(this);
}
catch (Exception e)
{
Log.getInstance().exception("TypeConversionParser() failed to create SAX parser, no type conversion config is available", e);
}
}
/**
* @todo implement config file parsing and throw SaxException
*
* @param typeConversionConfigFile
*/
public void parseCodeGenConfig(String typeConversionConfigFile)
{
FileInputStream fileIn = null;
try
{
fileIn = new FileInputStream(typeConversionConfigFile);
InputSource in = new InputSource(fileIn);
xmlReader.parse(in);
}
catch (Exception e)
{
Log.getInstance().exception("TypeConversionParser.parseTypeConversionConfig() failed to parse type conversion config file: " + typeConversionConfigFile, e);
}
finally
{
if (fileIn != null)
{
try
{
fileIn.close();
}
catch (Exception e)
{
}
}
}
}
/**
* Fired when the start of a doucment is encountered
*
* @throws SAXException thrown if something goes wrong
*/
public void startDocument() throws SAXException
{
TypeConversionManager.getInstance().clearConfig();
currentConfig = null;
}
/**
* Fired when an element is started
*
* @param uri
* @param localName
* @param qualifiedName
* @param attributes
* @throws SAXException
*/
public void startElement(String uri, String localName, String qualifiedName, Attributes attributes) throws SAXException
{
if (qualifiedName.equals("typeLibraryType"))
{
String id = getAttribute("id", attributes);
String string = getAttribute("string", attributes);
String pointer = getAttribute("pointer", attributes);
currentConfig = new TypeConversionConfig(id, string, pointer);
TypeConversionManager.getInstance().addTypeConversionConfig(currentConfig);
return;
}
if (qualifiedName.equals("javaType"))
{
String string = getAttribute("string", attributes);
String objectType = getAttribute("objectType", attributes);
String signature = getAttribute("signature", attributes);
String nativeString = getAttribute("nativeString", attributes);
String nativeArray = getAttribute("nativeArray", attributes);
JavaType javaType = new JavaType(string, objectType, signature, nativeString, nativeArray);
if (currentConfig != null)
{
currentConfig.setJavaType(javaType);
}
return;
}
if (qualifiedName.equals("cppType"))
{
String string = getAttribute("string", attributes);
String direct = getAttribute("direct", attributes);
String pointer = getAttribute("pointer", attributes);
CPlusPlusType cppType = new CPlusPlusType(string, direct, pointer);
if (currentConfig != null)
{
currentConfig.setCPlusPlusType(cppType);
}
return;
}
}
/**
* Fetches the value of a required attribute, some attributes may be empty Strings
* but all shoudl be specified in the config file
*
* @param name the name of the attribute to fetch
* @param attributes the attributes on the node
* @return the value of the named attribute
* @throws SAXException thrown if the attribute does not exist
*/
private String getAttribute(String name, Attributes attributes) throws SAXException
{
String value = attributes.getValue(name);
if (value == null)
{
throw new SAXException("TypeConversionParser.getAttribute() a required attribute was missing: " + name);
}
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -