1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wadl.inference.schema.particles;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import javax.xml.namespace.QName;
19
20 import org.apache.xmlbeans.XmlException;
21
22 import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
23 import com.eviware.soapui.impl.wadl.inference.schema.Context;
24 import com.eviware.soapui.impl.wadl.inference.schema.Particle;
25 import com.eviware.soapui.impl.wadl.inference.schema.Schema;
26 import com.eviware.soapui.impl.wadl.inference.schema.Settings;
27 import com.eviware.soapui.impl.wadl.inference.schema.Type;
28 import com.eviware.soapui.inferredSchema.AttributeParticleConfig;
29 import com.eviware.soapui.inferredSchema.MapEntryConfig;
30
31 /***
32 * Represents an xs:attribute, with a name, a type, etc.
33 *
34 * @author Dain Nilsson
35 */
36 public class AttributeParticle implements Particle
37 {
38 private String name;
39 private Schema schema;
40 private Type type;
41 private Map<String, String> attributes;
42
43 public AttributeParticle( Schema schema, String name )
44 {
45 this.schema = schema;
46 this.name = name;
47 type = Type.Factory.newType( schema );
48 attributes = new HashMap<String, String>();
49 }
50
51 public AttributeParticle( AttributeParticleConfig xml, Schema schema )
52 {
53 this.schema = schema;
54 name = xml.getName();
55 type = Type.Factory.parse( xml.getType(), schema );
56 attributes = new HashMap<String, String>();
57 for( MapEntryConfig entry : xml.getAttributeList() )
58 {
59 attributes.put( entry.getKey(), entry.getValue() );
60 }
61 }
62
63 public AttributeParticleConfig save()
64 {
65 AttributeParticleConfig xml = AttributeParticleConfig.Factory.newInstance();
66 xml.setName( name );
67 for( Map.Entry<String, String> entry : attributes.entrySet() )
68 {
69 MapEntryConfig mapEntry = xml.addNewAttribute();
70 mapEntry.setKey( entry.getKey() );
71 mapEntry.setValue( entry.getValue() );
72 }
73 xml.setType( type.save() );
74 return xml;
75 }
76
77 public String getAttribute( String key )
78 {
79 String value = attributes.get( key );
80 if( value == null )
81 value = "";
82 return value;
83 }
84
85 public QName getName()
86 {
87 return new QName( schema.getNamespace(), name );
88 }
89
90 public Type getType()
91 {
92 return type;
93 }
94
95 public void setAttribute( String key, String value )
96 {
97 attributes.put( key, value );
98 }
99
100 public void setType( Type type )
101 {
102 this.type = type;
103 }
104
105 public void validate( Context context ) throws XmlException
106 {
107 context.getCursor().push();
108 Type newType = type.validate( context );
109 if( newType != type )
110 {
111 String problem = "Illegal value for attribute '" + name + "' with type '" + type.getName() + "'.";
112 if( context.getHandler().callback( ConflictHandler.Event.MODIFICATION, ConflictHandler.Type.ATTRIBUTE,
113 getName(), context.getPath(), "Illegal value." ) )
114 {
115 type = newType;
116 context.getCursor().pop();
117 validate( context );
118 return;
119 }
120 else
121 throw new XmlException( problem );
122 }
123 context.getCursor().pop();
124 }
125
126 @Override
127 public String toString()
128 {
129 StringBuilder s = new StringBuilder( "<" + schema.getPrefixForNamespace( Settings.xsdns ) + ":" + getPType()
130 + " name=\"" + name + "\" type=\"" );
131 if( type.getSchema() != schema )
132 s.append( schema.getPrefixForNamespace( type.getSchema().getNamespace() ) + ":" );
133 s.append( type.getName() + "\"" );
134 for( Map.Entry<String, String> entry : attributes.entrySet() )
135 s.append( " " + entry.getKey() + "=\"" + entry.getValue() + "\"" );
136 s.append( "/>" );
137 return s.toString();
138 }
139
140 public Particle.ParticleType getPType()
141 {
142 return Particle.ParticleType.ATTRIBUTE;
143 }
144
145 }