001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package javax.xml.bind.helpers;
018    
019    import java.io.OutputStream;
020    import java.io.UnsupportedEncodingException;
021    import java.io.Writer;
022    
023    import javax.xml.bind.JAXBException;
024    import javax.xml.bind.Marshaller;
025    import javax.xml.bind.PropertyException;
026    import javax.xml.bind.ValidationEventHandler;
027    import javax.xml.bind.annotation.adapters.XmlAdapter;
028    import javax.xml.bind.attachment.AttachmentMarshaller;
029    import javax.xml.stream.XMLEventWriter;
030    import javax.xml.stream.XMLStreamWriter;
031    import javax.xml.transform.dom.DOMResult;
032    import javax.xml.transform.sax.SAXResult;
033    import javax.xml.transform.stream.StreamResult;
034    import javax.xml.transform.Result;
035    import javax.xml.validation.Schema;
036    
037    import org.w3c.dom.Node;
038    
039    import org.xml.sax.ContentHandler;
040    
041    public abstract class AbstractMarshallerImpl implements Marshaller {
042    
043        static String aliases[] = {
044                "UTF-8", "UTF8",
045                "UTF-16", "Unicode",
046                "UTF-16BE", "UnicodeBigUnmarked",
047                "UTF-16LE", "UnicodeLittleUnmarked",
048                "US-ASCII", "ASCII",
049                "TIS-620", "TIS620",
050                "ISO-10646-UCS-2", "Unicode",
051                "EBCDIC-CP-US", "cp037",
052                "EBCDIC-CP-CA", "cp037",
053                "EBCDIC-CP-NL", "cp037",
054                "EBCDIC-CP-WT", "cp037",
055                "EBCDIC-CP-DK", "cp277",
056                "EBCDIC-CP-NO", "cp277",
057                "EBCDIC-CP-FI", "cp278",
058                "EBCDIC-CP-SE", "cp278",
059                "EBCDIC-CP-IT", "cp280",
060                "EBCDIC-CP-ES", "cp284",
061                "EBCDIC-CP-GB", "cp285",
062                "EBCDIC-CP-FR", "cp297",
063                "EBCDIC-CP-AR1", "cp420",
064                "EBCDIC-CP-HE", "cp424",
065                "EBCDIC-CP-BE", "cp500",
066                "EBCDIC-CP-CH", "cp500",
067                "EBCDIC-CP-ROECE", "cp870",
068                "EBCDIC-CP-YU", "cp870",
069                "EBCDIC-CP-IS", "cp871",
070                "EBCDIC-CP-AR2", "cp918"
071        };
072    
073        private ValidationEventHandler eventHandler = new DefaultValidationEventHandler();
074        private String encoding = "UTF-8";
075        private String schemaLocation;
076        private String noNSSchemaLocation;
077        private boolean formattedOutput;
078        private boolean fragment;
079    
080        public final void marshal(Object obj, OutputStream os) throws JAXBException {
081            checkNotNull(obj, "obj", os, "os");
082            marshal(obj, new StreamResult(os));
083        }
084    
085        public final void marshal(Object obj, Writer w) throws JAXBException {
086            checkNotNull(obj, "obj", w, "writer");
087            marshal(obj, new StreamResult(w));
088        }
089    
090        public final void marshal(Object obj, ContentHandler handler) throws JAXBException {
091            checkNotNull(obj, "obj", handler, "handler");
092            marshal(obj, new SAXResult(handler));
093        }
094    
095        public final void marshal(Object obj, Node node) throws JAXBException {
096            checkNotNull(obj, "obj", node, "node");
097            marshal(obj, new DOMResult(node));
098        }
099    
100        public Node getNode(Object obj) throws JAXBException {
101            checkNotNull(obj, "obj", "foo", "bar");
102            throw new UnsupportedOperationException();
103        }
104    
105        protected String getEncoding() {
106            return encoding;
107        }
108    
109        protected void setEncoding(String encoding) {
110            this.encoding = encoding;
111        }
112    
113        protected String getSchemaLocation() {
114            return schemaLocation;
115        }
116    
117        protected void setSchemaLocation(String location) {
118            schemaLocation = location;
119        }
120    
121        protected String getNoNSSchemaLocation() {
122            return noNSSchemaLocation;
123        }
124    
125        protected void setNoNSSchemaLocation(String location) {
126            noNSSchemaLocation = location;
127        }
128    
129        protected boolean isFormattedOutput() {
130            return formattedOutput;
131        }
132    
133        protected void setFormattedOutput(boolean v) {
134            formattedOutput = v;
135        }
136    
137        protected boolean isFragment() {
138            return fragment;
139        }
140    
141        protected void setFragment(boolean v) {
142            fragment = v;
143        }
144    
145        protected String getJavaEncoding(String encoding) throws UnsupportedEncodingException {
146            try {
147                "dummy".getBytes(encoding);
148                return encoding;
149            }
150            catch (UnsupportedEncodingException e) {
151            }
152            for (int i = 0; i < aliases.length; i += 2) {
153                if (encoding.equals(aliases[i])) {
154                    "dummy".getBytes(aliases[i + 1]);
155                    return aliases[i + 1];
156                }
157            }
158            throw new UnsupportedEncodingException(encoding);
159        }
160    
161        public void setProperty(String name, Object value) throws PropertyException {
162            if (name == null) {
163                throw new IllegalArgumentException("name must not be null");
164            }
165            if (JAXB_ENCODING.equals(name)) {
166                checkString(name, value);
167                setEncoding((String) value);
168            } else if (JAXB_FORMATTED_OUTPUT.equals(name)) {
169                checkBoolean(name, value);
170                setFormattedOutput(((Boolean) value).booleanValue());
171            } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name)) {
172                checkString(name, value);
173                setNoNSSchemaLocation((String) value);
174            } else if (JAXB_SCHEMA_LOCATION.equals(name)) {
175                checkString(name, value);
176                setSchemaLocation((String) value);
177            } else if (JAXB_FRAGMENT.equals(name)) {
178                checkBoolean(name, value);
179                setFragment(((Boolean) value).booleanValue());
180            } else {
181                throw new PropertyException(name, value);
182            }
183        }
184    
185        public Object getProperty(String name) throws PropertyException {
186            if (name == null) {
187                throw new IllegalArgumentException("name must not be null");
188            }
189            if (JAXB_ENCODING.equals(name)) {
190                return getEncoding();
191            } else if (JAXB_FORMATTED_OUTPUT.equals(name)) {
192                return isFormattedOutput() ? Boolean.TRUE : Boolean.FALSE;
193            } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name)) {
194                return getNoNSSchemaLocation();
195            } else if (JAXB_SCHEMA_LOCATION.equals(name)) {
196                return getSchemaLocation();
197            } else if (JAXB_FRAGMENT.equals(name)) {
198                return isFragment() ? Boolean.TRUE : Boolean.FALSE;
199            } else {
200                throw new PropertyException(name);
201            }
202        }
203    
204        public ValidationEventHandler getEventHandler() throws JAXBException {
205            return eventHandler;
206        }
207    
208        public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
209            if (handler == null) {
210                eventHandler = new DefaultValidationEventHandler();
211            } else {
212                eventHandler = handler;
213            }
214        }
215    
216        private void checkBoolean(String name, Object value) throws PropertyException {
217            if (!(value instanceof Boolean)) {
218                throw new PropertyException(name + " must be a boolean");
219            }
220        }
221    
222        private void checkString(String name, Object value) throws PropertyException {
223            if (!(value instanceof String)) {
224                throw new PropertyException(name + " must be a string");
225            }
226        }
227    
228        private void checkNotNull(Object o1, String o1Name, Object o2, String o2Name) {
229            if (o1 == null) {
230                throw new IllegalArgumentException(o1Name + " must not be null");
231            }
232            if (o2 == null) {
233                throw new IllegalArgumentException(o2Name + " must not be null");
234            }
235        }
236    
237        public void marshal(Object obj, XMLEventWriter writer)
238                throws JAXBException {
239            throw new UnsupportedOperationException();
240        }
241    
242        public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
243            throw new UnsupportedOperationException();
244        }
245    
246        public void setSchema(Schema schema) {
247            throw new UnsupportedOperationException();
248        }
249    
250        public Schema getSchema() {
251            throw new UnsupportedOperationException();
252        }
253    
254        public void setAdapter(XmlAdapter adapter) {
255            if (adapter == null) {
256                throw new IllegalArgumentException();
257            }
258            setAdapter((Class<XmlAdapter>) adapter.getClass(), adapter);
259        }
260    
261        public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
262            throw new UnsupportedOperationException();
263        }
264    
265        public <A extends XmlAdapter> A getAdapter(Class<A> type) {
266            throw new UnsupportedOperationException();
267        }
268    
269        public void setAttachmentMarshaller(AttachmentMarshaller am) {
270            throw new UnsupportedOperationException();
271        }
272    
273        public AttachmentMarshaller getAttachmentMarshaller() {
274            throw new UnsupportedOperationException();
275        }
276    
277        public void setListener(javax.xml.bind.Marshaller.Listener listener) {
278            throw new UnsupportedOperationException();
279        }
280    
281        public javax.xml.bind.Marshaller.Listener getListener() {
282            throw new UnsupportedOperationException();
283        }
284    }