|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package org.scijava.script.process; |
| 34 | + |
| 35 | +import java.util.LinkedHashMap; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +import org.scijava.log.LogService; |
| 39 | +import org.scijava.plugin.Parameter; |
| 40 | +import org.scijava.plugin.Plugin; |
| 41 | +import org.scijava.script.ScriptImport; |
| 42 | + |
| 43 | +/** |
| 44 | + * A {@link ScriptProcessor} which parses the {@code #@import} directive. |
| 45 | + * <p> |
| 46 | + * The syntax is: |
| 47 | + * </p> |
| 48 | + * |
| 49 | + * <pre> |
| 50 | + * #@import("myutils", scope="util") |
| 51 | + * #@import("utils") |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * @author Jan Eglinger |
| 55 | + */ |
| 56 | +@Plugin(type = ScriptProcessor.class) |
| 57 | +public class ImportDirectiveScriptProcessor extends DirectiveScriptProcessor { |
| 58 | + |
| 59 | + public ImportDirectiveScriptProcessor() { |
| 60 | + super(directive -> "import".equals(directive)); |
| 61 | + } |
| 62 | + |
| 63 | + @Parameter |
| 64 | + private LogService log; |
| 65 | + |
| 66 | + // -- Internal DirectiveScriptProcessor methods -- |
| 67 | + |
| 68 | + @Override |
| 69 | + protected String process(final String directive, final Map<String, Object> attrs, final String theRest) { |
| 70 | + String importItem = null; |
| 71 | + String scope = null; |
| 72 | + for (final String k : attrs.keySet()) { |
| 73 | + if (k == null || is(k, "name")) |
| 74 | + importItem = as(attrs.get(k), String.class); |
| 75 | + else if (is(k, "scope")) |
| 76 | + scope = as(attrs.get(k), String.class); |
| 77 | + else |
| 78 | + throw new IllegalArgumentException("Wrong import argument"); |
| 79 | + } |
| 80 | + if (importItem != null) |
| 81 | + addImport(importItem, scope); |
| 82 | + else |
| 83 | + throw new IllegalArgumentException("No import name provided"); |
| 84 | + return ""; |
| 85 | + } |
| 86 | + |
| 87 | + // -- Helper methods -- |
| 88 | + |
| 89 | + private void addImport(String importItem, String scope) { |
| 90 | + @SuppressWarnings("unchecked") |
| 91 | + Map<String, ScriptImport> map = (Map<String, ScriptImport>) info().getProperty("imports"); |
| 92 | + if (map==null) map = new LinkedHashMap<>(); |
| 93 | + map.put(importItem, new ScriptImport(scope)); |
| 94 | + info().setProperty("imports", map); |
| 95 | + } |
| 96 | +} |
0 commit comments