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
	private String formatId = null;
43
	
44

    
45
    private Logger logMetacat = Logger.getLogger(XMLSchema.class);
46

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

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

    
218
	/**
219
	 * Set the format id. 
220
	 * @param formatId. 
221
	 */
222
    public void setFormatId(String formatId) {
223
            this.formatId = formatId;
224
    }
225
}
(3-3/5)