View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.support.definition.support;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.xmlbeans.XmlObject;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.DocumentFragment;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.Node;
25  
26  import com.eviware.soapui.config.DefinitionCacheConfig;
27  import com.eviware.soapui.config.DefinitionCacheTypeConfig;
28  import com.eviware.soapui.config.DefintionPartConfig;
29  import com.eviware.soapui.impl.support.AbstractInterface;
30  import com.eviware.soapui.impl.support.definition.DefinitionCache;
31  import com.eviware.soapui.impl.support.definition.DefinitionLoader;
32  import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
33  import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
34  import com.eviware.soapui.support.xml.XmlUtils;
35  
36  public abstract class AbstractDefinitionCache<T extends AbstractInterface> implements DefinitionCache
37  {
38  	protected DefinitionCacheConfig definitionCache;
39  	private T container;
40  	private InterfaceDefinitionPart rootPart;
41  	private List<InterfaceDefinitionPart> parts;
42  
43  	public AbstractDefinitionCache( DefinitionCacheConfig definitionCache, T container )
44  	{
45  		this.definitionCache = definitionCache;
46  		this.container = container;
47  
48  		if( definitionCache == null )
49  			definitionCache = reinit( container );
50  	}
51  
52  	protected abstract DefinitionCacheConfig reinit( T owner );
53  
54  	public T getContainer()
55  	{
56  		return container;
57  	}
58  
59  	public boolean validate()
60  	{
61  		if( definitionCache.getRootPart() == null )
62  			return false;
63  
64  		if( definitionCache.sizeOfPartArray() == 0 )
65  			return false;
66  
67  		return true;
68  	}
69  
70  	public void importCache( DefinitionCache cache ) throws Exception
71  	{
72  		if( cache instanceof AbstractDefinitionCache )
73  		{
74  			definitionCache = reinit( container );
75  			definitionCache.set( ( ( AbstractDefinitionCache )cache ).getConfig() );
76  			initParts();
77  		}
78  		else
79  		{
80  			update( new InterfaceCacheDefinitionLoader( cache ) );
81  		}
82  	}
83  
84  	protected DefinitionCacheConfig getConfig()
85  	{
86  		return definitionCache;
87  	}
88  
89  	public void update( DefinitionLoader loader ) throws Exception
90  	{
91  		definitionCache = reinit( container );
92  
93  		definitionCache.setType( DefinitionCacheTypeConfig.TEXT );
94  
95  		Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
96  		definitionCache.setRootPart( loader.getFirstNewURI() );
97  
98  		for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
99  		{
100 			DefintionPartConfig definitionPart = definitionCache.addNewPart();
101 			String url = i.next();
102 			definitionPart.setUrl( url );
103 			XmlObject xmlObject = urls.get( url );
104 			Node domNode = xmlObject.getDomNode();
105 
106 			if( domNode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE )
107 			{
108 				Node node = ( ( DocumentFragment )domNode ).getFirstChild();
109 				if( node.getNodeType() == Node.TEXT_NODE )
110 				{
111 					domNode = XmlUtils.parseXml( node.getNodeValue() );
112 					xmlObject = XmlObject.Factory.parse( domNode );
113 				}
114 			}
115 
116 			Element contentElement = ( ( Document )domNode ).getDocumentElement();
117 
118 			Node newDomNode = definitionPart.addNewContent().getDomNode();
119 			newDomNode.appendChild( newDomNode.getOwnerDocument().createTextNode( xmlObject.toString() ) );
120 			definitionPart.setType( contentElement.getNamespaceURI() );
121 		}
122 
123 		initParts();
124 	}
125 
126 	public List<InterfaceDefinitionPart> getDefinitionParts() throws Exception
127 	{
128 		if( parts == null )
129 		{
130 			initParts();
131 		}
132 
133 		return parts;
134 	}
135 
136 	private void initParts()
137 	{
138 		parts = new ArrayList<InterfaceDefinitionPart>();
139 
140 		List<DefintionPartConfig> partList = definitionCache.getPartList();
141 		for( DefintionPartConfig part : partList )
142 		{
143 			ConfigInterfaceDefinitionPart configInterfaceDefinitionPart = new ConfigInterfaceDefinitionPart( part, part
144 					.getUrl().equals( definitionCache.getRootPart() ), definitionCache.getType() );
145 			parts.add( configInterfaceDefinitionPart );
146 
147 			if( configInterfaceDefinitionPart.isRootPart() )
148 				rootPart = configInterfaceDefinitionPart;
149 		}
150 	}
151 
152 	public InterfaceDefinitionPart getRootPart()
153 	{
154 		if( parts == null )
155 			initParts();
156 
157 		return rootPart;
158 	}
159 
160 	public void clear()
161 	{
162 		definitionCache.setRootPart( null );
163 
164 		while( definitionCache.sizeOfPartArray() > 0 )
165 			definitionCache.removePart( 0 );
166 	}
167 }