001: /*
002: Copyright (c) 2004-2005, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.model;
030:
031: import org.jibx.runtime.ITrackSource;
032: import org.jibx.runtime.ValidationException;
033:
034: /**
035: * Problem reported by model validation. Provides the details for a specific
036: * problem item.
037: *
038: * @author Dennis M. Sosnoski
039: * @version 1.0
040: */
041: public class ValidationProblem {
042: // severity levels
043: public static final int WARNING_LEVEL = 0;
044: public static final int ERROR_LEVEL = 1;
045: public static final int FATAL_LEVEL = 2;
046:
047: /** Problem severity level. */
048: private final int m_severity;
049:
050: /** Supplied problem description message. */
051: private final String m_message;
052:
053: /** Component that reported problem. */
054: private final Object m_component;
055:
056: /**
057: * Full constructor.
058: *
059: * @param level severity level of problem
060: * @param msg problem description
061: * @param obj source object for validation error (may be <code>null</code>
062: * if not specific to a particular component)
063: */
064: /*package*/ValidationProblem(int level, String msg, Object obj) {
065: m_severity = level;
066: m_message = msg;
067: m_component = obj;
068: }
069:
070: /**
071: * Create description text for a component of a binding definition.
072: *
073: * @param obj binding definition component
074: * @return description
075: */
076: public static String componentDescription(Object obj) {
077: StringBuffer buff = new StringBuffer();
078: if (obj instanceof ElementBase) {
079: buff.append(ElementBase.ELEMENT_NAMES[((ElementBase) obj)
080: .type()]);
081: buff.append(" element");
082: } else {
083: String cname = obj.getClass().getName();
084: int split = cname.lastIndexOf('.');
085: if (split >= 0) {
086: cname = cname.substring(split + 1);
087: }
088: buff.append(cname);
089: }
090: if (obj instanceof ITrackSource) {
091: buff.append(" at ");
092: buff.append(ValidationException.describe(obj));
093: } else {
094: buff.append(" at unknown location");
095: }
096: return buff.toString();
097: }
098:
099: /**
100: * Constructor using default (error) severity level.
101: *
102: * @param msg problem description
103: * @param obj source object for validation error
104: */
105: /*package*/ValidationProblem(String msg, Object obj) {
106: this (ERROR_LEVEL, msg, obj);
107: }
108:
109: /**
110: * Get the main binding definition item for the problem.
111: *
112: * @return element or attribute at root of problem
113: */
114: public Object getComponent() {
115: return m_component;
116: }
117:
118: /**
119: * Get problem description.
120: *
121: * @return problem description
122: */
123: public String getDescription() {
124: if (m_component == null) {
125: return m_message;
126: } else {
127: return m_message + "; on "
128: + componentDescription(m_component);
129: }
130: }
131:
132: /**
133: * Get problem severity level.
134: *
135: * @return severity level for problem
136: */
137: public int getSeverity() {
138: return m_severity;
139: }
140: }
|