View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.api.types;
36  
37  import java.util.ArrayList;
38  import java.util.Observable;
39  
40  import org.apache.log4j.Logger;
41  import org.apache.xmlbeans.XmlError;
42  import org.apache.xmlbeans.XmlObject;
43  import org.apache.xmlbeans.XmlOptions;
44  import org.ogf.graap.wsag.api.WsagConstants;
45  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
46  import org.ogf.schemas.graap.wsAgreement.AgreementType;
47  
48  /**
49   * WSAGXmlType
50   * 
51   * @author Oliver Waeldrich
52   */
53  public abstract class WSAGXmlType extends Observable
54  {
55  
56      private static final Logger LOG = Logger.getLogger( WSAGXmlType.class );
57  
58      /**
59       * Validates the internal XML object representation.
60       * 
61       * @return true, if the validation process succeeded, false otherwise.
62       */
63      public abstract boolean validate();
64  
65      /**
66       * Validates an XML object against its type definition.
67       * 
68       * @param object
69       *            the object to validate
70       * @return true, if the validation process succeeded, false otherwise.
71       */
72      public boolean validate( XmlObject object )
73      {
74          XmlOptions options = new XmlOptions();
75          ArrayList<XmlError> list = new ArrayList<XmlError>();
76          options.setErrorListener( list );
77  
78          if ( !object.validate( options ) )
79          {
80              for ( int i = 0; i < list.size(); i++ )
81              {
82                  LOG.error( list.get( i ).getMessage() );
83              }
84  
85              return false;
86          }
87  
88          return true;
89      }
90  
91      /**
92       * Processes an agreement template and strips off the template specific elements, namely the template id
93       * and creation constraint section.
94       * 
95       * @param template
96       *            the template to process
97       * @return the processed agreement type
98       */
99      protected AgreementType processTemplate( AgreementTemplateType template )
100     {
101         template = (AgreementTemplateType) template.copy();
102 
103         //
104         // remove constraint section, if present, and unset the template id
105         //
106         XmlObject[] constraints = template.selectChildren( WsagConstants.CREATION_CONSTRAINT_ELEMENT_QNAME );
107         for ( int i = 0; i < constraints.length; i++ )
108         {
109             template.getDomNode().removeChild( constraints[i].getDomNode() );
110         }
111 
112         if ( template.isSetTemplateId() )
113         {
114             template.unsetTemplateId();
115         }
116 
117         return (AgreementType) template.changeType( AgreementType.type );
118     }
119 
120 }