1- /**
2- * http://surenpi.com
1+ /*
2+ * Copyright 2002-2007 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
315 */
16+
417package org .suren .autotest .web .framework .data ;
518
619import java .io .File ;
720import java .io .IOException ;
821import java .io .InputStream ;
22+ import java .io .UnsupportedEncodingException ;
923import java .lang .reflect .InvocationTargetException ;
1024import java .lang .reflect .Method ;
1125import java .net .URL ;
26+ import java .net .URLDecoder ;
1227import java .util .HashMap ;
1328import java .util .List ;
1429import java .util .Map ;
2641import org .slf4j .LoggerFactory ;
2742import org .springframework .beans .BeanUtils ;
2843import org .springframework .beans .factory .annotation .Autowired ;
44+ import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
45+ import org .springframework .context .annotation .Scope ;
2946import org .springframework .stereotype .Component ;
3047import org .suren .autotest .web .framework .core .ui .AbstractElement ;
3148import org .suren .autotest .web .framework .core .ui .Button ;
4259 * @date Jul 17, 2016 8:56:51 AM
4360 */
4461@ Component ("xml_data_source" )
45- public class XmlDataSource implements DataSource
62+ @ Scope (value = ConfigurableBeanFactory .SCOPE_PROTOTYPE )
63+ public class XmlDataSource implements DataSource , DynamicDataSource
4664{
4765 private static final Logger LOGGER = LoggerFactory .getLogger (XmlDataSource .class );
4866
67+ public static final String NS_URI = "http://datasource.surenpi.com" ;
68+
4969 private Page page ;
70+ private URL url ;
5071 private Map <String , Object > globalMap = new HashMap <String , Object >();
5172
5273 @ Autowired
@@ -87,7 +108,7 @@ private void parse(Document doc, int row)
87108 {
88109 String pageClass = page .getClass ().getName ();
89110 SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext ();
90- simpleNamespaceContext .addNamespace ("ns" , "http://datasource.surenpi.com" );
111+ simpleNamespaceContext .addNamespace ("ns" , NS_URI );
91112
92113 XPath xpath = new DefaultXPath ("/ns:dataSources" );
93114 xpath .setNamespaceContext (simpleNamespaceContext );
@@ -139,6 +160,7 @@ public void visit(Element node)
139160 DynamicData dynamicData = getDynamicDataByType (type );
140161 if (dynamicData != null )
141162 {
163+ dynamicData .setData (globalMap );
142164 value = dynamicData .getValue (value );
143165 }
144166 else
@@ -259,10 +281,12 @@ private DynamicData getDynamicDataByType(String type)
259281 public boolean loadData (DataResource resource , int row , Page page )
260282 {
261283 this .page = page ;
262- URL url = null ;
263- try {
284+ try
285+ {
264286 url = resource .getUrl ();
265- } catch (IOException e ) {
287+ }
288+ catch (IOException e )
289+ {
266290 LOGGER .error (e .getMessage (), e );
267291 }
268292
@@ -288,6 +312,7 @@ public boolean loadData(DataResource resource, int row, Page page)
288312 /**
289313 * @return the globalMap
290314 */
315+ @ Override
291316 public Map <String , Object > getGlobalMap ()
292317 {
293318 return globalMap ;
@@ -296,9 +321,27 @@ public Map<String, Object> getGlobalMap()
296321 /**
297322 * @param globalMap the globalMap to set
298323 */
324+ @ Override
299325 public void setGlobalMap (Map <String , Object > globalMap )
300326 {
301327 this .globalMap = globalMap ;
302328 }
303329
330+ @ Override
331+ public String getName ()
332+ {
333+ try
334+ {
335+ File file = new File (URLDecoder .decode (this .url .getFile (), "utf-8" ));
336+
337+ return file .getName ();
338+ }
339+ catch (UnsupportedEncodingException e )
340+ {
341+ e .printStackTrace ();
342+ }
343+
344+ return this .url .getPath ();
345+ }
346+
304347}
0 commit comments