+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2009 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// Import.java\r
-// Since: Feb 9, 2009 3:40:27 PM\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/plugin/Import.java $\r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.silk.plugin;\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.net.URL;\r
-\r
-import org.xerial.core.XerialErrorCode;\r
-import org.xerial.core.XerialException;\r
-import org.xerial.silk.SilkEnv;\r
-import org.xerial.silk.SilkParser;\r
-import org.xerial.util.FileType;\r
-import org.xerial.util.io.Base64OutputStream;\r
-import org.xerial.util.tree.TreeEventHandler;\r
-\r
-/**\r
- * <em>import</em> function\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class Import implements SilkFunctionPlugin\r
-{\r
-\r
- @SilkFunctionArgument\r
- String filePath = null;\r
-\r
- public void init(SilkEnv env) throws XerialException\r
- {\r
-\r
- }\r
-\r
- public void eval(SilkEnv env, TreeEventHandler handler) throws Exception\r
- {\r
- if (filePath == null)\r
- {\r
- env.getLogger().warn("no file path is specified");\r
- return;\r
- }\r
-\r
- try\r
- {\r
- String url = env.getResourceBasePath();\r
- if (!env.getResourceBasePath().endsWith("/"))\r
- url += "/";\r
- url += filePath;\r
-\r
- FileType f = FileType.getFileType(filePath);\r
- switch (f)\r
- {\r
- case SILK:\r
- case TAB:\r
- {\r
- SilkParser parser = new SilkParser(new URL(url), env);\r
- parser.parseWithoutInitAndFinish(handler);\r
- break;\r
- }\r
- case JPEG:\r
- case GIF:\r
- case BMP:\r
- case PDF:\r
- case PS:\r
- case TIFF:\r
- case WORD:\r
- case EXCEL:\r
- case POWER_POINT:\r
- case PNG:\r
- {\r
- loadBinary(new URL(url), env, handler);\r
- }\r
- break;\r
- default:\r
- {\r
- SilkParser parser = new SilkParser(new URL(url), env);\r
- parser.parseWithoutInitAndFinish(handler);\r
- break;\r
- }\r
- }\r
-\r
- }\r
- catch (IOException e)\r
- {\r
- throw new XerialException(XerialErrorCode.IO_EXCEPTION, e);\r
- }\r
-\r
- }\r
-\r
- public void loadBinary(URL path, SilkEnv env, TreeEventHandler handler) throws Exception\r
- {\r
- env.getLogger().debug("load binary: " + path);\r
-\r
- InputStream source = path.openStream();\r
- BufferedInputStream in = new BufferedInputStream(source);\r
-\r
- byte[] buffer = new byte[1024];\r
- int readBytes = 0;\r
- while ((readBytes = in.read(buffer, 0, buffer.length)) != -1)\r
- {\r
- ByteArrayOutputStream base64buffer = new ByteArrayOutputStream(readBytes);\r
- Base64OutputStream base64out = new Base64OutputStream(base64buffer);\r
- base64out.write(buffer, 0, readBytes);\r
- base64out.flush();\r
- String[] fragment = new String(base64buffer.toByteArray()).split("\\r\\n");\r
- for (String each : fragment)\r
- {\r
- handler.text(env.getContextNode().getName(), each);\r
- }\r
- }\r
-\r
- }\r
-\r
-}\r