Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that stores xml schema information 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: leinfelder $'
9
 *     '$Date: 2008-09-26 15:43:57 -0700 (Fri, 26 Sep 2008) $'
10
 * '$Revision: 4399 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.service;
28

    
29
import org.apache.log4j.Logger;
30

    
31
import edu.ucsb.nceas.metacat.DocumentImpl;
32
import edu.ucsb.nceas.metacat.util.SystemUtil;
33
import edu.ucsb.nceas.utilities.FileUtil;
34
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
35

    
36
public class XMLSchema {
37
	
38
	private String fileNamespace = null;
39
	private String externalFileUri = null;
40
	private String fileName = null;
41
	private String localFileUri = null;
42
	private String localFileDir = null;
43
	private String formatId = null;
44
	
45
	private static final String type = DocumentImpl.SCHEMA;
46
	
47

    
48
    private Logger logMetacat = Logger.getLogger(XMLSchema.class);
49

    
50
	/**
51
	 * Constructor - the schema file name will be extracted from the external
52
	 * file uri. The local file uri and local file dir will be constructed using
53
	 * system values and the file name.
54
	 * 
55
	 * @param fileNamespace
56
	 *            the file's name space
57
	 * @param externalFileUri
58
	 *            the external uri where the schema is located
59
	 */
60
	public XMLSchema(String fileNamespace, String externalFileUri, String formatId) {
61
		setFileNamespace(fileNamespace);
62
		setExternalFileUri(externalFileUri);
63
		setFormatId(formatId);
64
	}
65
	
66
	/**
67
	 * Constructor - sets the schema file namespace only. The file name will
68
	 * need to be added separately.
69
	 * 
70
	 * @param namespace
71
	 *            the file's name space
72
	 */
73
	/*public XMLSchema(String fileNamespace) {
74
		setFileNamespace(fileNamespace);
75
	}*/
76
	
77
	/**
78
	 * Set the file name. The local file uri and local file dir will also get
79
	 * set using system context url and dir values and the schema directory
80
	 * value.
81
	 * 
82
	 * @param fileName
83
	 *            the file name to set
84
	 */
85
	public void setFileName(String fileName) {
86
		// there are a few different cases for the file name:
87
		// -- it starts with /schema/.  if so, use everything after /schema/ as 
88
		//    the file name.
89
		// -- it starts with http and has /schema/ in the path.  again, use 
90
		//    everything after /schema/
91
		// -- it starts with http but doesnt have /schema/ in the path.  use 
92
		//    everything after the last / as the file name
93
		// -- otherwise leave the file name as is.
94
		if (fileName.startsWith(XMLSchemaService.SCHEMA_DIR)) {
95
			fileName = fileName.substring(XMLSchemaService.SCHEMA_DIR.length());
96
		} else if (fileName.startsWith("http") && fileName.contains(XMLSchemaService.SCHEMA_DIR)) {
97
			int index = fileName.lastIndexOf(XMLSchemaService.SCHEMA_DIR) + XMLSchemaService.SCHEMA_DIR.length();
98
			fileName = fileName.substring(index);
99
		} else if (fileName.startsWith("http")) {
100
			fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
101
		}
102
			
103
		this.fileName = fileName;
104
		try { 
105
			this.localFileUri = SystemUtil.getContextURL() + XMLSchemaService.SCHEMA_DIR
106
					+ fileName;
107
			logMetacat.debug("XMLSchema.setFileName - localFileUri: " + this.localFileUri);
108
		} catch (PropertyNotFoundException pnfe) {
109
			localFileUri = XMLSchemaService.SCHEMA_DIR + fileName;
110
			logMetacat.warn("XMLSchema.setFileName - Could not get context url. Setting localFileUri to: "
111
					+ localFileUri);
112
		}
113
		try {
114
			String fileDir = SystemUtil.getContextDir() + XMLSchemaService.SCHEMA_DIR
115
				+ fileName;
116
			this.localFileDir = FileUtil.normalizePath(fileDir);
117
			logMetacat.debug("XMLSchema.setFileName - localFileDir: " + this.localFileDir);
118
		} catch (PropertyNotFoundException pnfe) {
119
			localFileDir = XMLSchemaService.SCHEMA_DIR + fileName;
120
			logMetacat.warn("XMLSchema.setFileName - Could not get context directory. Setting localFileDir to: "
121
					+ localFileDir);
122
		}
123
	}
124
	
125
	/**
126
	 * Gets the file name
127
	 * 
128
	 * @return string holding the file name
129
	 */
130
	public String getFileName() {
131
		return fileName;
132
	}
133
	
134
	/**
135
	 * Sets the file namespace
136
	 * 
137
	 * @param fileNamespace
138
	 *            the namespace to set
139
	 */
140
	public void setFileNamespace(String fileNamespace) {
141
		this.fileNamespace = fileNamespace;
142
	}
143
	
144
	/**
145
	 * Gets the file namespace
146
	 * 
147
	 * @return a string holding the file namespace
148
	 */
149
	public String getFileNamespace() {
150
		return fileNamespace;
151
	}
152
	
153
	/**
154
	 * Sets the external file uri. Extracts the file name from the uri and sets
155
	 * the file name as well.
156
	 * 
157
	 * @param externalFileUri
158
	 *            the external file uri to set
159
	 */
160
	public void setExternalFileUri(String externalFileUri) {
161
		this.externalFileUri = externalFileUri;
162
		String fileName = XMLSchemaService.getSchemaFileNameFromUri(externalFileUri);
163
		setFileName(fileName);
164
	}
165
	
166
	/**
167
	 * Gets the external file uri
168
	 * @return a string holding the external file uri
169
	 */
170
	public String getExternalFileUri() {
171
		return externalFileUri;
172
	}
173
	
174
	/**
175
	 * Sets the local file uri. If the uri doesn't start with http:// the full
176
	 * uri is constructed using the system context url and the schema directory.
177
	 * 
178
	 * @param localFileUri
179
	 *            the base uri to set.
180
	 */
181
	public void setLocalFileUri(String localFileUri) {
182
		if (!localFileUri.startsWith("http://")) {
183
			try {
184
				localFileUri = SystemUtil.getContextURL() + XMLSchemaService.SCHEMA_DIR + localFileUri;
185
			} catch (PropertyNotFoundException pnfe) {
186
				logMetacat.warn("XMLSchema.setLocalFileUri - Could not find context url: " + pnfe.getMessage() + 
187
						". Setting schema file uri to: " + XMLSchemaService.SCHEMA_DIR + localFileUri);
188
				localFileUri = XMLSchemaService.SCHEMA_DIR + localFileUri;
189
			}
190
		}
191
		this.localFileUri = localFileUri;
192
	}
193
	
194
	/**
195
	 * Gets the local file uri
196
	 * 
197
	 * @return a string holding the local file uri
198
	 */
199
	public String getLocalFileUri() {
200
		return localFileUri;
201
	}
202

    
203
	/**
204
	 * Gets the local file directory path
205
	 * 
206
	 * @return a string holding the local file directory path
207
	 */
208
	public String getLocalFileDir() {
209
		return localFileDir;
210
	}
211
	
212
	
213
	/**
214
	 * Get the format id
215
	 * @return the format id
216
	 */
217
	public String getFormatId() {
218
        return formatId;
219
    }
220

    
221
	/**
222
	 * Set the format id. 
223
	 * @param formatId. 
224
	 */
225
    public void setFormatId(String formatId) {
226
            this.formatId = formatId;
227
    }
228
    
229
    /**
230
     * Return the type of the schema. It always is "Schema"
231
     * @return
232
     */
233
    public static String getType() {
234
        return type;
235
    }
236
}
(5-5/7)