1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.support.definition.support;
14
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLDecoder;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.xmlbeans.XmlObject;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.DocumentFragment;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.Node;
27
28 import com.eviware.soapui.config.DefinitionCacheConfig;
29 import com.eviware.soapui.config.DefinitionCacheTypeConfig;
30 import com.eviware.soapui.config.DefintionPartConfig;
31 import com.eviware.soapui.impl.support.AbstractInterface;
32 import com.eviware.soapui.impl.support.definition.DefinitionCache;
33 import com.eviware.soapui.impl.support.definition.DefinitionLoader;
34 import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
35 import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
36 import com.eviware.soapui.support.xml.XmlUtils;
37
38 public abstract class AbstractDefinitionCache<T extends AbstractInterface<?>> implements DefinitionCache
39 {
40 protected DefinitionCacheConfig definitionCache;
41 private T container;
42 private InterfaceDefinitionPart rootPart;
43 private List<InterfaceDefinitionPart> parts;
44
45 public AbstractDefinitionCache( DefinitionCacheConfig definitionCache, T container )
46 {
47 this.definitionCache = definitionCache;
48 this.container = container;
49
50 if( definitionCache == null )
51 definitionCache = reinit( container );
52 }
53
54 protected abstract DefinitionCacheConfig reinit( T owner );
55
56 public T getContainer()
57 {
58 return container;
59 }
60
61 public boolean validate()
62 {
63 if( definitionCache.getRootPart() == null )
64 return false;
65
66 if( definitionCache.sizeOfPartArray() == 0 )
67 return false;
68
69 return true;
70 }
71
72 public void importCache( DefinitionCache cache ) throws Exception
73 {
74 if( cache instanceof AbstractDefinitionCache<?> )
75 {
76 definitionCache = reinit( container );
77 definitionCache.set( ( ( AbstractDefinitionCache<?> )cache ).getConfig() );
78 initParts();
79 }
80 else
81 {
82 update( new InterfaceCacheDefinitionLoader( cache ) );
83 }
84 }
85
86 protected DefinitionCacheConfig getConfig()
87 {
88 return definitionCache;
89 }
90
91 public void update( DefinitionLoader loader ) throws Exception
92 {
93 definitionCache = reinit( container );
94
95 String baseUri = loader.getBaseURI();
96 definitionCache.setType( DefinitionCacheTypeConfig.TEXT );
97 Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
98 definitionCache.setRootPart( baseUri );
99
100 for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
101 {
102 DefintionPartConfig definitionPart = definitionCache.addNewPart();
103 String url = i.next();
104 definitionPart.setUrl( url );
105 XmlObject xmlObject = urls.get( url );
106 Node domNode = xmlObject.getDomNode();
107
108 if( domNode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE )
109 {
110 Node node = ( ( DocumentFragment )domNode ).getFirstChild();
111 if( node.getNodeType() == Node.TEXT_NODE )
112 {
113 domNode = XmlUtils.parseXml( node.getNodeValue() );
114 xmlObject = XmlObject.Factory.parse( domNode );
115 }
116 }
117
118 Element contentElement = ( ( Document )domNode ).getDocumentElement();
119
120 Node newDomNode = definitionPart.addNewContent().getDomNode();
121 newDomNode.appendChild( newDomNode.getOwnerDocument().createTextNode( xmlObject.toString() ) );
122 definitionPart.setType( contentElement.getNamespaceURI() );
123 }
124
125 initParts();
126 }
127
128 public List<InterfaceDefinitionPart> getDefinitionParts() throws Exception
129 {
130 if( parts == null )
131 {
132 initParts();
133 }
134
135 return parts;
136 }
137
138 private void initParts()
139 {
140 parts = new ArrayList<InterfaceDefinitionPart>();
141
142 List<DefintionPartConfig> partList = definitionCache.getPartList();
143 for( DefintionPartConfig part : partList )
144 {
145 try
146 {
147 boolean rootElement = URLDecoder.decode( part.getUrl() , "UTF-8").equals( URLDecoder.decode( definitionCache.getRootPart(), "UTF-8") );
148 ConfigInterfaceDefinitionPart configInterfaceDefinitionPart = new ConfigInterfaceDefinitionPart( part,
149 rootElement, definitionCache.getType() );
150 parts.add( configInterfaceDefinitionPart );
151
152 if( configInterfaceDefinitionPart.isRootPart() )
153 rootPart = configInterfaceDefinitionPart;
154 }
155 catch( UnsupportedEncodingException e )
156 {
157 e.printStackTrace();
158 }
159
160
161
162 }
163 }
164
165 public InterfaceDefinitionPart getRootPart()
166 {
167 if( parts == null )
168 initParts();
169
170 return rootPart;
171 }
172
173 public void clear()
174 {
175 definitionCache.setRootPart( null );
176
177 while( definitionCache.sizeOfPartArray() > 0 )
178 definitionCache.removePart( 0 );
179 }
180 }