1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.dom.smil.parser; 19 20 import java.io.BufferedWriter; 21 import java.io.IOException; 22 import java.io.OutputStream; 23 import java.io.OutputStreamWriter; 24 import java.io.UnsupportedEncodingException; 25 import java.io.Writer; 26 27 import org.w3c.dom.Attr; 28 import org.w3c.dom.Element; 29 import org.w3c.dom.NamedNodeMap; 30 import org.w3c.dom.smil.SMILDocument; 31 import org.w3c.dom.smil.SMILElement; 32 33 public class SmilXmlSerializer { serialize(SMILDocument smilDoc, OutputStream out)34 public static void serialize(SMILDocument smilDoc, OutputStream out) { 35 try { 36 Writer writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"), 2048); 37 38 writeElement(writer, smilDoc.getDocumentElement()); 39 writer.flush(); 40 } catch (UnsupportedEncodingException e) { 41 e.printStackTrace(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 writeElement(Writer writer, Element element)47 private static void writeElement(Writer writer, Element element) 48 throws IOException { 49 writer.write('<'); 50 writer.write(element.getTagName()); 51 52 if (element.hasAttributes()) { 53 NamedNodeMap attributes = element.getAttributes(); 54 for (int i = 0; i < attributes.getLength(); i++) { 55 Attr attribute = (Attr)attributes.item(i); 56 writer.write(" " + attribute.getName()); 57 writer.write("=\"" + attribute.getValue() + "\""); 58 } 59 } 60 61 // FIXME: Might throw ClassCastException 62 SMILElement childElement = (SMILElement) element.getFirstChild(); 63 64 if (childElement != null) { 65 writer.write('>'); 66 67 do { 68 writeElement(writer, childElement); 69 childElement = (SMILElement) childElement.getNextSibling(); 70 } while (childElement != null); 71 72 writer.write("</"); 73 writer.write(element.getTagName()); 74 writer.write('>'); 75 } else { 76 writer.write("/>"); 77 } 78 } 79 } 80 81