1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.export.xml;
34 
35 import com.jme3.asset.AssetInfo;
36 import com.jme3.asset.AssetManager;
37 import com.jme3.export.InputCapsule;
38 import com.jme3.export.JmeImporter;
39 import com.jme3.export.Savable;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import javax.xml.parsers.DocumentBuilderFactory;
45 import javax.xml.parsers.ParserConfigurationException;
46 import org.xml.sax.SAXException;
47 
48 /**
49  * Part of the jME XML IO system as introduced in the google code jmexml project.
50  * @author Kai Rabien (hevee) - original author of the code.google.com jmexml project
51  * @author Doug Daniels (dougnukem) - adjustments for jME 2.0 and Java 1.5
52  */
53 public class XMLImporter implements JmeImporter {
54 
55     private AssetManager assetManager;
56     private DOMInputCapsule domIn;
57     int formatVersion = 0;
58 
XMLImporter()59     public XMLImporter() {
60     }
61 
getFormatVersion()62     public int getFormatVersion() {
63         return formatVersion;
64     }
65 
getAssetManager()66     public AssetManager getAssetManager(){
67         return assetManager;
68     }
69 
setAssetManager(AssetManager assetManager)70     public void setAssetManager(AssetManager assetManager){
71         this.assetManager = assetManager;
72     }
73 
load(AssetInfo info)74     public Object load(AssetInfo info) throws IOException{
75         assetManager = info.getManager();
76         InputStream in = info.openStream();
77         Savable obj = load(in);
78         in.close();
79         return obj;
80     }
81 
load(File f)82     public Savable load(File f) throws IOException {
83         FileInputStream fis = null;
84         try {
85             fis = new FileInputStream(f);
86             Savable sav = load(fis);
87             return sav;
88         } finally {
89             if (fis != null) fis.close();
90         }
91     }
92 
load(InputStream f)93     public Savable load(InputStream f) throws IOException {
94         try {
95             domIn = new DOMInputCapsule(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f), this);
96             return domIn.readSavable(null, null);
97         } catch (SAXException e) {
98             IOException ex = new IOException();
99             ex.initCause(e);
100             throw ex;
101         } catch (ParserConfigurationException e) {
102             IOException ex = new IOException();
103             ex.initCause(e);
104             throw ex;
105         }
106     }
107 
getCapsule(Savable id)108     public InputCapsule getCapsule(Savable id) {
109         return domIn;
110     }
111 
getInstance()112     public static XMLImporter getInstance() {
113         return new XMLImporter();
114     }
115 
116 }
117