Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   /*
   16   * Contributed by Dutta Satadip for adding functionality to retrieve xml validation errors
   17   * programatically.
   18   */
   19   package org.apache.xmlbeans;
   20   
   21   import java.util.List;
   22   
   23   import javax.xml.namespace.QName;
   24   import javax.xml.stream.Location;
   25   
   26   /**
   27    * The XmlValidationError class extends the {@link XmlError }XMLError class. The XML Validator contains
   28    * extra attributes that can be used to construct the XML validation error programatically.
   29    * <p>
   30    * To extract the validation error cast the errors to XmlValidationError instead of
   31    * XmlError for example:
   32    * <br/>
   33    * <pre>
   34    * xobj.validate(new XmlOptions().setErrorListener(errors))
   35    * for (Iterator it = errors.iterator(); it.hasNext(); )
   36    * {
   37    *      XmlError err = (XmlError)it.next());
   38    *      if (err instanceof XmlValidationError)
   39    *      {
   40    *          XmlValidationError validationError = (XmlValidationError) err;
   41    *     }
   42    * }
   43    *</pre>
   44    *
   45    *
   46    * Whenever an XmlValidationError is retrieved it will always be populated with
   47    * the following information:
   48    * <ul>
   49    * <li>Message</li>
   50    * <li>Severity</li>
   51    * <li>Error Type </li>
   52    * </ul>
   53    *
   54    * <p>
   55    *  The error type is very important because the other attributes of the
   56    * XMLValidationError are populated based on the error type.
   57    * </p>
   58    * if errortype == INCORRECT_ELEMENT then
   59    * <br/>
   60    *      offendingQName, badSchemaType will always be present, however expectedSchemaType and
   61    *      expectedQNames are available only if it is possible to determine them during vaildation.
   62    *<br/>
   63    *
   64    * <p>
   65    * if errortype == ELEMENT_NOT_ALLOWED then
   66    * <br/>
   67    *      badSchemaType will always be present, however expectedSchemaType and
   68    *      offendingQName are available only if it is possible to determine them during vaildation.
   69    * <br/>
   70    *
   71    * <p>
   72    * if errortype == INCORRECT_ATTRIBUTE then
   73    * <br/>
   74    *      offendingQName, badSchemaType will always be present
   75    * <br/>
   76    *
   77    * <p>
   78    * if errortype == ATTRIBUTE_TYPE_INVALID  then
   79    * <br/>
   80    *      no other addtional attributes are populated
   81    * <br/>
   82    *
   83    * <p>
   84    * if errortype == LIST_INVALID  then
   85    * <br/>
   86    *      expectedSchemaType will always be present
   87    * <br/>
   88    *
   89    * <p>
   90    * if errortype == UNION_INVALID  then
   91    * <br/>
   92    *      expectedSchemaType will always be present
   93    * <br/>
   94    *
   95    *
   96    * <p>
   97    * if errortype == NIL_ELEMENT  then
   98    * <br/>
   99    *      offendingQName, expectedSchemaType and badSchemaType  will always be present
  100    * <br/>
  101    *
  102    * <p>
  103    * if errortype == ELEMENT_TYPE_INVALID  then
  104    * <br/>
  105    *      offendingQName  will always be present, other attributes may be available
  106    * <br/>
  107    */
  108   
  109   public class XmlValidationError extends XmlError
  110   {
  111   
  112       public static final int INCORRECT_ELEMENT = 1;
  113       public static final int ELEMENT_NOT_ALLOWED = 2;
  114       public static final int ELEMENT_TYPE_INVALID = 3;
  115       public static final int NIL_ELEMENT = 4;
  116   
  117       public static final int INCORRECT_ATTRIBUTE = 1000;
  118       public static final int ATTRIBUTE_TYPE_INVALID = 1001;
  119   
  120       public static final int LIST_INVALID = 2000;
  121       public static final int UNION_INVALID = 3000;
  122   
  123       public static final int UNDEFINED = 10000;
  124   
  125       // QName of field in error.  can be null.
  126       private QName _fieldQName;
  127       private QName _offendingQName;
  128       private SchemaType _expectedSchemaType;
  129   
  130       private List _expectedQNames;
  131       private int _errorType;
  132   
  133       private SchemaType _badSchemaType;
  134   
  135       /**
  136        * The static factory methods should be used instead of
  137        * this constructor.
  138        */
  139       // KHK: remove this
  140       private XmlValidationError(String message, int severity,
  141          XmlCursor cursor, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  142          List expectedQNames, int errorType, SchemaType badSchemaType)
  143       {
  144           super(message, (String)null, severity, cursor);
  145   
  146           setFieldQName(fieldQName);
  147           setOffendingQName(offendingQname);
  148           setExpectedSchemaType(expectedSchemaType);
  149           setExpectedQNames(expectedQNames);
  150           setErrorType(errorType);
  151           setBadSchemaType(badSchemaType);
  152       }
  153   
  154       /**
  155        * The static factory methods should be used instead of
  156        * this constructor.
  157        */
  158       private XmlValidationError(String code, Object[] args, int severity,
  159          XmlCursor cursor, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  160          List expectedQNames, int errorType, SchemaType badSchemaType)
  161       {
  162           super(code, args, severity, cursor);
  163   
  164           setFieldQName(fieldQName);
  165           setOffendingQName(offendingQname);
  166           setExpectedSchemaType(expectedSchemaType);
  167           setExpectedQNames(expectedQNames);
  168           setErrorType(errorType);
  169           setBadSchemaType(badSchemaType);
  170       }
  171   
  172       /**
  173        * The static factory methods should be used instead of
  174        * this constructor.
  175        */
  176       // KHK: remove this
  177       private XmlValidationError(String message, int severity,
  178          Location loc, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  179          List expectedQNames, int errorType, SchemaType badSchemaType)
  180       {
  181           super(message, (String)null, severity, loc);
  182   
  183           setFieldQName(fieldQName);
  184           setOffendingQName(offendingQname);
  185           setExpectedSchemaType(expectedSchemaType);
  186           setExpectedQNames(expectedQNames);
  187           setErrorType(errorType);
  188           setBadSchemaType(badSchemaType);
  189       }
  190   
  191       /**
  192        * The static factory methods should be used instead of
  193        * this constructor.
  194        */
  195       private XmlValidationError(String code, Object[] args, int severity,
  196           Location loc, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  197           List expectedQNames, int errorType, SchemaType badSchemaType)
  198       {
  199           super(code, args, severity, loc);
  200   
  201           setFieldQName(fieldQName);
  202           setOffendingQName(offendingQname);
  203           setExpectedSchemaType(expectedSchemaType);
  204           setExpectedQNames(expectedQNames);
  205           setErrorType(errorType);
  206           setBadSchemaType(badSchemaType);
  207       }
  208   
  209       public static XmlValidationError forCursorWithDetails( String message, String code, Object[] args, int severity,
  210          XmlCursor cursor, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  211          List expectedQNames, int errorType, SchemaType badSchemaType)
  212       {
  213           if (code == null)
  214               return new XmlValidationError(message, severity, cursor, fieldQName, offendingQname,
  215                   expectedSchemaType, expectedQNames, errorType, badSchemaType);
  216           else
  217               return new XmlValidationError(code, args, severity, cursor, fieldQName, offendingQname,
  218                   expectedSchemaType, expectedQNames, errorType, badSchemaType);
  219       }
  220   
  221       public static XmlValidationError forLocationWithDetails( String message, String code, Object[] args, int severity,
  222           Location location, QName fieldQName, QName offendingQname, SchemaType expectedSchemaType,
  223           List expectedQNames, int errorType, SchemaType badSchemaType)
  224       {
  225           if (code == null)
  226               return new XmlValidationError(message, severity, location, fieldQName, offendingQname,
  227                   expectedSchemaType, expectedQNames, errorType, badSchemaType);
  228           else
  229               return new XmlValidationError(code, args, severity, location, fieldQName, offendingQname,
  230                   expectedSchemaType, expectedQNames, errorType, badSchemaType);
  231       }
  232   
  233       public String getMessage()
  234       {
  235           if (_fieldQName != null)
  236           {
  237               String msg = super.getMessage();
  238               StringBuffer sb = new StringBuffer(msg.length() + 100);
  239   
  240               sb.append(msg);
  241   
  242               sb.append(" in element ");
  243               sb.append(_fieldQName.getLocalPart());
  244               if (_fieldQName.getNamespaceURI() != null && _fieldQName.getNamespaceURI().length() != 0)
  245                   sb.append('@').append(_fieldQName.getNamespaceURI());
  246   
  247               return sb.toString();
  248           }
  249           else
  250               return super.getMessage();
  251       }
  252   
  253       public SchemaType getBadSchemaType()
  254       {
  255           return _badSchemaType;
  256       }
  257   
  258       public void setBadSchemaType(SchemaType _badSchemaType)
  259       {
  260           this._badSchemaType = _badSchemaType;
  261       }
  262   
  263       public int getErrorType()
  264       {
  265           return _errorType;
  266       }
  267   
  268       public void setErrorType(int _errorType)
  269       {
  270           this._errorType = _errorType;
  271       }
  272   
  273       public List getExpectedQNames()
  274       {
  275           return _expectedQNames;
  276       }
  277   
  278       public void setExpectedQNames(List _expectedQNames)
  279       {
  280           this._expectedQNames = _expectedQNames;
  281       }
  282   
  283       public QName getFieldQName()
  284       {
  285           return _fieldQName;
  286       }
  287   
  288       public void setFieldQName(QName _fieldQName)
  289       {
  290           this._fieldQName = _fieldQName;
  291       }
  292   
  293       public QName getOffendingQName()
  294       {
  295           return _offendingQName;
  296       }
  297   
  298       public void setOffendingQName(QName _offendingQName)
  299       {
  300           this._offendingQName = _offendingQName;
  301       }
  302   
  303       public SchemaType getExpectedSchemaType()
  304       {
  305           return _expectedSchemaType;
  306       }
  307   
  308       public void setExpectedSchemaType(SchemaType _expectedSchemaType)
  309       {
  310           this._expectedSchemaType = _expectedSchemaType;
  311       }
  312   }

Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]