KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > IncludedSchemaDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.schema;
12
13 import java.io.File JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.core.plugin.PluginRegistry;
21 import org.eclipse.pde.internal.core.ischema.ISchema;
22 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
23
24 public class IncludedSchemaDescriptor implements ISchemaDescriptor {
25     private URL JavaDoc fSchemaURL;
26     private String JavaDoc fSchemaLocation;
27     private Schema fSchema;
28     private long fLastModified;
29
30     public IncludedSchemaDescriptor(URL JavaDoc schemaURL) {
31         fSchemaURL = schemaURL;
32         File JavaDoc file = new File JavaDoc(fSchemaURL.getFile());
33         if (file.exists())
34             fLastModified = file.lastModified();
35     }
36     
37     public static URL JavaDoc computeURL(ISchemaDescriptor parentDesc, String JavaDoc schemaLocation) throws MalformedURLException JavaDoc {
38         URL JavaDoc parentURL = parentDesc == null ? null : parentDesc.getSchemaURL();
39         if (schemaLocation.startsWith("schema://")) { //$NON-NLS-1$
40
// extract plug-in ID
41
IPath path = new Path( schemaLocation.substring(9));
42             return getPluginRelativePath(path.segment(0), path.removeFirstSegments(1), parentURL);
43         }
44         
45         if (parentURL == null)
46             return null;
47         
48         // parent-relative location
49
IPath path = new Path(parentURL.getPath());
50         path = path.removeLastSegments(1).append(schemaLocation);
51         return new URL JavaDoc(parentURL.getProtocol(), parentURL.getHost(), path.toString());
52     }
53     
54     private static URL JavaDoc getPluginRelativePath(String JavaDoc pluginID, IPath path, URL JavaDoc parentURL) {
55         URL JavaDoc url = SchemaRegistry.getSchemaURL(pluginID, path.toString());
56         if (url == null) {
57             IPluginModelBase model = PluginRegistry.findModel(pluginID);
58             if (model != null)
59                 url = SchemaRegistry.getSchemaFromSourceExtension(model.getPluginBase(), path);
60         }
61         try {
62             if (url == null && parentURL != null) {
63                 String JavaDoc parentFile = parentURL.getFile();
64                 if (parentFile == null)
65                     return null;
66                 int lastSep = parentFile.lastIndexOf(File.separatorChar);
67                 parentFile = parentFile.substring(0, lastSep + 1);
68                 // assuming schemas are located in: pluginId/schema/schemaFile.exsd
69
File JavaDoc file = new File JavaDoc(parentFile + "../../" + pluginID + "/" + path.toString()); //$NON-NLS-1$ //$NON-NLS-2$
70
if (file.exists() && file.isFile())
71                     url = file.toURL();
72             }
73         } catch (MalformedURLException JavaDoc e) {
74         }
75         return url;
76     }
77
78     /**
79      * @see org.eclipse.pde.internal.core.schema.AbstractSchemaDescriptor#isEnabled()
80      */

81     public boolean isEnabled() {
82         return true;
83     }
84
85     /**
86      * @see org.eclipse.pde.internal.core.ischema.ISchemaDescriptor#getPointId()
87      */

88     public String JavaDoc getPointId() {
89         int dotLoc = fSchemaLocation.lastIndexOf('.');
90         if (dotLoc!= -1) {
91             return fSchemaLocation.substring(0, dotLoc);
92         }
93         return null;
94     }
95
96     /**
97      * @see org.eclipse.pde.internal.core.ischema.ISchemaDescriptor#getSchemaURL()
98      */

99     public URL JavaDoc getSchemaURL() {
100         return fSchemaURL;
101     }
102
103     /* (non-Javadoc)
104      * @see org.eclipse.pde.internal.core.ischema.ISchemaDescriptor#getSchema(boolean)
105      */

106     public ISchema getSchema(boolean abbreviated) {
107         if (fSchema == null && fSchemaURL != null) {
108             fSchema = new Schema(this, fSchemaURL, abbreviated);
109             fSchema.load();
110         }
111         return fSchema;
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.pde.internal.core.ischema.ISchemaDescriptor#isStandalone()
116      */

117     public boolean isStandalone() {
118         return false;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.pde.internal.core.ischema.ISchemaDescriptor#getLastModified()
123      */

124     public long getLastModified() {
125         return fLastModified;
126     }
127
128 }
129
Popular Tags