摘自spring.io [论坛]
默认的StaxEventItemWriter可能没有此功能。我必须为我的一个项目做类似的事情,并编写了一个自定义的GroupingStaxEventItemWriter。请参见下面的代码。您需要针对特定用例修改openGroup和closeGroup方法。请注意,通过直接写入基础java.io.Writer而不是通过XMLEventWriter来关闭分组标记。这是重新启动性所必需的。
public class GroupingStaxEventItemWriter<T> extends StaxEventItemWriter<T> { private static final String GROUP_IDENTIFIER = "CURRENT_GROUP"; private Classifier<T, String> classifier; private String currentGroup; private XMLEventWriter eventWriter; private Writer writer; @Override public void write(List<? extends T> items) throws XmlMappingException, Exception { Map<String, List<T>> itemsGroup = new linkedHashMap<String, List<T>>(); for (T item : items) { String group = classifier.classify(item); if (!itemsGroup.containsKey(group)) { itemsGroup.put(group, new ArrayList<T>()); } itemsGroup.get(group).add(item); } for (String group : itemsGroup.keySet()) { if (group == null || !group.equals(currentGroup)) { if (currentGroup != null) { closeGroup(currentGroup); } currentGroup = group; if (currentGroup != null) { openGroup(currentGroup); } } super.write(itemsGroup.get(group)); } } protected void openGroup(String group) throws XMLStreamException, FactoryConfigurationError { String groupTagName = group; String groupTagNameSpacePrefix = ""; String groupTagNameSpace = null; if (groupTagName.contains("{")) { groupTagNameSpace = groupTagName.replaceAll("\{(.*)\}.*", ""); groupTagName = groupTagName.replaceAll("\{.*\}(.*)", ""); if (groupTagName.contains(":")) { groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", ""); groupTagName = groupTagName.replaceAll(".*:(.*)", ""); } } XMLEventFactory xmlEventFactory = createXmlEventFactory(); eventWriter.add(xmlEventFactory.createStartElement(groupTagNameSpacePrefix, groupTagNameSpace, groupTagName)); } protected void closeGroup(String group) throws XMLStreamException, FactoryConfigurationError { String groupTagName = group; String groupTagNameSpacePrefix = ""; if (groupTagName.contains("{")) { groupTagName = groupTagName.replaceAll("\{.*\}(.*)", ""); if (groupTagName.contains(":")) { groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", "") + ":"; groupTagName = groupTagName.replaceAll(".*:(.*)", ""); } } try { writer.write("</" + groupTagNameSpacePrefix + groupTagName + ">"); } catch (IOException ioe) { throw new DataAccessResourceFailureException("Unable to close group: [" + group + "]", ioe); } } @Override protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException { this.writer = writer; this.eventWriter = super.createXmlEventWriter(outputFactory, writer); return eventWriter; } @Override public void open(ExecutionContext executionContext) { if (executionContext.containsKey(getExecutionContextKey(GROUP_IDENTIFIER))) { currentGroup = executionContext.getString(getExecutionContextKey(GROUP_IDENTIFIER)); } super.open(executionContext); } @Override public void update(ExecutionContext executionContext) { executionContext.putString(getExecutionContextKey(GROUP_IDENTIFIER), currentGroup); super.update(executionContext); } @Override public void close() { if (currentGroup != null) { try { closeGroup(currentGroup); } catch (XMLStreamException e) { throw new ItemStreamException("Failed to write close tag for element: " + currentGroup, e); } catch (FactoryConfigurationError e) { throw new ItemStreamException("Failed to write close tag for element: " + currentGroup, e); } } super.close(); } @Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); Assert.notNull(classifier, "Missing required property 'classifier'"); } public void setClassifier(Classifier<T, String> classifier) { this.classifier = classifier; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)