blob: 85c011b62cef06b9aaea66a7444636da4198f3d7 [file] [log] [blame]
Carsten Ziegeler1da03592007-08-24 07:27:04 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.scrplugin.xml;
20
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000021import java.io.File;
Carsten Ziegeler87a82482013-01-23 12:19:18 +000022import java.io.FileOutputStream;
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000023import java.io.IOException;
24import java.io.InputStream;
Carsten Ziegeler87a82482013-01-23 12:19:18 +000025import java.io.OutputStreamWriter;
Carsten Ziegeler87a82482013-01-23 12:19:18 +000026import java.io.Writer;
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000027import java.util.Properties;
Carsten Ziegeler1da03592007-08-24 07:27:04 +000028
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000029import javax.xml.transform.OutputKeys;
30import javax.xml.transform.Transformer;
31import javax.xml.transform.TransformerException;
32import javax.xml.transform.TransformerFactory;
33import javax.xml.transform.sax.SAXResult;
34import javax.xml.transform.sax.SAXTransformerFactory;
35import javax.xml.transform.sax.TransformerHandler;
Carsten Ziegeler1da03592007-08-24 07:27:04 +000036import javax.xml.transform.stream.StreamResult;
37import javax.xml.transform.stream.StreamSource;
38
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000039import org.xml.sax.ContentHandler;
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000040import org.xml.sax.SAXException;
Carsten Ziegeler1da03592007-08-24 07:27:04 +000041import org.xml.sax.helpers.AttributesImpl;
42
43/**
44 * Utility class for xml/sax handling.
Carsten Ziegeler1da03592007-08-24 07:27:04 +000045 */
46public class IOUtils {
47
48 /** The transformer factory. */
49 private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) TransformerFactory.newInstance();
50
Carsten Ziegeler1da03592007-08-24 07:27:04 +000051 /**
52 * Parse a file and send the sax events to the content handler.
53 * @param file
54 * @param handler
Carsten Ziegeler1da03592007-08-24 07:27:04 +000055 * @throws TransformerException
56 */
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +000057 public static final void parse(final InputStream file, final ContentHandler handler)
Felix Meschberger1fd58292009-10-02 12:59:27 +000058 throws TransformerException {
Carsten Ziegeler1da03592007-08-24 07:27:04 +000059 final Transformer transformer = FACTORY.newTransformer();
Felix Meschberger1fd58292009-10-02 12:59:27 +000060 transformer.transform( new StreamSource( file ), new SAXResult( handler ) );
Carsten Ziegeler1da03592007-08-24 07:27:04 +000061 }
62
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +000063 /**
64 * Get a serializer to write XML to a file.
65 */
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000066 public static ContentHandler getSerializer(final File file)
Carsten Ziegelera21b5842013-01-23 13:00:46 +000067 throws TransformerException, IOException {
68 final Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000069
Carsten Ziegelera21b5842013-01-23 13:00:46 +000070 final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
71 final Transformer transformer = transformerHandler.getTransformer();
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000072
Carsten Ziegelera21b5842013-01-23 13:00:46 +000073 final Properties format = new Properties();
74 format.put(OutputKeys.METHOD, "xml");
75 format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
76 format.put(OutputKeys.ENCODING, "UTF-8");
77 format.put(OutputKeys.INDENT, "yes");
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +000078
Carsten Ziegelera21b5842013-01-23 13:00:46 +000079 transformer.setOutputProperties(format);
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000080
Carsten Ziegelera21b5842013-01-23 13:00:46 +000081 transformerHandler.setResult(new StreamResult(writer));
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000082
Carsten Ziegelera21b5842013-01-23 13:00:46 +000083 return transformerHandler;
Carsten Ziegeler1da03592007-08-24 07:27:04 +000084 }
85
Carsten Ziegeler28ff2702007-08-28 15:40:03 +000086 /**
87 * Helper method to add an attribute.
88 * This implementation adds a new attribute with the given name
89 * and value. Before adding the value is checked for non-null.
90 * @param ai The attributes impl receiving the additional attribute.
91 * @param name The name of the attribute.
92 * @param value The value of the attribute.
93 */
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +000094 public static void addAttribute(final AttributesImpl ai, final String name, final Object value) {
Carsten Ziegeler28ff2702007-08-28 15:40:03 +000095 if ( value != null ) {
96 ai.addAttribute("", name, name, "CDATA", value.toString());
97 }
98 }
99
100 /**
101 * Helper method writing out a string.
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000102 * @param ch The content handler.
Carsten Ziegeler28ff2702007-08-28 15:40:03 +0000103 * @param text
104 * @throws SAXException
105 */
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +0000106 public static void text(final ContentHandler ch, final String text)
Carsten Ziegeler28ff2702007-08-28 15:40:03 +0000107 throws SAXException {
108 if ( text != null ) {
109 final char[] c = text.toCharArray();
110 ch.characters(c, 0, c.length);
111 }
112 }
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000113
114 /**
115 * Helper method to indent the xml elements.
116 * Each level is indented with four spaces.
117 * @param ch The content handler.
118 * @param level The level of indention.
119 */
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +0000120 public static void indent(final ContentHandler ch, final int level)
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000121 throws SAXException {
122 for(int i=0;i<level;i++) {
123 IOUtils.text(ch, " ");
124 }
125 }
126
127 /**
128 * Helper method to create a new line.
129 * @param ch The content handler.
130 * @throws SAXException
131 */
Carsten Ziegelerb1a850f2013-01-23 12:55:54 +0000132 public static void newline(final ContentHandler ch)
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000133 throws SAXException {
134 IOUtils.text(ch, "\n");
135 }
Carsten Ziegeler1da03592007-08-24 07:27:04 +0000136}