1 24 25 26 package org.netbeans.modules.javadoc.httpfs; 27 28 import java.io.*; 29 import java.net.URL ; 30 import java.net.HttpURLConnection ; 31 import java.util.*; 32 import javax.swing.text.html.*; 33 import javax.swing.text.BadLocationException ; 34 35 import org.openide.filesystems.*; 36 import org.openide.util.NbBundle; 37 38 39 44 class HTTPFileObject extends FileObject { 45 46 private static final long serialVersionUID = 200104; 47 48 transient HTTPFileSystem parentFileSystem; 50 String uriStem; 52 transient private HTTPFileObject parentFileObject; 54 transient private Map childFileObjects; 56 transient URL fileURL; 58 transient private String fullFileName; 60 transient private String fileName; 62 transient private String fileExtension; 64 transient private boolean wasFileHeaderRead; 66 transient private long fileSize; 68 transient private String fileMIMEType; 70 transient private Date fileDate; 72 transient private Hashtable fileAttributes; 74 transient private boolean areFolderContentsKnown; 76 transient private Vector listeners; 78 79 88 HTTPFileObject( String uriStem, HTTPFileSystem parentFileSystem ) { 89 90 initialize( uriStem, parentFileSystem ); 91 92 } 93 94 95 101 protected HTTPFileObject( 102 ) { 103 104 } 105 106 107 115 private void initialize( 116 String uriStem, 117 HTTPFileSystem parentFileSystem 118 ) { 119 120 try { 121 122 this.parentFileSystem = parentFileSystem; 124 this.parentFileObject = null; 125 this.childFileObjects = Collections.synchronizedMap( new Hashtable( ) ); 126 this.uriStem = uriStem; 127 this.fileURL = new java.net.URL ( parentFileSystem.baseURL, "." + uriStem ); this.fileAttributes = new Hashtable( 0 ); 129 this.areFolderContentsKnown = true; 130 this.listeners = new Vector( ); 131 this.fullFileName = ""; this.fileName = ""; this.fileExtension = ""; 135 136 if( isFolder( ) ) { 138 139 this.wasFileHeaderRead = true; 141 142 this.fullFileName = uriStem.substring( 0, uriStem.length( ) - 1 ); 144 145 } else { 147 148 this.wasFileHeaderRead = false; 150 this.fileSize = -1; 151 this.fileMIMEType = ""; this.fileDate = new Date( ); 153 this.fullFileName = uriStem; 154 155 } 156 this.fullFileName = this.fullFileName.substring( this.fullFileName.lastIndexOf( '/' ) + 1 ); 158 159 if( this.fullFileName.lastIndexOf( '.' ) != -1 ) { 161 162 this.fileName = this.fullFileName.substring( 0, this.fullFileName.lastIndexOf( '.' ) ); 164 this.fileExtension = this.fullFileName.substring( this.fullFileName.lastIndexOf( '.' ) + 1 ); 165 166 } else { 167 168 this.fileName = this.fullFileName; 169 170 } 171 172 } catch( java.net.MalformedURLException e ) { 173 174 176 } 177 178 } 179 180 181 188 private void writeObject(ObjectOutputStream out) throws IOException { 189 190 out.writeObject( parentFileSystem.getSystemName( ) ); 192 out.writeObject( uriStem ); 193 194 } 195 196 197 204 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 205 206 String fileSystemName; 208 HTTPFileSystem newParentFileSystem; 210 String newURIStem; 212 213 fileSystemName = (String )in.readObject( ); 215 newParentFileSystem = (HTTPFileSystem)Repository.getDefault( ).findFileSystem( fileSystemName ); 216 217 newURIStem = (String )in.readObject( ); 219 initialize( newURIStem, newParentFileSystem ); 220 } 221 222 223 228 private void readFileHeader( ) { 229 230 try { 231 getFileConnection( "HEAD" ).disconnect( ); 235 } catch( IOException e ) { 236 237 } 239 } 240 241 242 254 private HttpURLConnection getFileConnection( String requestMethod ) throws IOException { 255 256 HttpURLConnection fileConnection; 258 259 260 fileConnection = (HttpURLConnection )fileURL.openConnection( ); 262 fileConnection.setUseCaches( true ); 263 fileConnection.setRequestMethod( requestMethod ); 264 265 if( !wasFileHeaderRead ) { 267 268 readFileHeadersFromConnection( fileConnection ); 270 271 } 272 return fileConnection; 273 } 274 275 276 285 private HttpURLConnection getFileConnection( ) throws IOException { 286 287 return getFileConnection( "GET" ); } 289 290 291 299 private void readFileHeadersFromConnection( HttpURLConnection fileConnection ) { 300 301 304 fileSize = fileConnection.getContentLength( ); 305 fileMIMEType = fileConnection.getContentType( ); 306 fileDate = new Date( fileConnection.getLastModified( ) ); 307 314 wasFileHeaderRead = true; 315 316 } 317 318 319 329 public org.openide.filesystems.FileSystem getFileSystem( ) throws FileStateInvalidException { 330 331 if( parentFileSystem != null ) { 332 333 return parentFileSystem; 334 335 } else { 336 337 throw new FileStateInvalidException( NbBundle.getMessage(HTTPFileObject.class, "MSG_FilesystemNotFound" ) ); 339 } 340 } 341 342 343 348 public String getName( ) { 349 350 return fileName; 351 } 352 353 354 359 public String getExt( 360 ) { 361 362 return fileExtension; 363 364 } 365 366 367 372 public String getNameExt( 373 ) { 374 375 return fullFileName; 376 377 } 378 379 380 385 public long getSize( ) { 386 387 if( !wasFileHeaderRead ) { 388 readFileHeader( ); 389 } 390 return fileSize; 391 } 392 393 394 399 public String getMIMEType( ) { 400 if( !wasFileHeaderRead ) { 401 readFileHeader( ); 402 } 403 return fileMIMEType; 404 } 405 406 407 412 public FileObject getParent( ) { 413 return parentFileObject; 414 } 415 416 417 422 public Date lastModified( ) { 423 if( !wasFileHeaderRead ) { 424 readFileHeader( ); 425 } 426 return fileDate; 427 } 428 429 430 435 public boolean isReadOnly( ) { 436 return true; 437 } 438 439 440 445 public boolean isValid( ) { 446 return fileURL != null; 447 } 448 449 450 455 public boolean isRoot( ) { 456 return uriStem.equals( "/" ); } 458 459 460 465 public boolean isFolder( 466 ) { 467 468 return uriStem.endsWith( "/" ); 470 } 471 472 473 478 public boolean isData( 479 ) { 480 481 return !isFolder( ); 482 483 } 484 485 486 496 public FileLock lock( 497 ) throws IOException { 498 499 return FileLock.NONE; 500 501 } 502 503 504 515 public FileObject createData( 516 String fileName, 517 String extension 518 ) throws IOException { 519 520 throw new IOException( ); 521 522 } 523 524 525 535 public FileObject createFolder( 536 String fileName 537 ) throws IOException { 538 539 throw new IOException( ); 540 541 } 542 543 544 556 public void rename( 557 FileLock lock, 558 String fileName, 559 String extension 560 ) throws IOException { 561 562 throw new IOException( ); 563 } 564 565 566 576 public void delete( FileLock lock ) throws IOException { 577 throw new IOException( ); 578 } 579 580 581 589 public void setImportant( boolean isImportant ) { 590 } 592 593 594 599 public java.util.Enumeration getAttributes() { 600 601 if( !wasFileHeaderRead ) { 602 603 readFileHeader( ); 604 605 } 606 return fileAttributes.keys( ); 607 } 608 609 610 619 public Object getAttribute( String attributeName ) { 620 621 if( !wasFileHeaderRead ) { 622 readFileHeader( ); 623 } 624 return fileAttributes.get( attributeName ); 625 } 626 627 628 639 public void setAttribute( 640 String attributeName, 641 Object newValue 642 ) throws IOException { 643 644 throw new IOException( ); 645 } 646 647 648 653 public FileObject[] getChildren( ) { 654 655 return (FileObject[])getChildFileObjects( true ).values( ).toArray( new FileObject[ 0 ] ); 656 } 657 658 659 670 public FileObject getFileObject( String fileName, String extension ) { 671 672 if( extension != null && extension.equals( "" ) == false ) { return child( fileName + "." + extension ); } else { 675 return child( fileName ); 676 } 677 } 678 679 680 688 public InputStream getInputStream() throws FileNotFoundException { 689 690 try { 691 return new HTTPFileInputStream( getFileConnection( ) ); 692 } catch( IOException e ) { 693 throw new java.io.FileNotFoundException ( e.getMessage( ) ); 694 } 695 } 696 697 698 708 public OutputStream getOutputStream(FileLock lock) throws IOException { 709 throw new IOException( ); 710 } 711 712 713 722 public void addFileChangeListener(FileChangeListener listener) { 723 724 listeners.add( listener ); 725 726 } 727 728 729 738 public void removeFileChangeListener(FileChangeListener listener) { 739 740 listeners.remove( listener ); 741 742 } 743 744 745 752 void addChild( HTTPFileObject newChildFileObject ) { 753 754 newChildFileObject.parentFileObject = this; 755 childFileObjects.put( newChildFileObject.getNameExt( ), newChildFileObject ); 756 757 if( !listeners.isEmpty( ) && areFolderContentsKnown ) { 759 760 if( newChildFileObject.isData( ) ) { 762 763 fireFileDataCreatedEvent( listeners.elements( ), new FileEvent( this, newChildFileObject, true ) ); 764 765 } else { 766 767 fireFileFolderCreatedEvent( listeners.elements( ), new FileEvent( this, newChildFileObject, true ) ); 768 769 } 770 771 } 772 773 } 774 775 776 781 void removeAllChildren( 782 ) { 783 784 Iterator childIterator; 786 HTTPFileObject childFile; 788 789 790 synchronized( childFileObjects ) { 791 792 childIterator = childFileObjects.values( ).iterator( ); 793 while( childIterator.hasNext( ) ) { 794 795 childFile = (HTTPFileObject)childIterator.next( ); 796 childIterator.remove( ); 797 childFile.parentFileObject = null; 798 799 if( !listeners.isEmpty( ) && areFolderContentsKnown ) { 801 802 fireFileDeletedEvent( listeners.elements( ), new FileEvent( this, childFile, true ) ); 804 805 } 806 807 } 808 809 } 810 811 } 812 813 814 822 void addChild( String newChildFileName ) { 823 addChild( new HTTPFileObject( newChildFileName, parentFileSystem ) ); 824 } 825 826 827 838 boolean addOptionalChild( String newChildFileName ) { 839 HttpURLConnection fileConnection; 841 HTTPFileObject childFileObject; 843 boolean wasFileAdded; 845 846 847 fileConnection = null; 848 try { 849 850 childFileObject = new HTTPFileObject( newChildFileName, parentFileSystem ); 852 fileConnection = childFileObject.getFileConnection( "HEAD" ); 854 if( fileConnection.getResponseCode( ) < 400 ) { 856 857 addChild( childFileObject ); 859 wasFileAdded = true; 860 861 } else { 862 863 wasFileAdded = false; 864 } 865 866 } catch( Exception e ) { 867 868 wasFileAdded = false; 869 870 } finally { 871 872 if( fileConnection != null ) { 874 875 fileConnection.disconnect( ); 876 } 877 878 } 879 return wasFileAdded; 880 } 881 882 883 893 HTTPFileObject child( String fullFileName ) { 894 895 return child( fullFileName, true ); 896 897 } 898 899 900 913 HTTPFileObject child( String fullFileName, boolean readPackageContents ) { 914 915 return (HTTPFileObject)getChildFileObjects( readPackageContents ).get( fullFileName ); 916 917 } 918 919 920 932 private Map getChildFileObjects( boolean readPackageContents ) { 933 934 if( readPackageContents ) { 936 937 synchronized( childFileObjects ) { 938 939 if( !areFolderContentsKnown ) { 940 941 readPackageContents( ); 943 944 } 945 946 } 947 948 } 949 return childFileObjects; 950 951 } 952 953 954 959 void makePackage() { 960 areFolderContentsKnown = false; 961 } 962 963 964 969 private void readPackageContents( ) { 970 971 HTTPFileObject packageSummaryFile; 973 HTTPFileObject classUseDirectory; 975 InputStream packageFileInputStream; 977 HTMLEditorKit editorKit; 979 HTMLDocument htmlDoc; 981 HTMLDocument.Iterator tagIterator; 983 String classFileName; 985 986 987 packageSummaryFile = new HTTPFileObject( uriStem + "package-summary.html", parentFileSystem ); addChild( packageSummaryFile ); 990 addOptionalChild( uriStem + "package-frame.html" ); addOptionalChild( uriStem + "package-tree.html" ); if( addOptionalChild( uriStem + "package-use.html" ) ) { 994 classUseDirectory = new HTTPFileObject( uriStem + "class-use/", parentFileSystem ); addChild( classUseDirectory ); 996 997 } else { 998 999 classUseDirectory = null; 1000 1001 } 1002 1003 try { 1004 1005 packageFileInputStream = packageSummaryFile.getInputStream( ); 1007 editorKit = new HTMLEditorKit(); 1008 htmlDoc = (HTMLDocument)editorKit.createDefaultDocument(); 1009 editorKit.read( new InputStreamReader( packageFileInputStream ), htmlDoc, 0); 1010 1011 tagIterator = htmlDoc.getIterator( HTML.Tag.A ); 1013 while( tagIterator.isValid( ) ) { 1014 1015 classFileName = (String )tagIterator.getAttributes( ).getAttribute( HTML.Attribute.HREF ); 1017 if( classFileName != null ) { 1018 1019 if( classFileName.indexOf( '/' ) == -1 && !classFileName.startsWith( "." ) && !classFileName.startsWith( "#" ) && classFileName.indexOf( ':' ) == -1 && !classFileName.startsWith( "package-" ) ) { 1024 addChild( uriStem + classFileName ); 1026 1027 if( classUseDirectory != null ) { 1029 classUseDirectory.addChild( classUseDirectory.uriStem + classFileName ); 1031 } 1032 } 1033 } 1034 tagIterator.next( ); 1035 } 1036 packageFileInputStream.close( ); 1037 1038 } catch( BadLocationException e ) { 1039 } catch( IOException e ) { 1041 } finally { 1043 areFolderContentsKnown = true; 1044 } 1045 } 1046 1047} 1048 | Popular Tags |