KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > MappingFileReader


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import net.sf.dozer.util.mapping.fieldmap.Mappings;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * @author tierney.matt
29  * @author garsombke.franz
30  */

31 public class MappingFileReader {
32   private static final Log log = LogFactory.getLog(MappingFileReader.class);
33   
34   private final URL JavaDoc url;
35   private final MappingUtils mappingUtils = new MappingUtils();
36   
37   public MappingFileReader(URL JavaDoc url) {
38     this.url = url;
39   }
40   
41   public MappingFileReader(String JavaDoc fileName) {
42     Loader loader = new Loader();
43     url = loader.getResource(fileName);
44   }
45   
46   public Mappings read() {
47     Mappings result = null;
48     InputStream JavaDoc stream = null;
49     try {
50       XMLParser parser = new XMLParser();
51       stream = url.openStream();
52       result = parser.parse(stream);
53     } catch (Throwable JavaDoc e) {
54       log.error("Error in loading dozer mapping file url: [" + url + "] : " + e);
55       mappingUtils.throwMappingException(e);
56     } finally {
57       try {
58         if (stream != null) {
59           stream.close();
60         }
61       } catch (IOException JavaDoc e) {
62         mappingUtils.throwMappingException(e);
63       }
64     }
65     return result;
66   }
67   
68 }
Popular Tags