src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java

Print this page




 467     }
 468 
 469     /**
 470      * Constructor
 471      *
 472      * @param name  source name
 473      * @param file  file from which source can be loaded
 474      *
 475      * @return source instance
 476      *
 477      * @throws IOException if source cannot be loaded
 478      */
 479     public static Source sourceFor(final String name, final File file) throws IOException {
 480         return sourceFor(name, file, null);
 481     }
 482 
 483     /**
 484      * Constructor
 485      *
 486      * @param name  source name
























 487      * @param file  file from which source can be loaded
 488      * @param cs    Charset used to convert bytes to chars
 489      *
 490      * @return source instance
 491      *
 492      * @throws IOException if source cannot be loaded
 493      */
 494     public static Source sourceFor(final String name, final File file, final Charset cs) throws IOException {
 495         final File absFile = file.getAbsoluteFile();
 496         return sourceFor(name, dirName(absFile, null), new FileData(file, cs));
 497     }
 498 
 499     /**
 500      * Returns an instance
 501      *
 502      * @param name source name
 503      * @param reader reader from which source can be loaded
 504      *
 505      * @return source instance
 506      *


 584      * @return Source content portion.
 585      */
 586     public String getString(final long token) {
 587         final int start = Token.descPosition(token);
 588         final int len = Token.descLength(token);
 589         return new String(data(), start, len);
 590     }
 591 
 592     /**
 593      * Returns the source URL of this script Source. Can be null if Source
 594      * was created from a String or a char[].
 595      *
 596      * @return URL source or null
 597      */
 598     public URL getURL() {
 599         return data.url();
 600     }
 601 
 602     /**
 603      * Get explicit source URL.
 604      * @return URL set vial sourceURL directive
 605      */
 606     public String getExplicitURL() {
 607         return explicitURL;
 608     }
 609 
 610     /**
 611      * Set explicit source URL.
 612      * @param explicitURL URL set via sourceURL directive
 613      */
 614     public void setExplicitURL(final String explicitURL) {
 615         this.explicitURL = explicitURL;
 616     }
 617 
 618     /**
 619      * Returns whether this source was submitted via 'eval' call or not.
 620      *
 621      * @return true if this source represents code submitted via 'eval'
 622      */
 623     public boolean isEvalCode() {
 624         return data.isEvalCode();




 467     }
 468 
 469     /**
 470      * Constructor
 471      *
 472      * @param name  source name
 473      * @param file  file from which source can be loaded
 474      *
 475      * @return source instance
 476      *
 477      * @throws IOException if source cannot be loaded
 478      */
 479     public static Source sourceFor(final String name, final File file) throws IOException {
 480         return sourceFor(name, file, null);
 481     }
 482 
 483     /**
 484      * Constructor
 485      *
 486      * @param name  source name
 487      * @param path  path from which source can be loaded
 488      *
 489      * @return source instance
 490      *
 491      * @throws IOException if source cannot be loaded
 492      */
 493     public static Source sourceFor(final String name, final Path path) throws IOException {
 494         File file = null;
 495         try {
 496             file = path.toFile();
 497         } catch (final UnsupportedOperationException uoe) {
 498         } 
 499         
 500         if (file != null) {
 501             return sourceFor(name, file);
 502         } else {
 503             return sourceFor(name, Files.newBufferedReader(path));
 504         }
 505     }
 506     
 507     /**
 508      * Constructor
 509      *
 510      * @param name  source name
 511      * @param file  file from which source can be loaded
 512      * @param cs    Charset used to convert bytes to chars
 513      *
 514      * @return source instance
 515      *
 516      * @throws IOException if source cannot be loaded
 517      */
 518     public static Source sourceFor(final String name, final File file, final Charset cs) throws IOException {
 519         final File absFile = file.getAbsoluteFile();
 520         return sourceFor(name, dirName(absFile, null), new FileData(file, cs));
 521     }
 522 
 523     /**
 524      * Returns an instance
 525      *
 526      * @param name source name
 527      * @param reader reader from which source can be loaded
 528      *
 529      * @return source instance
 530      *


 608      * @return Source content portion.
 609      */
 610     public String getString(final long token) {
 611         final int start = Token.descPosition(token);
 612         final int len = Token.descLength(token);
 613         return new String(data(), start, len);
 614     }
 615 
 616     /**
 617      * Returns the source URL of this script Source. Can be null if Source
 618      * was created from a String or a char[].
 619      *
 620      * @return URL source or null
 621      */
 622     public URL getURL() {
 623         return data.url();
 624     }
 625 
 626     /**
 627      * Get explicit source URL.
 628      * @return URL set via sourceURL directive
 629      */
 630     public String getExplicitURL() {
 631         return explicitURL;
 632     }
 633 
 634     /**
 635      * Set explicit source URL.
 636      * @param explicitURL URL set via sourceURL directive
 637      */
 638     public void setExplicitURL(final String explicitURL) {
 639         this.explicitURL = explicitURL;
 640     }
 641 
 642     /**
 643      * Returns whether this source was submitted via 'eval' call or not.
 644      *
 645      * @return true if this source represents code submitted via 'eval'
 646      */
 647     public boolean isEvalCode() {
 648         return data.isEvalCode();