📄 beantype.java
字号:
if (t != null) { t.writeObject(object, writer, context); } } } private MessageWriter getWriter(MessageWriter writer, QName name, Type type) { MessageWriter cwriter; if (type.isAbstract()) { cwriter = writer.getElementWriter(name); } else { cwriter = writer.getElementWriter(name); } return cwriter; } protected Object readProperty(Object object, QName name) { try { PropertyDescriptor desc = getTypeInfo().getPropertyDescriptorFromMappedName(name); Method m = desc.getReadMethod(); if (m == null) throw new XFireFault("No read method for property " + name + " in class " + object.getClass().getName(), XFireFault.SENDER); return m.invoke(object, new Object[0]); } catch (Exception e) { throw new XFireRuntimeException("Couldn't get property " + name + " from bean " + object, e); } } /** * @see org.codehaus.xfire.aegis.type.Type#writeSchema(org.jdom.Element) */ public void writeSchema(Element root) { BeanTypeInfo info = getTypeInfo(); Element complex = new Element("complexType", SoapConstants.XSD_PREFIX, SoapConstants.XSD); complex.setAttribute(new Attribute("name", getSchemaType().getLocalPart())); root.addContent(complex); Type sooperType = getSuperType(); /* * See Java Virtual Machine specification: * http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734 */ if (((info.getTypeClass().getModifiers() & Modifier.ABSTRACT) != 0) && !info.getTypeClass().isInterface()) { complex.setAttribute(new Attribute("abstract", "true")); } if (info.isExtension() && sooperType != null) { Element complexContent = new Element("complexContent", SoapConstants.XSD_PREFIX, SoapConstants.XSD); complex.addContent(complexContent); complex = complexContent; } /* * Decide if we're going to extend another type. If we are going to * defer, then make sure that we extend the type for our superclass. */ boolean isExtension = info.isExtension(); Element dummy = complex; if (isExtension && sooperType != null) { Element extension = new Element("extension", SoapConstants.XSD_PREFIX, SoapConstants.XSD); complex.addContent(extension); QName baseType = sooperType.getSchemaType(); extension.setAttribute(new Attribute("base", getNameWithPrefix2(root, baseType .getNamespaceURI(), baseType.getLocalPart()))); dummy = extension; } Element seq = null; // Write out schema for elements for (Iterator itr = info.getElements(); itr.hasNext();) { QName name = (QName) itr.next(); if (isExtension) { PropertyDescriptor pd = info.getPropertyDescriptorFromMappedName(name); assert pd.getReadMethod() != null && pd.getWriteMethod() != null; if (pd.getReadMethod().getDeclaringClass() != info.getTypeClass()) { continue; } } if (seq == null) { seq = new Element("sequence", SoapConstants.XSD_PREFIX, SoapConstants.XSD); dummy.addContent(seq); } Element element = new Element("element", SoapConstants.XSD_PREFIX, SoapConstants.XSD); seq.addContent(element); Type type = getType(info, name); String nameNS = name.getNamespaceURI(); String nameWithPrefix = getNameWithPrefix(root, nameNS, name.getLocalPart()); String prefix = NamespaceHelper.getUniquePrefix((Element) root.getParent(), type .getSchemaType().getNamespaceURI()); writeTypeReference(name, nameWithPrefix, element, type, prefix); } /** * if future proof then add <xsd:any/> element */ if (info.isExtensibleElements()) { if (seq == null) { seq = new Element("sequence", SoapConstants.XSD_PREFIX, SoapConstants.XSD); dummy.addContent(seq); } seq.addContent(createAnyElement()); } // Write out schema for attributes for (Iterator itr = info.getAttributes(); itr.hasNext();) { QName name = (QName) itr.next(); Element element = new Element("attribute", SoapConstants.XSD_PREFIX, SoapConstants.XSD); dummy.addContent(element); Type type = getType(info, name); String nameNS = name.getNamespaceURI(); String nameWithPrefix = getNameWithPrefix(root, nameNS, name.getLocalPart()); String prefix = NamespaceHelper.getUniquePrefix((Element) root.getParent(), type .getSchemaType().getNamespaceURI()); element.setAttribute(new Attribute("name", nameWithPrefix)); element.setAttribute(new Attribute("type", prefix + ':' + type.getSchemaType().getLocalPart())); } /** * If extensible attributes then add <xsd:anyAttribute/> */ if (info.isExtensibleAttributes()) { dummy.addContent(createAnyAttribute()); } } private String getNameWithPrefix(Element root, String nameNS, String localName) { if (!nameNS.equals(getSchemaType().getNamespaceURI())) { String prefix = NamespaceHelper.getUniquePrefix((Element) root.getParent(), nameNS); if (prefix == null || prefix.length() == 0) prefix = NamespaceHelper.getUniquePrefix(root, nameNS); return prefix + ":" + localName; } return localName; } private String getNameWithPrefix2(Element root, String nameNS, String localName) { String prefix = NamespaceHelper.getUniquePrefix((Element) root.getParent(), nameNS); if (prefix == null || prefix.length() == 0) prefix = NamespaceHelper.getUniquePrefix(root, nameNS); return prefix + ":" + localName; } private Type getType(BeanTypeInfo info, QName name) { Type type = info.getType(name); if (type == null) { throw new NullPointerException("Couldn't find type for" + name + " in class " + getTypeClass().getName()); } return type; } private void writeTypeReference(QName name, String nameWithPrefix, Element element, Type type, String prefix) { if (type.isAbstract()) { element.setAttribute(new Attribute("name", nameWithPrefix)); element.setAttribute(new Attribute("type", prefix + ':' + type.getSchemaType().getLocalPart())); int minOccurs = getTypeInfo().getMinOccurs(name); if (minOccurs != 1) { element.setAttribute(new Attribute("minOccurs", new Integer(minOccurs).toString())); } if (getTypeInfo().isNillable(name)) { element.setAttribute(new Attribute("nillable", "true")); } } else { element.setAttribute(new Attribute("ref", prefix + ':' + type.getSchemaType().getLocalPart())); } } public void setTypeClass(Class typeClass) { super.setTypeClass(typeClass); isInterface = typeClass.isInterface(); isException = Exception.class.isAssignableFrom(typeClass); } /** * We need to write a complex type schema for Beans, so return true. * * @see org.codehaus.xfire.aegis.type.Type#isComplex() */ public boolean isComplex() { return true; } public Set getDependencies() { Set deps = new HashSet(); BeanTypeInfo info = getTypeInfo(); for (Iterator itr = info.getAttributes(); itr.hasNext();) { QName name = (QName) itr.next(); deps.add(info.getType(name)); } for (Iterator itr = info.getElements(); itr.hasNext();) { QName name = (QName) itr.next(); if (info.isExtension() && info.getPropertyDescriptorFromMappedName(name).getReadMethod() .getDeclaringClass() != info.getTypeClass()) continue; deps.add(info.getType(name)); } /* * Automagically add chain of superclasses *if* this is an an extension. */ if (info.isExtension()) { Type sooperType = getSuperType(); if (sooperType != null) { deps.add(sooperType); } } return deps; } private BeanType getBeanTypeWithProperty(QName name) { BeanType sooper = this; Type type = null; while (type == null && sooper != null) { type = sooper.getTypeInfo().getType(name); if (type == null) sooper = sooper.getSuperType(); } return (BeanType) sooper; } private BeanType getSuperType() { BeanTypeInfo info = getTypeInfo(); Class c = info.getTypeClass().getSuperclass(); /* * Don't dig any deeper than Object or Exception */ if (c != null && c != Object.class && c != Exception.class && c != RuntimeException.class) { TypeMapping tm = info.getTypeMapping(); BeanType superType = (BeanType) tm.getType(c); if (superType == null) { superType = (BeanType) getTypeMapping().getTypeCreator().createType(c); Class cParent = c.getSuperclass(); if (cParent != null && cParent != Object.class) { superType.getTypeInfo().setExtension(true); } tm.register(superType); } return superType; } else { return null; } } public BeanTypeInfo getTypeInfo() { if (_info == null) { _info = createTypeInfo(); } // Delay initialization so things work in recursive scenarios _info.initialize(); return _info; } public BeanTypeInfo createTypeInfo() { BeanTypeInfo info = new BeanTypeInfo(getTypeClass(), getSchemaType().getNamespaceURI()); info.setTypeMapping(getTypeMapping()); info.initialize(); return info; } /** * Create an element to represent any future elements that might get added * to the schema <xsd:any minOccurs="0" maxOccurs="unbounded"/> * * @return */ private Element createAnyElement() { Element result = new Element("any", SoapConstants.XSD_PREFIX, SoapConstants.XSD); result.setAttribute(new Attribute("minOccurs", "0")); result.setAttribute(new Attribute("maxOccurs", "unbounded")); return result; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append(getClass().getName()); sb.append(": [class="); Class c = getTypeClass(); sb.append((c == null) ? ("<null>") : (c.getName())); sb.append(",\nQName="); QName q = getSchemaType(); sb.append((q == null) ? ("<null>") : (q.toString())); sb.append(",\ninfo="); sb.append(getTypeInfo().toString()); sb.append("]"); return sb.toString(); } /** * Create an element to represent any future attributes that might get added * to the schema <xsd:anyAttribute/> * * @return */ private Element createAnyAttribute() { Element result = new Element("anyAttribute", SoapConstants.XSD_PREFIX, SoapConstants.XSD); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -