Skip to content

Commit

Permalink
Exception conversion for XML parser to support localization
Browse files Browse the repository at this point in the history
  • Loading branch information
drallen committed Dec 1, 2009
1 parent e5625c7 commit c3fe669
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 13 deletions.
45 changes: 45 additions & 0 deletions impl/src/main/java/org/jboss/weld/logging/messages/XmlMessage.java
@@ -0,0 +1,45 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.logging.messages;

import org.jboss.weld.logging.MessageId;

import ch.qos.cal10n.BaseName;
import ch.qos.cal10n.Locale;
import ch.qos.cal10n.LocaleData;

@BaseName("org.jboss.weld.messages.xml")
@LocaleData({
@Locale("en")
})
/**
* Error messages relating to XML parser
*
* Message ids: 001200 - 001299
*/
public enum XmlMessage
{
@MessageId("001200") CONFIGURATION_ERROR,
@MessageId("001201") LOAD_ERROR,
@MessageId("001202") PARSING_ERROR,
@MessageId("001203") MULTIPLE_ALTERNATIVES,
@MessageId("001204") MULTIPLE_DECORATORS,
@MessageId("001205") MULTIPLE_INTERCEPTORS,
@MessageId("001206") CANNOT_LOAD_CLASS;

}
31 changes: 18 additions & 13 deletions impl/src/main/java/org/jboss/weld/xml/BeansXmlParser.java
Expand Up @@ -16,19 +16,24 @@
*/
package org.jboss.weld.xml;

import static org.jboss.weld.logging.messages.XmlMessage.CANNOT_LOAD_CLASS;
import static org.jboss.weld.logging.messages.XmlMessage.CONFIGURATION_ERROR;
import static org.jboss.weld.logging.messages.XmlMessage.LOAD_ERROR;
import static org.jboss.weld.logging.messages.XmlMessage.MULTIPLE_ALTERNATIVES;
import static org.jboss.weld.logging.messages.XmlMessage.MULTIPLE_DECORATORS;
import static org.jboss.weld.logging.messages.XmlMessage.MULTIPLE_INTERCEPTORS;
import static org.jboss.weld.logging.messages.XmlMessage.PARSING_ERROR;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.enterprise.inject.InjectionException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.interceptor.Interceptor;

import org.jboss.weld.DeploymentException;
import org.jboss.weld.resources.spi.ResourceLoader;
Expand Down Expand Up @@ -122,7 +127,7 @@ public void parse()
}
catch (ParserConfigurationException e)
{
throw new InjectionException("Error configuring XML parser", e);
throw new WeldXmlException(CONFIGURATION_ERROR, e);
}
List<XmlElement> policiesElements = new ArrayList<XmlElement>();
List<XmlElement> decoratorsElements = new ArrayList<XmlElement>();
Expand All @@ -138,7 +143,7 @@ public void parse()
}
catch (IOException e)
{
throw new InjectionException("Error loading beans.xml " + url.toString(), e);
throw new WeldXmlException(LOAD_ERROR, e, url.toString());
}
if (fileHasContents)
{
Expand All @@ -150,11 +155,11 @@ public void parse()
}
catch (SAXException e)
{
throw new DeploymentException("Error parsing beans.xml " + url.toString(), e);
throw new DeploymentException(PARSING_ERROR, e, url.toString());
}
catch (IOException e)
{
throw new DeploymentException("Error loading beans.xml " + url.toString(), e);
throw new DeploymentException(LOAD_ERROR, e, url.toString());
}
Element beans = document.getDocumentElement();
for (Node child : new NodeListIterable(beans.getChildNodes()))
Expand All @@ -178,7 +183,7 @@ public void parse()

if (policiesElements.size() > 1)
{
throw new DeploymentException("<alternatives> can only be specified once, but it is specified muliple times " + policiesElements);
throw new DeploymentException(MULTIPLE_ALTERNATIVES, policiesElements);
}
else if (policiesElements.size() == 1)
{
Expand All @@ -189,7 +194,7 @@ else if (policiesElements.size() == 1)

if (decoratorsElements.size() > 1)
{
throw new DeploymentException("<decorator> can only be specified once, but it is specified muliple times " + decoratorsElements);
throw new DeploymentException(MULTIPLE_DECORATORS, decoratorsElements);
}
else if (decoratorsElements.size() == 1)
{
Expand All @@ -199,7 +204,7 @@ else if (decoratorsElements.size() == 1)

if (interceptorsElements.size() > 1)
{
throw new DeploymentException("<interceptor> can only be specified once, but it is specified muliple times " + interceptorsElements);
throw new DeploymentException(MULTIPLE_INTERCEPTORS, interceptorsElements);
}
else if (interceptorsElements.size() == 1)
{
Expand Down Expand Up @@ -230,7 +235,7 @@ private static void processPolicyElement(ResourceLoader resourceLoader, XmlEleme
}
catch (ResourceLoadingException e)
{
throw new DeploymentException("Cannot load class " + className + " defined in " + element.getFile().toString());
throw new DeploymentException(CANNOT_LOAD_CLASS, className, element.getFile());
}
}
}
Expand Down Expand Up @@ -263,7 +268,7 @@ private static List<Class<?>> processElement(ResourceLoader resourceLoader, XmlE
}
catch (ResourceLoadingException e)
{
throw new DeploymentException("Cannot load class " + className + " defined in " + element.getFile().toString());
throw new DeploymentException(CANNOT_LOAD_CLASS, className, element.getFile());
}
}
}
Expand All @@ -287,7 +292,7 @@ private static List<Class<?>> processInterceptorElement(ResourceLoader resourceL
}
catch (ResourceLoadingException e)
{
throw new DeploymentException("Cannot load class " + className + " defined in " + element.getFile().toString());
throw new DeploymentException(CANNOT_LOAD_CLASS, className, element.getFile());
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions impl/src/main/java/org/jboss/weld/xml/WeldXmlException.java
@@ -0,0 +1,50 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.xml;

import static org.jboss.weld.logging.LoggerFactory.loggerFactory;

import javax.enterprise.inject.InjectionException;

import ch.qos.cal10n.IMessageConveyor;

/**
* Used for exceptions from the Weld XML parser and provides localization
* support.
*
* @author David Allen
*
*/
public class WeldXmlException extends InjectionException
{

private static final long serialVersionUID = -6716110761385845182L;

// Exception messages
private static final IMessageConveyor messageConveyer = loggerFactory().getMessageConveyor();

public WeldXmlException(Throwable throwable)
{
super(throwable.getLocalizedMessage(), throwable);
}

public <E extends Enum<?>> WeldXmlException(E key, Object... args)
{
super(messageConveyer.getMessage(key, args));
}
}
@@ -0,0 +1,6 @@
CONFIGURATION_ERROR=Error configuring XML parser
LOAD_ERROR=Error loading {0}
PARSING_ERROR=Error parsing {0}
MULTIPLE_ALTERNATIVES=<alternatives> can only be specified once, but appears multiple times\: {0}
MULTIPLE_DECORATORS=<decorator> can only be specified once, but is specified multiple times\: {0}
MULTIPLE_INTERCEPTORS=<interceptor> can only be specified once, but it is specified multiple times\: {0}

0 comments on commit c3fe669

Please sign in to comment.