KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > xml > XMLLoader


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.xml;
25
26 import java.io.InputStream JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.fractal.adl.ADLException;
30 import org.objectweb.fractal.adl.Definition;
31 import org.objectweb.fractal.adl.Loader;
32 import org.objectweb.fractal.adl.Node;
33 import org.objectweb.fractal.adl.Parser;
34 import org.objectweb.fractal.adl.ParserException;
35
36 /**
37  * A {@link Loader} that loads definitions from XML files.
38  */

39
40 public class XMLLoader implements Loader {
41
42   private Parser parser;
43
44   public XMLLoader () {
45     this(true);
46   }
47   
48   public XMLLoader (boolean validate) {
49     parser = new XMLParser(validate);
50   }
51   
52   public Definition load (final String JavaDoc name, final Map JavaDoc context)
53     throws ADLException
54   {
55     try {
56       String JavaDoc file = name.replace('.', '/') + ".fractal";
57       ClassLoader JavaDoc cl = null;
58       if (context != null) {
59         cl = (ClassLoader JavaDoc)context.get("classloader");
60       }
61       if (cl == null) {
62         cl = getClass().getClassLoader();
63       }
64       InputStream JavaDoc is = cl.getResourceAsStream(file);
65       if (is == null) {
66         throw new ADLException(
67           "Cannot find '" + file + "' in the classpath", null);
68       }
69       Definition d = (Definition)parser.parse(is, file);
70       if (d.getName() == null) {
71         throw new ADLException("Definition name missing", (Node)d);
72       }
73       if (!d.getName().equals(name)) {
74         throw new ADLException(
75           "Wrong definition name ('" + name +
76           "' expected, instead of '" + d.getName() + "')", (Node)d);
77       }
78       return d;
79     } catch (ParserException e) {
80       throw new ADLException("Cannot load '" + name + "'", null, e);
81     }
82   }
83 }
84
Popular Tags