Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.asSyntax.directives;
  
  
  import java.io.File;
 import java.net.URL;
 import java.util.List;
Implementation of the include directive.
 
 public class Include implements Directive {
 
     static Logger logger = Logger.getLogger(Include.class.getName());
     public static final String CRPrefix = "ClassResource:";
 
     private List<StringaslSourcePath = null;
     
     public Agent process(Pred directiveAgent outerContentAgent innerContent) {
         if (outerContent == null)
             return null;
         String file = ((StringTerm)directive.getTerm(0)).getString().replaceAll("\\\\""/");
         try {
             InputStream in = null;
             // test include from jar
             if (file.startsWith("$")) { // the case of "$jasonJar/src/a.asl"
                 String jar = file.substring(1,file.indexOf("/"));
                 String path = Config.get().get(jar).toString();
                 file = "jar:file:" + path + "!" + file.substring(file.indexOf("/"));
                 in = new URL(file).openStream();
             } else {
                 String outerPrefix = outerContent.getASLSrc(); // the source file that has the include directive
                 if (outerContent != null && outerPrefix != null) {
                     // check if the outer is URL
                     if (outerPrefix.startsWith("jar")) {
                         outerPrefix = outerPrefix.substring(0,outerPrefix.indexOf("!")+1) + "/";
                         file = checkPathAndFixWithSourcePath(fileouterPrefix);
                         in = new URL(file).openStream();
                         
                     } if (outerPrefix.startsWith()) {
                         // outer is loaded from a resource ("application".jar) file, used for java web start
                         int posSlash = outerPrefix.lastIndexOf("/");
     
                         List<Stringnewpath = new ArrayList<String>();
                         if (outerPrefix.indexOf("/") != posSlash) { // has only one slash
                             newpath.add(outerPrefix.substring(.length()+1,posSlash));
                         }
                         newpath.addAll();
                         
                         file = checkPathAndFixWithSourcePath(filenewpath+"/");
                         in = Agent.class.getResource(file.substring(.length())).openStream();
                     } else {
                         // get the directory of the source of the outer agent and 
                         // try to find the included source in the same directory
                         // or in the source paths
                         List<Stringnewpath = new ArrayList<String>();
                         newpath.add(new File(outerPrefix).getAbsoluteFile().getParent());
                         if ( != null)
                             newpath.addAll();
                         file = checkPathAndFixWithSourcePath(filenewpathnull);
                         in = new FileInputStream(file);
                     }
                 } else {
                     in = new FileInputStream(checkPathAndFixWithSourcePath(filenull));         
                 }
             }
 
             Agent ag = new Agent();
             ag.initAg();
             ag.setASLSrc(file);
             as2j parser = new as2j(in); 
             parser.agent(ag);
             .fine("as2j: AgentSpeak program '"+file+"' parsed successfully!");
             return ag;
         } catch (FileNotFoundException e) {
             .log(.,"The included file '"+file+"' was not found! (it is being included in the agent '"+outerContent.getASLSrc()+"')");
         } catch (Exception e) {
             .log(.,"as2j: error parsing \"" + file + "\""e);
         }
         return null;
     }
 
     public void setSourcePath(List<Stringsp) {
          = sp;
     }
    
    
fix path of the asl code based on aslSourcePath, also considers code from a jar file (if urlPrefix is not null)
 
     public static String checkPathAndFixWithSourcePath(String fList<StringsrcpathString urlPrefix) {
         if (urlPrefix == null || urlPrefix.length() == 0) {
             if (new File(f).exists()) {
                return f;
            } else if (srcpath != null) {
                for (String pathsrcpath) {
                    try {
                        File newname = new File(path + "/" + f.toString());
                        if (newname.exists()) {
                            return newname.getCanonicalFile().toString();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            if (testURLSrc(urlPrefix + f)) {
                return urlPrefix + f;
            } else if (srcpath != null) {
                for (String pathsrcpath) {
                    String newname = urlPrefix + path + "/" + f;
                    newname = newname.replaceAll("\\./""");
                    if (testURLSrc(newname)) {
                        return newname;
                    }
                }
            }
        }
        return f;
    }
    
    private static boolean testURLSrc(String asSrc) {
        try {
            if (asSrc.startsWith()) {
                Agent.class.getResource(asSrc.substring(.length())).openStream();
                return true;
            } else {
                Agent.class.getResource(asSrc).openStream();
                return true;                
            }
        } catch (Exception e) {}
        return false;
    }    
New to GrepCode? Check out our FAQ X