fork(11) download
  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.xml.bind.JAXBContext;
  7. import javax.xml.bind.JAXBException;
  8. import javax.xml.bind.Marshaller;
  9. import javax.xml.bind.Unmarshaller;
  10. import javax.xml.bind.annotation.XmlAttribute;
  11. import javax.xml.bind.annotation.XmlElement;
  12. import javax.xml.bind.annotation.XmlElementWrapper;
  13. import javax.xml.bind.annotation.XmlRootElement;
  14. import javax.xml.bind.annotation.XmlTransient;
  15.  
  16. class XmlMerge {
  17.  
  18. public static void main(String[] args) {
  19. if (args == null || args.length != 3) {
  20. System.out.println("Usage: java -jar XmlMerge.jar"
  21. + " <base-xml> <source-xml> <out-xml>");
  22. System.exit(1);
  23. }
  24. File baseXml = new File(args[0]);
  25. File sourceXml = new File(args[1]);
  26. File outXml = new File(args[2]);
  27. merge(baseXml, sourceXml, outXml);
  28. }
  29.  
  30. private static void merge(File baseXml, File sourceXml, File outXml) {
  31. try {
  32. JAXBContext jaxb = JAXBContext.newInstance(Data.class);
  33. Unmarshaller reader = jaxb.createUnmarshaller();
  34. Marshaller writer = jaxb.createMarshaller();
  35. writer.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  36.  
  37. Data baseData = (Data) reader.unmarshal(baseXml);
  38. Data sourceData = (Data) reader.unmarshal(sourceXml);
  39. baseData.getLineMap().putAll(sourceData.getLineMap());
  40.  
  41. writer.marshal(baseData, outXml);
  42. } catch (JAXBException ex) {
  43. System.err.println(ex);
  44. }
  45. }
  46. }
  47.  
  48.  
  49. @XmlRootElement(name = "data")
  50. class Data {
  51. Map<String, String> lineMap = new LinkedHashMap<>();
  52.  
  53. @XmlElement(name = "line")
  54. public List<Line> getLines() {
  55. List<Line> lines = new ArrayList<>();
  56. for (Map.Entry<String, String> entry : lineMap.entrySet()) {
  57. Line line = new Line();
  58. line.setKey(entry.getKey());
  59. line.setData(entry.getValue());
  60. lines.add(line);
  61. }
  62. return lines;
  63. }
  64.  
  65. public void setLines(List<Line> lines) {
  66. for (Line line : lines) {
  67. lineMap.put(line.getKey(), line.getData());
  68. }
  69. }
  70.  
  71. @XmlTransient
  72. public Map<String, String> getLineMap() {
  73. return lineMap;
  74. }
  75.  
  76. public void setLineMap(Map<String, String> lineMap) {
  77. this.lineMap = lineMap;
  78. }
  79. }
  80.  
  81. @XmlRootElement(name = "line")
  82. class Line {
  83. private String key;
  84. private String data;
  85.  
  86. @XmlAttribute(name = "key", required = true)
  87. public String getKey() {
  88. return key;
  89. }
  90.  
  91. public void setKey(String key) {
  92. this.key = key;
  93. }
  94.  
  95. @XmlElement(name = "data")
  96. public String getData() {
  97. return data;
  98. }
  99.  
  100. public void setData(String data) {
  101. this.data = data;
  102. }
  103.  
  104. }
  105.  
Runtime error #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Usage: java -jar XmlMerge.jar <base-xml> <source-xml> <out-xml>