KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > archive > Handler


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id$
16  */

17 package org.eclipse.emf.common.archive;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23 import java.net.URLStreamHandler JavaDoc;
24
25 /**
26  * A URL stream handler that can be {@link #register() registered} to support archive access protocol.
27  * It uses {@link ArchiveURLConnection} to implement the connection.
28  */

29 public class Handler extends URLStreamHandler JavaDoc
30 {
31   /**
32    * Registers this class.
33    * A handler for protocol "xyz" is registered
34    * by providing a class named Handler implementing {@link URLStreamHandler}
35    * in a package called named xyz in a package of your choosing,
36    * and then registering that chosen prefix package name
37    * in the system property for <code>"java.protocol.handler.pkgs"</code>,
38    * which is an "|" separated list of package prefixes to search for handlers.
39    */

40   public static void register()
41   {
42     String JavaDoc javaProtocolHandlerPkgs = System.getProperty("java.protocol.handler.pkgs");
43     if (javaProtocolHandlerPkgs == null || javaProtocolHandlerPkgs.length() == 0)
44     {
45       javaProtocolHandlerPkgs = "org.eclipse.emf.common";
46     }
47     else
48     {
49       javaProtocolHandlerPkgs += "|org.eclipse.emf.common";
50     }
51     System.setProperty("java.protocol.handler.pkgs", javaProtocolHandlerPkgs);
52   }
53   
54   /**
55    * Registers this handler and interprets each argument as URL to be opened, read in, and written out to System.out.
56    * @param args URLs to open, read, and write to System.out
57    * @throws IOException if there are problems opening or reading from the URLs, or writing to System.out.
58    */

59   public static void main(String JavaDoc[] args) throws IOException JavaDoc
60   {
61     register();
62     
63     for (int i = 0; i < args.length; ++i)
64     {
65       InputStream JavaDoc inputStream = new URL JavaDoc(args[i]).openStream();
66       byte [] bytes = new byte [4048];
67       for (int size; (size = inputStream.read(bytes, 0, bytes.length)) > -1; )
68       {
69         System.out.write(bytes, 0, size);
70       }
71     }
72   }
73   
74   /**
75    * Creates an instance.
76    */

77   public Handler()
78   {
79     super();
80   }
81   
82   /**
83    * Overrides parsing the URL to validate constraints on well formed archive syntax.
84    * @see URLStreamHandler#parseURL(java.net.URL, java.lang.String, int, int)
85    */

86   protected void parseURL(URL JavaDoc url, String JavaDoc specification, int start, int limit)
87   {
88     super.parseURL(url, specification, start, limit);
89       
90     // There needs to be another URL protocol right after the archive protocol, and not a "/".
91
//
92
if (start > limit || specification.charAt(start) == '/')
93     {
94       throw
95         new IllegalArgumentException JavaDoc
96           ("archive protocol must be immediately followed by another URL protocol " + specification);
97     }
98     
99     // There must be at least one archive path.
100
//
101
int archiveSeparator = specification.indexOf("!/", start);
102     if (archiveSeparator < 0)
103     {
104       throw new IllegalArgumentException JavaDoc("missing archive separators " + specification.substring(start, limit));
105     }
106           
107     // Parse to count the archive paths that must will be delegated to the nested URL based on the number of schemes at the start.
108
//
109
for (int i = start, end = specification.indexOf('/', start) - 1; (i = specification.indexOf(':', i)) < end; ++i)
110     {
111       // There should be at least one archive separator per scheme.
112
//
113
archiveSeparator = specification.indexOf("!/", archiveSeparator + 2);
114       if (archiveSeparator < 0)
115       {
116         throw new IllegalArgumentException JavaDoc("too few archive separators " + specification);
117       }
118     }
119   }
120   
121   /**
122    * Returns a new {@link ArchiveURLConnection}.
123    */

124   protected URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc
125   {
126     return new ArchiveURLConnection(url);
127   }
128 }
129
Popular Tags