1 23 24 package org.objectweb.fractal.adl.xml; 25 26 import java.io.InputStream ; 27 import java.util.Map ; 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 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 name, final Map context) 53 throws ADLException 54 { 55 try { 56 String file = name.replace('.', '/') + ".fractal"; 57 ClassLoader cl = null; 58 if (context != null) { 59 cl = (ClassLoader )context.get("classloader"); 60 } 61 if (cl == null) { 62 cl = getClass().getClassLoader(); 63 } 64 InputStream 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 |