classskeleton.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,047 行 · 第 1/2 页

JAVA
1,047
字号
  {    XmlMapping mapping = XmlMapping.newInstance(_context, accessor);    if (mapping instanceof XmlValueMapping) {      if (_value != null)        throw new JAXBException(L.l("Cannot have two @XmlValue annotated fields or properties"));      if (_elementMappings.size() > 0) {        // in case of propOrder & XmlValue        if (_elementMappings.size() != 1 || _elementMappings.get(0) != null)          throw new JAXBException(L.l("Cannot have both @XmlValue and elements in a JAXB element (e.g. {0})", _elementMappings.get(0)));        _elementMappings.clear();      }      _value = (XmlValueMapping) mapping;    }    else if (mapping instanceof AttributeMapping) {      mapping.putQNames(_attributeQNameToMappingMap);      _attributeMappings.add((AttributeMapping) mapping);    }    else if (mapping instanceof AnyAttributeMapping) {      if (_anyAttributeMapping != null)        throw new JAXBException(L.l("Cannot have two fields or properties with @XmlAnyAttribute annotation"));      _anyAttributeMapping = (AnyAttributeMapping) mapping;      _attributeMappings.add(mapping);    }    else if ((mapping instanceof ElementMapping) ||             (mapping instanceof ElementRefMapping) ||             (mapping instanceof ElementsMapping)) {      if (_value != null)        throw new JAXBException(L.l("{0}: Cannot have both @XmlValue and elements in a JAXB element", _class.getName()));      if (mapping.getAccessor().getOrder() >= 0)        _elementMappings.set(mapping.getAccessor().getOrder(), mapping);      else        _elementMappings.add(mapping);    }    else if (mapping instanceof AnyElementMapping) {      if (_anyElementMapping != null)        throw new JAXBException(L.l("{0}: Cannot have two @XmlAnyElement annotations in a single class", _class.getName()));      _anyElementMapping = (AnyElementMapping) mapping;    }    else {      throw new RuntimeException(L.l("Unknown mapping type {0}", mapping.getClass()));    }  }  private XmlMapping getElementMapping(QName q)    throws JAXBException  {    XmlMapping mapping = _elementQNameToMappingMap.get(q);    if (mapping != null)      return mapping;    if (_anyElementMapping != null)      return _anyElementMapping;    if (_parent != null)      return _parent.getElementMapping(q);    return null;  }  public XmlMapping getAttributeMapping(QName q)    throws JAXBException  {    XmlMapping mapping = _attributeQNameToMappingMap.get(q);    if (mapping != null)      return mapping;    if (_anyAttributeMapping != null)      return _anyAttributeMapping;    if (_parent != null)      return _parent.getAttributeMapping(q);    return null;  }  public QName getElementName()  {    if (_elementName != null)      return _elementName;    else      return _typeName;  }  public void setElementName(QName elementName)  {    _elementName = elementName;  }  public QName getTypeName()  {    return _typeName;  }  public void setCreateMethod(Method createMethod, Object factory)  {    _createMethod = createMethod;    _factory = factory;  }  public C newInstance()    throws JAXBException  {    try {      if (_createMethod != null && _factory != null) {        return (C) _createMethod.invoke(_factory);      }      else {        // XXX move into constructor        XmlType xmlType = getXmlType();        if (xmlType != null) {          Class factoryClass = xmlType.factoryClass();          if (xmlType.factoryClass() == XmlType.DEFAULT.class)            factoryClass = _class;          if (! "".equals(xmlType.factoryMethod())) {            Method m =               factoryClass.getMethod(xmlType.factoryMethod(), NO_PARAMS);            if (! Modifier.isStatic(m.getModifiers()))              throw new JAXBException(L.l("Factory method not static"));            return (C) m.invoke(null);          }        }        Constructor con = _class.getConstructor(NO_PARAMS);        return (C)con.newInstance(NO_ARGS);      }    }    catch (JAXBException e) {      throw e;    }    catch (Exception e) {      throw new JAXBException(e);    }  }  public XmlType getXmlType()  {    return (XmlType)_class.getAnnotation(XmlType.class);  }  public Object read(Unmarshaller u, XMLStreamReader in)    throws IOException, XMLStreamException, JAXBException  {    try {      C ret = null;      String nil = in.getAttributeValue(W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil");      if (! "true".equals(nil))        ret = newInstance();      if (_locationAccessor != null)        _locationAccessor.set(ret, in.getLocation());      if (_beforeUnmarshal != null)        _beforeUnmarshal.invoke(ret, u, null);      if (u.getListener() != null)        u.getListener().beforeUnmarshal(ret, null);      if (_value != null) {        _value.read(u, in, ret, this);      }      else {        // process the attributes        for (int i = 0; i < in.getAttributeCount(); i++) {          QName attributeName = in.getAttributeName(i);          XmlMapping mapping = getAttributeMapping(attributeName);          if (mapping == null)            throw new UnmarshalException(L.l("Attribute {0} not found in {1}",                                              attributeName, getType()));          mapping.readAttribute(in, i, ret);        }        int i = 0;        in.nextTag();        while (in.getEventType() == in.START_ELEMENT) {          XmlMapping mapping = getElementMapping(in.getName());          if (mapping == null) {            throw new UnmarshalException(L.l("Child <{0}> not found in {1}",                                              in.getName(), getType()));          }          if (! mapping.getAccessor().checkOrder(i++, u.getEventHandler())) {            throw new UnmarshalException(L.l("Child <{0}> misordered in {1}",                                              in.getName(), getType()));          }          mapping.read(u, in, ret);        }        // essentially a nextTag() that handles end of document gracefully        while (in.hasNext()) {          in.next();          if (in.getEventType() == in.START_ELEMENT ||              in.getEventType() == in.END_ELEMENT)            break;        }      }      if (_afterUnmarshal != null)        _afterUnmarshal.invoke(ret, u, null);      if (u.getListener() != null)        u.getListener().afterUnmarshal(ret, null);      return ret;    }    catch (InvocationTargetException e) {      if (e.getTargetException() != null)        throw new UnmarshalException(e.getTargetException());      throw new UnmarshalException(e);    }    catch (IllegalAccessException e) {      throw new UnmarshalException(e);    }  }  public Object bindFrom(BinderImpl binder, Object existing, NodeIterator node)    throws IOException, JAXBException  {    Node root = node.getNode();    C ret = (C) existing;        if (ret == null)      ret = newInstance();    if (_value != null) {       _value.bindFrom(binder, node, ret);    }    else {      int i = 0;      Node child = node.firstChild();      while (child != null) {        if (child.getNodeType() == Node.ELEMENT_NODE) {          QName name = JAXBUtil.qnameFromNode(child);          XmlMapping mapping = getElementMapping(name);          if (mapping == null)            throw new UnmarshalException(L.l("Child <{0}> not found", name));          if (! mapping.getAccessor().checkOrder(i++, binder.getEventHandler()))            throw new UnmarshalException(L.l("Child <{0}> misordered", name));          mapping.bindFrom(binder, node, ret);        }        child = node.nextSibling();      }    }        node.setNode(root);    binder.bind(ret, root);    return ret;  }    public void write(Marshaller m, XMLStreamWriter out,                    Object obj, Namer namer,                     ArrayList<XmlMapping> attributes)    throws IOException, XMLStreamException, JAXBException  {    if (obj == null)      return;    try {      if (_beforeMarshal != null)        _beforeMarshal.invoke(obj, m);      if (m.getListener() != null)        m.getListener().beforeMarshal(obj);      QName tagName = null;            if (namer != null)        tagName = namer.getQName(obj);      if (tagName == null)        tagName = _elementName;      if (_value != null) {        _value.setQName(tagName);        _value.write(m, out, obj, _attributeMappings);      }      else {        if (tagName.getNamespaceURI() == null ||            "".equals(tagName.getNamespaceURI()))          out.writeStartElement(tagName.getLocalPart());        else if (tagName.getPrefix() == null ||                 "".equals(tagName.getPrefix()))          out.writeStartElement(tagName.getNamespaceURI(),                                tagName.getLocalPart());        else          out.writeStartElement(tagName.getPrefix(),                                tagName.getLocalPart(),                                tagName.getNamespaceURI());        if (attributes != null) {          for (int i = 0; i < attributes.size(); i++)            attributes.get(i).write(m, out, obj);        }        for (XmlMapping mapping : _attributeMappings)          mapping.write(m, out, obj);        for (XmlMapping mapping : _elementMappings)          mapping.write(m, out, obj);        if (_anyElementMapping != null) // XXX ordering!          _anyElementMapping.write(m, out, obj);                out.writeEndElement();      }            if (_afterMarshal != null)        _afterMarshal.invoke(obj, m);      if (m.getListener() != null)        m.getListener().afterMarshal(obj);    }    catch (InvocationTargetException e) {      throw new JAXBException(e);    }    catch (IllegalAccessException e) {      throw new JAXBException(e);    }  }  public Node bindTo(BinderImpl binder, Node node,                      Object obj, Namer namer,                      ArrayList<XmlMapping> attributes)    throws IOException, JAXBException  {    if (obj == null)      return null;    QName tagName = null;    if (namer != null)      tagName = namer.getQName(obj);    if (tagName == null)      tagName = _elementName;    if (_value != null) {      Node newNode = _value.bindTo(binder, node, obj);      if (newNode != node) {        binder.invalidate(node);        node = newNode;      }    }    else {      QName nodeName = JAXBUtil.qnameFromNode(node);      if (tagName.equals(nodeName)) {        Node child = node.getFirstChild();        child = JAXBUtil.skipIgnorableNodes(child);        if (attributes != null) {          // XXX        }        for (XmlMapping mapping : _elementMappings) {          if (child != null) {            // try to reuse as many of the child nodes as possible            Node newNode = mapping.bindTo(binder, child, obj);            if (newNode != child) {              node.replaceChild(newNode, child);              binder.invalidate(child);              child = newNode;            }            child = child.getNextSibling();            child = JAXBUtil.skipIgnorableNodes(child);          }          else {            Node newNode =               JAXBUtil.elementFromQName(mapping.getQName(obj), node);            node.appendChild(mapping.bindTo(binder, newNode, obj));          }        }      }      else {        binder.invalidate(node);        node = JAXBUtil.elementFromQName(tagName, node);        for (XmlMapping mapping : _elementMappings) {          Node child = JAXBUtil.elementFromQName(mapping.getQName(obj), node);          node.appendChild(mapping.bindTo(binder, child, obj));        }      }    }    binder.bind(obj, node);    return node;  }  public boolean isRootElement()  {    return _elementName != null;  }  public void generateSchema(XMLStreamWriter out)    throws JAXBException, XMLStreamException  {    if (_elementName != null) {      if ("".equals(_typeName.getLocalPart()))        out.writeStartElement(XML_SCHEMA_PREFIX, "element", XML_SCHEMA_NS);      else {        out.writeEmptyElement(XML_SCHEMA_PREFIX, "element", XML_SCHEMA_NS);        out.writeAttribute("type", _typeName.getLocalPart());      }      out.writeAttribute("name", _elementName.getLocalPart());    }    generateSchemaType(out);    if (_elementName != null && "".equals(_typeName.getLocalPart()))      out.writeEndElement(); // element  }  public void generateSchemaType(XMLStreamWriter out)    throws JAXBException, XMLStreamException  {    if (_value != null) {      out.writeStartElement(XML_SCHEMA_PREFIX, "simpleType", XML_SCHEMA_NS);      if (! "".equals(_typeName.getLocalPart()))        out.writeAttribute("name", _typeName.getLocalPart());      if (Collection.class.isAssignableFrom(_value.getAccessor().getType())) {        out.writeEmptyElement(XML_SCHEMA_PREFIX, "list", XML_SCHEMA_NS);        String itemType = StaxUtil.qnameToString(out, _value.getSchemaType());        out.writeAttribute("itemType", itemType);      }      else {        out.writeEmptyElement(XML_SCHEMA_PREFIX, "restriction", XML_SCHEMA_NS);        String base = StaxUtil.qnameToString(out, _value.getSchemaType());        out.writeAttribute("base", base);      }      for (XmlMapping mapping : _attributeMappings)        mapping.generateSchema(out);      out.writeEndElement(); // simpleType    }    else {      out.writeStartElement(XML_SCHEMA_PREFIX, "complexType", XML_SCHEMA_NS);      if (Modifier.isAbstract(_class.getModifiers()))        out.writeAttribute("abstract", "true");      if (! "".equals(_typeName.getLocalPart()))        out.writeAttribute("name", _typeName.getLocalPart());      out.writeStartElement(XML_SCHEMA_PREFIX, "sequence", XML_SCHEMA_NS);      for (XmlMapping mapping : _elementMappings)        mapping.generateSchema(out);      if (_anyElementMapping != null)        _anyElementMapping.generateSchema(out);      out.writeEndElement(); // sequence      for (XmlMapping mapping : _attributeMappings)        mapping.generateSchema(out);      out.writeEndElement(); // complexType    }  }  //XXX The TreeSet needs this for some reason  private static final Comparator methodComparator    = new java.util.Comparator<Method>() {      public int compare(Method m1, Method m2)      {        return m1.toGenericString().compareTo(m2.toGenericString());      }      public boolean equals(Object obj)      {        return obj == this;      }    };}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?