BeanGenerator.java 文件源码

java
阅读 35 收藏 0 点赞 0 评论 0

项目:orbeon-forms 作者:
protected void readBean(PipelineContext context, Config config, Mapping mapping, XMLReceiver xmlReceiver) {
    ExternalContext externalContext = (ExternalContext) context.getAttribute(PipelineContext.EXTERNAL_CONTEXT);
    if (externalContext == null)
        throw new OXFException("Missing external context in BeanGenerator");
    try {
        xmlReceiver.startDocument();
        String rootElementName = "beans";
        xmlReceiver.startElement("", rootElementName, rootElementName, XMLUtils.EMPTY_ATTRIBUTES);

        // Initialize Castor
        ParserAdapter adapter = new ParserAdapter(XMLUtils.newSAXParser(XMLUtils.ParserConfiguration.PLAIN).getParser());
        adapter.setContentHandler(xmlReceiver);
        Marshaller marshaller = new Marshaller(adapter);
        marshaller.setMarshalAsDocument(false);
        marshaller.setMapping(mapping);

        for (Iterator atts = config.getAttributesIterator(); atts.hasNext();) {
            String attName = (String) atts.next();
            Object bean = getBean(attName, config.getSourcesIterator(), externalContext);
            if (bean == null) {
                // Create empty element
                if (logger.isInfoEnabled())
                    logger.info("Bean " + attName + " is null");
                xmlReceiver.startElement("", attName, attName, XMLUtils.EMPTY_ATTRIBUTES);
                xmlReceiver.endElement("", attName, attName);
            } else if (bean instanceof org.w3c.dom.Document) {
                // W3C Document: send as-is
                TransformerUtils.sourceToSAX(new DOMSource((org.w3c.dom.Document) bean), new EmbeddedDocumentXMLReceiver(xmlReceiver));
            } else {
                // Serialize with Castor
                if (logger.isDebugEnabled())
                    logger.debug("Serializing bean" + attName + " value=" + bean);
                marshaller.setRootElement(attName);
                marshaller.marshal(bean);
            }
        }
        xmlReceiver.endElement("", rootElementName, rootElementName);
        xmlReceiver.endDocument();

    } catch (Exception e) {
        throw new OXFException(e);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号