Debian Codegraph @

scripting/java/com/sun/star/script/framework/io/XStorageHelper.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package com.sun.star.script.framework.io;
20
21 import com.sun.star.beans.XPropertySet;
22
23 import com.sun.star.container.XNameAccess;
24
25 import com.sun.star.document.XDocumentSubStorageSupplier;
26
27 import com.sun.star.embed.XStorage;
28 import com.sun.star.embed.XTransactedObject;
29
30 import com.sun.star.frame.XModel;
31
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XEventListener;
35
36 import com.sun.star.script.framework.log.LogUtils;
37 import com.sun.star.script.framework.provider.PathUtils;
38
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42
43 import java.io.IOException;
44
45 import java.util.HashMap;
46 import java.util.Map;
47 import java.util.StringTokenizer;
48
49 public class XStorageHelper implements XEventListener {
50
51 XStorage[] xStorages;
52 static Map<String, XModel> modelMap = new HashMap<String, XModel>();
53 XModel xModel = null;
54 private static XStorageHelper listener = new XStorageHelper();
55
56 private XStorageHelper() {}
57
58 public XStorageHelper(String path, int mode,
59 boolean create) throws IOException {
60
61 String modelUrl = null;
62 int indexOfScriptsDir = path.lastIndexOf("Scripts");
63
64 if (indexOfScriptsDir > -1) {
65 modelUrl = path.substring(0, indexOfScriptsDir - 1);
66 path = path.substring(indexOfScriptsDir, path.length());
67 }
68
69 LogUtils.DEBUG("XStorageHelper ctor, path: " + path);
70 this.xModel = getModelForURL(modelUrl);
71
72 try {
73 StringTokenizer tokens = new StringTokenizer(path, "/");
74
75 if (tokens.countTokens() == 0) {
76 throw new IOException("Invalid path");
77 }
78
79 XDocumentSubStorageSupplier xDocumentSubStorageSupplier =
80 UnoRuntime.queryInterface(XDocumentSubStorageSupplier.class,
81 xModel);
82
83 xStorages = new XStorage[tokens.countTokens()];
84
85 LogUtils.DEBUG("XStorageHelper ctor, path chunks length: "
86 + xStorages.length);
87
88 for (int i = 0; i < xStorages.length; i++) {
89 LogUtils.DEBUG("XStorageHelper, processing index " + i);
90 String name = tokens.nextToken();
91 LogUtils.DEBUG("XStorageHelper, getting: " + name);
92 XStorage storage = null;
93
94 if (i == 0) {
95 storage = xDocumentSubStorageSupplier.getDocumentSubStorage(name, mode);
96
97 if (storage == null) {
98 LogUtils.DEBUG("** boo hoo Storage is null ");
99 }
100
101 XPropertySet xProps =
102 UnoRuntime.queryInterface(XPropertySet.class, storage);
103
104 if (xProps != null) {
105
106 String mediaType =
107 AnyConverter.toString(xProps.getPropertyValue("MediaType"));
108
109 LogUtils.DEBUG("***** media type is " + mediaType);
110
111 if (!mediaType.equals("scripts")) {
112 xProps.setPropertyValue("MediaType", "scripts");
113 }
114 }
115 } else {
116
117 XNameAccess xNameAccess =
118 UnoRuntime.queryInterface(XNameAccess.class, xStorages[i - 1]);
119
120 if (xNameAccess == null) {
121 disposeObject();
122 throw new IOException("No name access " + name);
123 } else if (!xNameAccess.hasByName(name)
124 || !xStorages[i - 1].isStorageElement(name)) {
125 if (!create) {
126 disposeObject();
127 throw new IOException("No subdir: " + name);
128 } else {
129 // attempt to create new storage
130 LogUtils.DEBUG("Attempt to create new storage for "
131 + name);
132 }
133 }
134
135 storage = xStorages[i - 1].openStorageElement(
136 name, mode);
137 }
138
139 if (storage == null) {
140 disposeObject();
141 throw new IOException("storage not found: " + name);
142 }
143
144 xStorages[i] = storage;
145
146 }
147 } catch (com.sun.star.io.IOException ioe) {
148 disposeObject();
149 } catch (com.sun.star.uno.Exception ex1) {
150 disposeObject();
151 IOException ex2 = new IOException();
152 ex2.initCause(ex1);
153 throw ex2;
154 }
155 }
156
157 public synchronized static void addNewModel(XModel model) {
158 // TODO needs to cater for model for untitled document
159 modelMap.put(PathUtils.getOidForModel(model), model);
160 XComponent xComp = UnoRuntime.queryInterface(XComponent.class, model);
161
162 if (xComp != null) {
163 try {
164 xComp.addEventListener(listener);
165 } catch (Exception e) {
166 // What TODO here ?
167 LogUtils.DEBUG(LogUtils.getTrace(e));
168 }
169 }
170 }
171
172 public void disposing(EventObject Source) {
173 XModel model = UnoRuntime.queryInterface(XModel.class, Source.Source);
174
175 if (model != null) {
176 LogUtils.DEBUG(" Disposing doc " + model.getURL());
177 modelMap.remove(PathUtils.getOidForModel(model));
178 }
179 }
180
181 public XStorage getStorage() {
182 return xStorages[ xStorages.length - 1 ];
183 }
184
185 public XModel getModel() {
186 return xModel;
187 }
188
189 public void disposeObject() {
190 disposeObject(false);
191 }
192
193 public void disposeObject(boolean shouldCommit) {
194 LogUtils.DEBUG("In disposeObject");
195
196 for (int i = xStorages.length - 1 ; i > -1; i--) {
197 LogUtils.DEBUG("In disposeObject disposing storage " + i);
198
199 try {
200 XStorage xStorage = xStorages[i];
201
202 if (shouldCommit) {
203 commit(xStorage);
204 }
205
206 disposeObject(xStorage);
207 LogUtils.DEBUG("In disposeObject disposed storage " + i);
208 } catch (Exception ignore) {
209 LogUtils.DEBUG("Exception disposing storage " + i);
210 }
211
212 }
213
214 }
215
216 public static void disposeObject(XInterface xInterface) {
217 if (xInterface == null) {
218 return;
219 }
220
221 XComponent xComponent =
222 UnoRuntime.queryInterface(XComponent.class, xInterface);
223
224 if (xComponent == null) {
225 return;
226 }
227
228 xComponent.dispose();
229 }
230
231 public static void commit(XInterface xInterface) {
232
233 XTransactedObject xTrans =
234 UnoRuntime.queryInterface(XTransactedObject.class, xInterface);
235
236 if (xTrans != null) {
237 try {
238 xTrans.commit();
239 } catch (Exception e) {
240 LogUtils.DEBUG("Something went belly up exception: " + e);
241 }
242 }
243 }
244
245 public XModel getModelForURL(String url) {
246 //TODO does not cater for untitled documents
247 return modelMap.get(url);
248 }
249 }