gpt4 book ai didi

com.sun.xml.bind.v2.runtime.XMLSerializer类的使用及代码示例

转载 作者:知者 更新时间:2024-03-23 23:13:05 25 4
gpt4 key购买 nike

本文整理了Java中com.sun.xml.bind.v2.runtime.XMLSerializer类的一些代码示例,展示了XMLSerializer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XMLSerializer类的具体详情如下:
包路径:com.sun.xml.bind.v2.runtime.XMLSerializer
类名称:XMLSerializer

XMLSerializer介绍

[英]Receives XML serialization event and writes to XmlOutput.

This object coordinates the overall marshalling efforts across different content-tree objects and different target formats.

The following CFG gives the proper sequence of method invocation.

MARSHALLING  :=  ELEMENT 
ELEMENT      :=  "startElement" NSDECL* "endNamespaceDecls" 
ATTRIBUTE* "endAttributes" BODY "endElement" 
NSDECL       :=  "declareNamespace" 
ATTRIBUTE    :=  "attribute" 
ATTVALUES    :=  "text" 
BODY         :=  ( "text" | ELEMENT )

A marshalling of one element consists of two stages. The first stage is for marshalling attributes and collecting namespace declarations. The second stage is for marshalling characters/child elements of that element.

Observe that multiple invocation of "text" is allowed.

Also observe that the namespace declarations are allowed only between "startElement" and "endAttributes".
Exceptions in marshaller

IOException, SAXException, and XMLStreamExceptionare thrown from XmlOutput. They are always considered fatal, and therefore caught only by MarshallerImpl.

AccessorException can be thrown when an access to a property/field fails, and this is considered as a recoverable error, so it's caught everywhere.
[中]接收XML序列化事件并写入XmlOutput。
该对象协调跨不同内容树对象和不同目标格式的总体编组工作。
下面的CFG给出了方法调用的正确顺序。

MARSHALLING  :=  ELEMENT 
ELEMENT      :=  "startElement" NSDECL* "endNamespaceDecls" 
ATTRIBUTE* "endAttributes" BODY "endElement" 
NSDECL       :=  "declareNamespace" 
ATTRIBUTE    :=  "attribute" 
ATTVALUES    :=  "text" 
BODY         :=  ( "text" | ELEMENT )

一个元素的编组包括两个阶段。第一阶段是编组属性和收集命名空间声明。第二阶段用于编组该元素的字符/子元素。
请注意,允许多次调用“文本”。
还要注意,名称空间声明只允许在“startElement”和“endAttributes”之间使用。
马歇尔的例外
从XmlOutput抛出IOException、SAXException和XMLStreamExceptionary。它们通常被认为是致命的,因此只有马歇尔·雷普勒才能捕捉到它们。
当对属性/字段的访问失败时,可以抛出AccessorException,这被认为是一个可恢复的错误,因此它到处都会被捕获。

代码示例

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
  try {
    if (retainPropertyInfo) {
    final Property parentProperty = target.getCurrentProperty();
    for( Property<BeanT> p : uriProperties ) {
      target.currentProperty.set(p);
      p.serializeURIs(bean,target);
    }
    target.currentProperty.set(parentProperty);
    } else {
      for( Property<BeanT> p : uriProperties ) {
        p.serializeURIs(bean,target);
      }
    }
    if(inheritedAttWildcard!=null) {
      Map<QName,String> map = inheritedAttWildcard.get(bean);
      target.attWildcardAsURIs(map,null);
    }
  } catch (AccessorException e) {
    target.reportError(null,e);
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public Base64Data print(Image v) {
  ByteArrayOutputStreamEx imageData = new ByteArrayOutputStreamEx();
  XMLSerializer xs = XMLSerializer.getInstance();
  String mimeType = xs.getXMIMEContentType();
  if(mimeType==null || mimeType.startsWith("image/*"))
    } else {
      xs.handleEvent(new ValidationEventImpl(
        ValidationEvent.ERROR,
        Messages.NO_IMAGE_WRITER.format(mimeType),
        xs.getCurrentLocation(null) ));
    xs.handleError(e);

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

/**
 * @param assoc
 *      non-null if the marshaller is working inside {@link BinderImpl}.
 */
public MarshallerImpl( JAXBContextImpl c, AssociationMap assoc ) {
  context = c;
  serializer = new XMLSerializer(this);
  c14nSupport = context.c14nSupport;
  try {
    setEventHandler(this);
  } catch (JAXBException e) {
    throw new AssertionError(e);    // impossible
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public void serializeItem(JaxBeanInfo expected, ItemT item, XMLSerializer w) throws SAXException, IOException, XMLStreamException {
    if(item==null) {
      w.writeXsiNilTrue();
    } else {
      w.childAsXsiType(item,fieldName,expected, false);
    }
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public void marshal(T value, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
  out.startElement(tagName,null);
  if(value==null) {
    out.writeXsiNilTrue();
  } else {
    out.childAsXsiType(value,null,bi,false);
  }
  out.endElement();
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

@Override
public CharSequence print(V o) throws AccessorException {
  XMLSerializer w = XMLSerializer.getInstance();
  MimeType old = w.setExpectedMimeType(expectedMimeType);
  try {
    return core.print(o);
  } finally {
    w.setExpectedMimeType(old);
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

handleMissingObjectError(fieldName);
} else {
  child = pushObject(child,fieldName);
  if(child==null) { // error recovery
    endNamespaceDecls(null);
    endAttributes();
    return;
    fireBeforeMarshalEvents(actual, child);
      actual = grammar.getBeanInfo(child,true);
      if (actual.lookForLifecycleMethods()) {
        fireBeforeMarshalEvents(actual, child);
      reportError(fieldName,e);
      endNamespaceDecls(null);
      endAttributes();
      return; // recover by ignore
      actualTypeName = actual.getTypeName(child);
      if(actualTypeName==null) {
        reportError(new ValidationEventImpl(
            ValidationEvent.ERROR,
            Messages.SUBSTITUTED_BY_ANONYMOUS_TYPE.format(
              child.getClass().getName(),
              actual.jaxbType.getName()),
            getCurrentLocation(fieldName)));

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
  if(tagName==null) {
    target.reportError(
      new ValidationEventImpl(
        ValidationEvent.ERROR,
        Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()),
        null,
        null));
  }
  else {
    target.startElement(tagName,bean);
    target.childAsSoleContent(bean,null);
    target.endElement();
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

try {
  prewrite(out, true, postInitAction);
  serializer.startElement(rootTagName,null);
  if(bi.jaxbType==Void.class || bi.jaxbType==void.class) {
    serializer.endNamespaceDecls(null);
    serializer.endAttributes();
  } else { // normal cases
    if(obj==null)
      serializer.writeXsiNilTrue();
    else
      serializer.childAsXsiType(obj,"root",bi, false);
  serializer.endElement();
  postwrite();
} catch( SAXException e ) {
  throw new MarshalException(e);
} finally {
  serializer.close();

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

serializer.startElementForce(namespaceURI,localName,p,null);
  else
    serializer.startElement(namespaceURI,localName, p,null);
    serializer.getNamespaceContext().force(
        prefixMap.get(i + 1), prefixMap.get(i));
    String prefix = getPrefix(qname);
    serializer.getNamespaceContext().declareNamespace(
      atts.getURI(i), prefix, true );
  serializer.endNamespaceDecls(null);
    serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
  serializer.endAttributes();
} catch (IOException e) {
  throw new SAXException2(e);

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

/**
 * Short for the following call sequence:
 *
 * <pre>
   getNamespaceContext().declareNamespace(WellKnownNamespace.XML_SCHEMA_INSTANCE,"xsi",true);
   endNamespaceDecls();
   attribute(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil","true");
   endAttributes();
 * </pre>
 */
public final void writeXsiNilTrue() throws SAXException, IOException, XMLStreamException {
  getNamespaceContext().declareNamespace(WellKnownNamespace.XML_SCHEMA_INSTANCE,"xsi",true);
  endNamespaceDecls(null);
  attribute(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil","true");
  endAttributes();
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public void serializeAttributes(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
  for( AttributeProperty<BeanT> p : attributeProperties )
    try {
      if (retainPropertyInfo) {
      final Property parentProperty = target.getCurrentProperty();
      target.currentProperty.set(p);
      p.serializeAttributes(bean,target);
      target.currentProperty.set(parentProperty);
      } else {
        p.serializeAttributes(bean,target);
      }
      if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) {
        isNilIncluded = true;
      }
    } catch (AccessorException e) {
      target.reportError(null,e);
    }
  try {
    if(inheritedAttWildcard!=null) {
      Map<QName,String> map = inheritedAttWildcard.get(bean);
      target.attWildcardAsAttributes(map,null);
    }
  } catch (AccessorException e) {
    target.reportError(null,e);
  }
}

代码示例来源:origin: com.sun.jersey/jersey-json

private RuntimePropertyInfo getCurrentElementRuntimePropertyInfo() {
  final XMLSerializer xs = XMLSerializer.getInstance();
  final Property cp = (xs == null) ? null : xs.getCurrentProperty();
  return (cp == null) ? null : cp.getInfo();
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public String print(XMLGregorianCalendar cal) {
  XMLSerializer xs = XMLSerializer.getInstance();
  QName type = xs.getSchemaType();
  if (type != null) {
    try {
      checkXmlGregorianCalendarFieldRef(type, cal);
      String format = xmlGregorianCalendarFormatString.get(type);
      if (format != null) {
        return format(format, cal);
      }
    } catch (javax.xml.bind.MarshalException e) {
      // see issue 649
      xs.handleEvent(new ValidationEventImpl(ValidationEvent.WARNING, e.getMessage(),
        xs.getCurrentLocation(null) ));
      return "";
    }
  }
  return cal.toXMLFormat();
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException {
  serializer.startDocument(out,fragment,getSchemaLocation(),getNoNSSchemaLocation());
  if(postInitAction!=null)    postInitAction.run();
  if(prefixMapper!=null) {
    // be defensive as we work with the user's code
    String[] decls = prefixMapper.getContextualNamespaceDecls();
    if(decls!=null) { // defensive check
      for( int i=0; i<decls.length; i+=2 ) {
        String prefix = decls[i];
        String nsUri = decls[i+1];
        if(nsUri!=null && prefix!=null) // defensive check
          serializer.addInscopeBinding(nsUri,prefix);
      }
    }
  }
  serializer.setPrefixMapper(prefixMapper);
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public Base64Data print(Source v) {
  XMLSerializer xs = XMLSerializer.getInstance();
  Base64Data bd = new Base64Data();
  String contentType = xs.getXMIMEContentType();
  MimeType mt = null;
  if(contentType!=null)
      mt = new MimeType(contentType);
    } catch (MimeTypeParseException e) {
      xs.handleError(e);
    Transformer tr = xs.getIdentityTransformer();
    String defaultEncoding = tr.getOutputProperty(OutputKeys.ENCODING);
    tr.setOutputProperty(OutputKeys.ENCODING, charset);                        
  } catch (TransformerException e) {
    xs.handleError(e);
  } catch (UnsupportedEncodingException e) {
    xs.handleError(e);

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public String print(BeanT bean) throws AccessorException, SAXException {
  TargetT target = acc.get(bean);
  if(target==null)    return null;
  
  XMLSerializer w = XMLSerializer.getInstance();
  try {
    String id = w.grammar.getBeanInfo(target,true).getId(target,w);
    if(id==null)
      w.errorMissingId(target);
    return id;
  } catch (JAXBException e) {
    w.reportError(null,e);
    return null;
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

serializer.childAsRoot(obj);
  postwrite();
} catch( SAXException e ) {
  throw new MarshalException(e);
} finally {
  serializer.close();

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

@Override
public @NotNull CharSequence print(@NotNull V o) throws AccessorException {
  XMLSerializer w = XMLSerializer.getInstance();
  boolean old = w.setInlineBinaryFlag(true);
  try {
    return core.print(o);
  } finally {
    w.setInlineBinaryFlag(old);
  }
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public Base64Data print(byte[] v) {
    XMLSerializer w = XMLSerializer.getInstance();
    Base64Data bd = new Base64Data();
    String mimeType = w.getXMIMEContentType();
    bd.set(v,mimeType);
    return bd;
  }
});

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com