1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.pure.exchange;
18
19 import java.io.File;
20 import java.io.OutputStream;
21 import java.io.Writer;
22
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import javax.annotation.WillClose;
26
27 import com.helger.commons.ValueEnforcer;
28 import com.helger.commons.annotation.OverrideOnDemand;
29 import com.helger.commons.state.ESuccess;
30 import com.helger.commons.string.ToStringGenerator;
31 import com.helger.schematron.pure.model.IPSElement;
32 import com.helger.xml.microdom.IMicroDocument;
33 import com.helger.xml.microdom.IMicroElement;
34 import com.helger.xml.microdom.IMicroNode;
35 import com.helger.xml.microdom.MicroDocument;
36 import com.helger.xml.microdom.serialize.MicroWriter;
37
38
39
40
41
42
43 public class PSWriter
44 {
45 private final IPSWriterSettings m_aWriterSettings;
46
47
48
49
50 public PSWriter ()
51 {
52 this (PSWriterSettings.DEFAULT_SETTINGS);
53 }
54
55
56
57
58
59
60
61 public PSWriter (@Nonnull final IPSWriterSettings aWriterSettings)
62 {
63 m_aWriterSettings = ValueEnforcer.notNull (aWriterSettings, "WriterSettings");
64 }
65
66
67
68
69 @Nonnull
70 public IPSWriterSettings getWriterSettings ()
71 {
72 return m_aWriterSettings;
73 }
74
75 @Nonnull
76 @OverrideOnDemand
77 protected IMicroNode getAsDocument (@Nonnull final IMicroElement aElement)
78 {
79 final IMicroDocument aDoc = new MicroDocument ();
80 aDoc.appendChild (aElement);
81 return aDoc;
82 }
83
84
85
86
87
88
89
90
91
92
93 @Nonnull
94 public ESuccess writeToFile (@Nonnull final IPSElement aPSElement, @Nonnull final File aFile)
95 {
96 ValueEnforcer.notNull (aPSElement, "PSElement");
97 final IMicroElement eXML = aPSElement.getAsMicroElement ();
98 return MicroWriter.writeToFile (getAsDocument (eXML), aFile, m_aWriterSettings.getXMLWriterSettings ());
99 }
100
101
102
103
104
105
106
107
108
109
110
111 @Nonnull
112 public ESuccess writeToStream (@Nonnull final IPSElement aPSElement, @Nonnull @WillClose final OutputStream aOS)
113 {
114 ValueEnforcer.notNull (aPSElement, "PSElement");
115 final IMicroElement eXML = aPSElement.getAsMicroElement ();
116 return MicroWriter.writeToStream (getAsDocument (eXML), aOS, m_aWriterSettings.getXMLWriterSettings ());
117 }
118
119
120
121
122
123
124
125
126
127
128
129 @Nonnull
130 public ESuccess writeToWriter (@Nonnull final IPSElement aPSElement, @Nonnull @WillClose final Writer aWriter)
131 {
132 ValueEnforcer.notNull (aPSElement, "PSElement");
133 final IMicroElement eXML = aPSElement.getAsMicroElement ();
134 return MicroWriter.writeToWriter (getAsDocument (eXML), aWriter, m_aWriterSettings.getXMLWriterSettings ());
135 }
136
137
138
139
140
141
142
143
144
145
146 @Nullable
147 public String getXMLString (@Nonnull final IPSElement aPSElement)
148 {
149 ValueEnforcer.notNull (aPSElement, "PSElement");
150 final IMicroElement eXML = aPSElement.getAsMicroElement ();
151 return MicroWriter.getNodeAsString (getAsDocument (eXML), m_aWriterSettings.getXMLWriterSettings ());
152 }
153
154
155
156
157
158
159
160
161
162
163
164 @Nullable
165 public String getXMLStringNotNull (@Nonnull final IPSElement aPSElement)
166 {
167 final String ret = getXMLString (aPSElement);
168 if (ret == null)
169 throw new IllegalStateException ("Failed to serialize the passed PS element: " + aPSElement);
170 return ret;
171 }
172
173 @Override
174 public String toString ()
175 {
176 return new ToStringGenerator (this).append ("writerSettings", m_aWriterSettings).getToString ();
177 }
178 }