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