📄 getversiontask.java
字号:
/*
* Copyright (c) 2003, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.flows.updateflowfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import net.orthanc.flow4j.runtime.AbstractTaskFlowlet;
import net.orthanc.flow4j.runtime.Flow4JRuntimeException;
import net.orthanc.flow4j.runtime.FlowDictionary;
import net.orthanc.flow4j.runtime.FlowManager;
import net.orthanc.flow4j.runtime.descriptors.TaskPropertyDescriptors;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author agreif
*
* This tasl puts the flow file's version in the dictionary.
*/
public class GetVersionTask extends AbstractTaskFlowlet {
// private static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
/**
* @see net.orthanc.flow4j.runtime.ITaskFlowlet#getTaskName()
*/
public String getName() {
return "GetVersion";
}
/**
* @see net.orthanc.flow4j.runtime.ITaskFlowlet#getDescription()
*/
public String getDescription() {
return "Puts the flow file's version in the dictionary";
}
/*
* TODO
* @see net.orthanc.flow4j.runtime.ITaskFlowlet#getPropertyDescriptors()
*/
public TaskPropertyDescriptors getPropertyDescriptors() {
return null;
}
/**
* Puts the flow file's version in the dictionary
* @see net.orthanc.flow4j.runtime.IFlowlet#execute(net.orthanc.flow4j.runtime.FlowDictionary)
*/
public void execute(FlowDictionary dictionary) {
File flowFile = (File)dictionary.get(UpdateFlowFileConsts.KEY_FLOW_FILE);
if (flowFile == null)
throw new Flow4JRuntimeException("Flow file not specified");
if (!flowFile.exists())
throw new Flow4JRuntimeException("specified flow file \"" + flowFile + "\" does not exist");
/*try {
parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
} catch (SAXException e) {
throw new Flow4JRuntimeException("Unable to instantiate parser (" + DEFAULT_PARSER_NAME + ")", e);
}*/
SAXHandler handler = new SAXHandler();
Reader flowFileReader = null;
try {
XMLReader parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
parser.setContentHandler(handler);
flowFileReader = new BufferedReader(new FileReader(flowFile));
InputSource source = new InputSource(flowFileReader);
parser.parse(source);
dictionary.put(UpdateFlowFileConsts.KEY_VERSION, new Integer(handler.getVersion()));
} catch (FileNotFoundException e1) {
// already checked
} catch (IOException e1) {
throw new Flow4JRuntimeException(e1);
} catch (SAXException e1) {
throw new Flow4JRuntimeException(e1);
} catch (ParserConfigurationException e1) {
throw new Flow4JRuntimeException(e1);
} finally {
if (flowFileReader != null)
try {
flowFileReader.close();
} catch (Exception e) {
}
}
}
/**
* @author agreif
*
* TODO
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
private class SAXHandler extends DefaultHandler {
private boolean foundVersion = false;
private String version;
/**
* TODO
* @return
*/
public int getVersion() {
return Integer.parseInt(version);
}
/**
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (foundVersion)
return;
String version = attributes.getValue("version");
if (version == null)
return;
this.version = version;
foundVersion = true;
}
}
/**
* For testing purposes
* @param args
*/
public static void main(String[] args) {
FlowManager.registerFlow(UpdateFlowFileFlow.class);
FlowDictionary dictionary = new FlowDictionary();
dictionary.put(UpdateFlowFileConsts.KEY_FLOW_FILE, new File("/Volumes/data/tmp/UpdateFlowFile.f4j"));
FlowManager.executeFlow("UpdateFlowFile", "Start", dictionary);
System.out.println(dictionary);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -