📄 projectparser.java
字号:
package org.jawin.browser.project;
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;
/**
* Parses Project items from a project 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 ProjectParser 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;
private Project project = null;
public ProjectParser()
{
try
{
xmlReader = XMLReaderFactory.createXMLReader(parserName);
xmlReader.setContentHandler(this);
}
catch (Exception e)
{
Log.getInstance().exception("ProjectParser() failed to create SAX parser, no project file was loaded", e);
}
}
/**
* Parses a project file on disk
*
* @param projectFile
*/
public Project parseProjectFile(String projectFile)
{
FileInputStream fileIn = null;
try
{
fileIn = new FileInputStream(projectFile);
InputSource in = new InputSource(fileIn);
xmlReader.parse(in);
}
catch (Exception e)
{
Log.getInstance().exception("ProjectParser.parseProjectFile() failed to parse project: " + projectFile, e);
}
finally
{
if (fileIn != null)
{
try
{
fileIn.close();
}
catch (Exception e)
{
}
}
}
return project;
}
/**
* Fired when the start of a document is encountered
*
* @throws SAXException thrown if something goes wrong
*/
public void startDocument() throws SAXException
{
project = 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("project"))
{
String projectFile = getAttribute("projectFile", attributes);
project = new Project(projectFile);
return;
}
if (qualifiedName.equals("projectItem"))
{
String name = attributes.getValue("name");
String libraryFile = getAttribute("libraryFile", attributes);
String packageName = attributes.getValue("package");
String saveDirectory = attributes.getValue("saveDirectory");
String javaEncodingDirectory = attributes.getValue("javaEncoding");
ProjectItem projectItem = new ProjectItem(libraryFile, name, packageName, saveDirectory, javaEncodingDirectory);
project.addProjectItem(projectItem);
return;
}
}
public void endElement(String uri, String localName, String qualifiedName) throws SAXException
{
}
/**
* Fetches the value of a required attribute
*
* @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 || value.equals(""))
{
throw new SAXException("ProjectParser.getAttribute() a required attribute was missing: " + name);
}
return value;
}
public void characters(char [] chars, int start, int length) throws SAXException
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -