parse.addProblem("exactly 1 attribute in {resource, file, class, url} was expected in mapping: "+XmlUtil.toString(element));
}
}
public Object parse(Element element, Parse parse, Parser parser) {
HibernateConfigurationDescriptor descriptor = new HibernateConfigurationDescriptor();
String configurationClassName = null;
if (element.hasAttribute("class")) {
configurationClassName = element.getAttribute("class");
} else {
configurationClassName = Configuration.class.getName();
}
descriptor.setClassName(configurationClassName);
if (element.hasAttribute("table-prefix")) {
descriptor.setTablePrefix(element.getAttribute("table-prefix"));
}
if (element.hasAttribute("naming-strategy")) {
descriptor.setNamingStrategyClassName(element.getAttribute("naming-strategy"));
}
// TODO add schema-update="enabled" attibute that automatically applies the schema generation
List<Element> configElements = XmlUtil.elements(element);
if (configElements!=null) {
for (Element configElement: configElements) {
if ("mappings".equals(XmlUtil.getTagName(configElement))) {
if (configElement.hasAttribute("resource")) {
String resource = configElement.getAttribute("resource");
InputStream stream = parser.getClassLoader().getResourceAsStream(resource);
parser.importStream(stream, configElement, parse);
}
if (configElement.hasAttribute("resources")) {
String resources = configElement.getAttribute("resources");
try {
Enumeration<URL> enumeration = parser.getClassLoader().getResources(resources);
if (enumeration!=null) {
while (enumeration.hasMoreElements()) {
URL url = enumeration.nextElement();
log.finest("parsing mappings resource from "+url);
InputStream inputStream = url.openStream();
parse.pushObject(descriptor);
try {
mappingParser.parseInputStream(inputStream, parse);
} finally {
parse.popObject();
inputStream.close();
}
}
}
} catch (Exception e) {
parse.addProblem("couldn't parse hibernate mapping resources '"+resources+"'", e);
}
}
} else if ("mapping".equals(XmlUtil.getTagName(configElement))) {
parseMapping(configElement, descriptor, parse);
} else if ("properties".equals(XmlUtil.getTagName(configElement))) {
PropertiesDescriptor propertiesDescriptor = (PropertiesDescriptor) propertiesBinding.parse(configElement, parse, parser);
descriptor.setPropertiesDescriptor(propertiesDescriptor);
} else if ("cache-configuration".equals(XmlUtil.getTagName(configElement))) {
InputStream stream = null;
String cacheUsage = configElement.getAttribute("usage");
if (! ( ("read-only".equals(cacheUsage))
|| ("nonstrict-read-write".equals(cacheUsage))
|| ("read-write".equals(cacheUsage))
|| ("transactional".equals(cacheUsage))
)
){
parse.addProblem("problem in cache-configuration: no usage attribute or illegal value: "+cacheUsage+" Possible values are {read-only, nonstrict-read-write, read-write, transactional}");
} else {
if (configElement.hasAttribute("file")) {
String fileName = configElement.getAttribute("file");
File file = new File(fileName);
if (file.exists() && file.isFile()) {
try {
stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
parse.addProblem("file "+fileName+" isn't a file", e);
}
} else {
parse.addProblem("file "+fileName+" isn't a file");
}
}
if (configElement.hasAttribute("resource")) {
String resource = configElement.getAttribute("resource");
stream = parser.getClassLoader().getResourceAsStream(resource);
if (stream==null) {
parse.addProblem("resource "+resource+" doesn't exist");
}
}
if (configElement.hasAttribute("url")) {
String urlText = configElement.getAttribute("url");
try {
URL url = new URL(urlText);
stream = url.openStream();
if (stream==null) {
parse.addProblem("couldn't open url "+urlText);
}
} catch (Exception e) {
parse.addProblem("couldn't open url "+urlText, e);
}
}
if (stream != null) {
parser.importStream(stream, configElement, parse);
}
// parse the cache configurations in the same way as the hibernate cfg schema
// translate the contents of the file into invoke operations for methods
// Configuration.setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region)
// Configuration.setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy)
// <class-cache class="org.hibernate.auction.Item" usage="read-write"/>
// <class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
// <collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
List<Element> cacheElements = XmlUtil.elements(configElement);
if (cacheElements!=null) {
for (Element cacheElement : cacheElements) {
if ("class-cache".equals(XmlUtil.getTagName(cacheElement))) {
String className = cacheElement.getAttribute("class");
descriptor.addCacheOperation(new SetCacheConcurrencyStrategy(className, cacheUsage));
} else if ("collection-cache".equals(XmlUtil.getTagName(cacheElement))) {
String collection = cacheElement.getAttribute("collection");
descriptor.addCacheOperation(new SetCollectionCacheConcurrencyStrategy(collection, cacheUsage));
} else {
parse.addProblem("unknown hibernate cache configuration element "+XmlUtil.toString(configElement));
}
}