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.util.SystemUtil;
32
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
33

    
34
public class XMLSchema {
35
	
36
	private String fileNamespace = null;
37
	private String externalFileUri = null;
38
	private String fileName = null;
39
	private String localFileUri = null;
40
	private String localFileDir = null;
41
	
42
	private Logger logMetacat = Logger.getLogger(XMLSchema.class);
43

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

    
193
	/**
194
	 * Gets the local file directory path
195
	 * 
196
	 * @return a string holding the local file directory path
197
	 */
198
	public String getLocalFileDir() {
199
		return localFileDir;
200
	}
201
}
(8-8/9)