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.FileUtil;
33
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
34

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

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

    
197
	/**
198
	 * Gets the local file directory path
199
	 * 
200
	 * @return a string holding the local file directory path
201
	 */
202
	public String getLocalFileDir() {
203
		return localFileDir;
204
	}
205
}
(3-3/4)