📄 jobinstructionshandler.java
字号:
package fi.javasom.jsom;
/**
* This is the base handler for gui properties.
*
* Copyright (C) 2001 Tomi Suuronen
*
* @version 1.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import fi.javasom.jsom.*;
import java.io.*;
import java.util.*;
public class JobInstructionsHandler extends DefaultHandler
{
private Hashtable rules = new Hashtable();
private Stack stack = new Stack();
private WeightVectors reference;
private JsomMap map;
private JSomTraining job;
//input element
private boolean input;
private SAXParser parser;
private JsomHandler handler2;
private InputVectors inputVectors;
//initialization element
private boolean init;
private String lattice;
private int xDim;
private int yDim;
private boolean normalization;
private JSomNormalization norm;
private String neighbourhood;
//ordering element
private boolean training;
private int steps;
private double lrate;
private int radius;
private String lrateType;
//output element
private boolean output;
private JSomLabeling labels;
private File folder;
private String fileName; //name of the file to be created
private String paper; //the size of a paper for pdf
private String orientation; //orientation of a paper for pdf
private boolean xml = false;
private boolean svg = false;
private boolean pdf = false;
/**
* Constructor.
*/
public JobInstructionsHandler()
{
super();
//input element
input = false;
inputVectors = new InputVectors();
//initialization element
init = false;
lattice = "";
xDim = 1;
yDim = 1;
normalization = true;
neighbourhood = "";
//ordering element
training = false;
//output element
output = false;
folder = null;
setElementHandler("input", new InputHandler());
setElementHandler("file", new DataFileHandler());
setElementHandler("initialization", new InitializationHandler());
setElementHandler("normalization", new NormalizationHandler());
setElementHandler("x.dimension", new XDimensionHandler());
setElementHandler("y.dimension", new YDimensionHandler());
setElementHandler("lattice", new LatticeHandler());
setElementHandler("neighbourhood", new NeighbourhoodHandler());
setElementHandler("training", new TrainingHandler());
setElementHandler("steps", new StepsHandler());
setElementHandler("lrate", new LrateHandler());
setElementHandler("radius", new RadiusHandler());
setElementHandler("output", new OutputHandler());
setElementHandler("folder", new FolderHandler());
setElementHandler("identifier", new IdentifierHandler());
setElementHandler("type", new TypeHandler());
}
/*
* Define processing for an element type.
*/
private void setElementHandler(String name, ElementHandler handler)
{
rules.put(name, handler);
}
/*
* Start of an element. Decide what handler to use, and call it.
*/
public void startElement(String namespaceURI,String name,String qName,Attributes atts) throws SAXException
{
ElementHandler handler = (ElementHandler)rules.get(name);
stack.push(handler);
if (handler!=null)
{
handler.startElement(namespaceURI,name,qName,atts);
}
}
/*
* End of an element.
*/
public void endElement(String uri,String name,String qName) throws SAXException
{
ElementHandler handler = (ElementHandler)stack.pop();
if (handler!=null)
{
handler.endElement(uri,name,qName);
}
}
/*
* Character data.
*/
public void characters(char[] chars,int start,int len) throws SAXException
{
ElementHandler handler = (ElementHandler)stack.peek();
if (handler!=null)
{
handler.characters(chars,start,len);
}
}
/*
* End of document.
*/
public void endDocument() throws SAXException
{
}
/*********************************************************************************/
/*
* Handles the input element events.
*/
private class InputHandler extends ElementHandler
{
/**
* Start of an element.
*/
public void startElement (String namespaceURI,String name,String qName,Attributes atts) throws SAXException
{
input = true;
}
/**
* End of an element.
*/
public void endElement (String uri,String name,String qName) throws SAXException
{
input = false;
}
}
/*********************************************************************************/
/*
* Handles the file element events.
*/
private class DataFileHandler extends ElementHandler
{
/**
* Character data.
*/
public void characters (char[] chars,int start,int len) throws SAXException
{
File file = new File(String.valueOf(chars,start,len));
if(file.isFile())
{
if(input)
{
parser = new org.apache.xerces.parsers.SAXParser();
handler2 = new JsomHandler();
parser.setContentHandler(handler2);
parser.setErrorHandler(handler2);
try
{
Reader reader = new BufferedReader(new FileReader(file));
parser.parse(new InputSource(reader));
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
inputVectors = handler2.getInputVectors();
map = handler2.getJsomMap();
}
}
}
}
/*********************************************************************************/
/*
* Handles the initialization element events.
*/
private class InitializationHandler extends ElementHandler
{
/**
* Start of an element.
*/
public void startElement (String namespaceURI,String name,String qName,Attributes atts) throws SAXException
{
init = true;
}
/**
* End of an element.
*/
public void endElement (String uri,String name,String qName) throws SAXException
{
init = false;
reference = new WeightVectors(xDim,yDim,handler2.getNodeValueCount(),lattice);
}
}
/*********************************************************************************/
/*
* Handles the normalization element events.
*/
private class NormalizationHandler extends ElementHandler
{
/**
* Start of an element.
*/
public void startElement (String namespaceURI,String name,String qName,Attributes atts) throws SAXException
{
if(init)
{
normalization = (new Boolean(atts.getValue(0))).booleanValue();
if(normalization)
{
norm = new JSomNormalization(inputVectors,handler2.getNodeValueCount());
inputVectors = norm.doNormalization();
}
}
}
}
/*********************************************************************************/
/*
* Handles the x-dimension element events.
*/
private class XDimensionHandler extends ElementHandler
{
/**
* Character data.
*/
public void characters (char[] chars,int start,int len) throws SAXException
{
if(init)
{
try
{
xDim = (Integer.valueOf(String.valueOf(chars,start,len).toLowerCase())).intValue();
}
catch(NumberFormatException nfe)
{
System.out.println(nfe.getMessage());
}
}
}
}
/*********************************************************************************/
/*
* Handles the y-dimension element events.
*/
private class YDimensionHandler extends ElementHandler
{
/**
* Character data.
*/
public void characters (char[] chars,int start,int len) throws SAXException
{
if(init)
{
try
{
yDim = (Integer.valueOf(String.valueOf(chars,start,len).toLowerCase())).intValue();
}
catch(NumberFormatException nfe)
{
System.out.println(nfe.getMessage());
}
}
}
}
/*********************************************************************************/
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -