001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.tools.importexport.import_;
017:
018: import org.outerj.daisy.repository.AccessException;
019: import org.outerj.daisy.repository.DocumentLockedException;
020: import org.outerj.daisy.tools.importexport.model.ImpExpVariantKey;
021: import org.outerj.daisy.tools.importexport.import_.documents.DocumentImportResult;
022: import org.xml.sax.ContentHandler;
023: import org.xml.sax.SAXException;
024: import org.xml.sax.helpers.AttributesImpl;
025:
026: import java.io.StringWriter;
027: import java.io.PrintWriter;
028: import java.util.List;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: /**
034: * Provides some base functionality that is likely useful for
035: * different ImportListener implementations.
036: */
037: public abstract class BaseImportListener implements ImportListener {
038: private List<ImpExpVariantKey> failedBecausePermissionDenied = new ArrayList<ImpExpVariantKey>();
039: private List<ImpExpVariantKey> failedBecauseLockedDocument = new ArrayList<ImpExpVariantKey>();
040: private List<FailureInfo> failedOtherReason = new ArrayList<FailureInfo>();
041: private List<SuccessInfo> succeeded = new ArrayList<SuccessInfo>();
042:
043: public void permissionDenied(ImpExpVariantKey variantKey,
044: AccessException e) throws Exception {
045: failedBecausePermissionDenied.add(variantKey);
046: }
047:
048: public void lockedDocument(ImpExpVariantKey variantKey,
049: DocumentLockedException e) throws Exception {
050: failedBecauseLockedDocument.add(variantKey);
051: }
052:
053: public void failed(ImpExpVariantKey variantKey, Throwable e)
054: throws Exception {
055: StringBuilder errorDescription = new StringBuilder(200);
056: Throwable current = e;
057: do {
058: if (errorDescription.length() > 0)
059: errorDescription.append(" --> ");
060: errorDescription.append(current.getMessage());
061: current = current.getCause();
062: } while (current != null);
063:
064: String stackTrace;
065: if (includeFullStackTracesOfFailures()) {
066: StringWriter exceptionWriter = new StringWriter();
067: e.printStackTrace(new PrintWriter(exceptionWriter));
068: stackTrace = exceptionWriter.toString();
069: } else {
070: stackTrace = getStackTraceDisabledMessage();
071: }
072:
073: failedOtherReason.add(new FailureInfo(variantKey,
074: errorDescription.toString(), stackTrace));
075: }
076:
077: protected abstract boolean includeFullStackTracesOfFailures();
078:
079: protected abstract String getStackTraceDisabledMessage();
080:
081: public void success(ImpExpVariantKey variantKey,
082: DocumentImportResult result) {
083: succeeded.add(new SuccessInfo(variantKey, result));
084: }
085:
086: public Collection<ImpExpVariantKey> getFailedBecauseAccessDenied() {
087: return failedBecausePermissionDenied;
088: }
089:
090: public Collection<ImpExpVariantKey> getFailedBecauseLockedDocument() {
091: return failedBecauseLockedDocument;
092: }
093:
094: public Collection<FailureInfo> getFailedDocuments() {
095: return failedOtherReason;
096: }
097:
098: public Collection<SuccessInfo> getSucceeded() {
099: return succeeded;
100: }
101:
102: public static class FailureInfo {
103: private final ImpExpVariantKey variantKey;
104: private final String errorDescription;
105: private final String stackTrace;
106:
107: public FailureInfo(ImpExpVariantKey variantKey,
108: String errorDescription, String stackTrace) {
109: this .variantKey = variantKey;
110: this .errorDescription = errorDescription;
111: this .stackTrace = stackTrace;
112: }
113:
114: public ImpExpVariantKey getVariantKey() {
115: return variantKey;
116: }
117:
118: public String getErrorDescription() {
119: return errorDescription;
120: }
121:
122: public String getStackTrace() {
123: return stackTrace;
124: }
125: }
126:
127: public static class SuccessInfo {
128: private final ImpExpVariantKey variantKey;
129: private final DocumentImportResult result;
130:
131: public SuccessInfo(ImpExpVariantKey variantKey,
132: DocumentImportResult result) {
133: this .variantKey = variantKey;
134: this .result = result;
135: }
136:
137: public ImpExpVariantKey getVariantKey() {
138: return variantKey;
139: }
140:
141: public DocumentImportResult getResult() {
142: return result;
143: }
144: }
145:
146: /**
147: * Allows to produce a piece of SAX with information about the imported schema.
148: * This will be included in the result generated by {@link #generateSax(org.xml.sax.ContentHandler)}.
149: * This is optional, the implementation can be empty.
150: */
151: public abstract void generateSchemaSaxFragment(
152: ContentHandler contentHandler) throws SAXException;
153:
154: public void generateSax(ContentHandler contentHandler)
155: throws SAXException {
156: contentHandler.startDocument();
157: contentHandler.startElement("", IMPORT_RESULT_EL,
158: IMPORT_RESULT_EL, new AttributesImpl());
159:
160: generateSchemaSaxFragment(contentHandler);
161:
162: if (failedBecauseLockedDocument.size() > 0) {
163: contentHandler.startElement("", FAILED_BECAUSE_LOCKED_EL,
164: FAILED_BECAUSE_LOCKED_EL, new AttributesImpl());
165:
166: for (ImpExpVariantKey variantKey : failedBecauseLockedDocument) {
167: AttributesImpl attrs = new AttributesImpl();
168: attrs.addAttribute("", ID_ATTR, ID_ATTR, "CDATA",
169: variantKey.getDocumentId());
170: attrs.addAttribute("", BRANCH_ATTR, BRANCH_ATTR,
171: "CDATA", variantKey.getBranch());
172: attrs.addAttribute("", LANG_ATTR, LANG_ATTR, "CDATA",
173: variantKey.getLanguage());
174: contentHandler.startElement("", DOCUMENT_EL,
175: DOCUMENT_EL, attrs);
176: contentHandler.endElement("", DOCUMENT_EL, DOCUMENT_EL);
177: }
178: contentHandler.endElement("", FAILED_BECAUSE_LOCKED_EL,
179: FAILED_BECAUSE_LOCKED_EL);
180: }
181:
182: if (failedBecausePermissionDenied.size() > 0) {
183: contentHandler.startElement("",
184: FAILED_BECAUSE_ACCESS_DENIED_EL,
185: FAILED_BECAUSE_ACCESS_DENIED_EL,
186: new AttributesImpl());
187:
188: AttributesImpl attrs = new AttributesImpl();
189: for (ImpExpVariantKey variantKey : failedBecausePermissionDenied) {
190: attrs.clear();
191: attrs.addAttribute("", ID_ATTR, ID_ATTR, "CDATA",
192: variantKey.getDocumentId());
193: attrs.addAttribute("", BRANCH_ATTR, BRANCH_ATTR,
194: "CDATA", variantKey.getBranch());
195: attrs.addAttribute("", LANG_ATTR, LANG_ATTR, "CDATA",
196: variantKey.getLanguage());
197: contentHandler.startElement("", DOCUMENT_EL,
198: DOCUMENT_EL, attrs);
199: contentHandler.endElement("", DOCUMENT_EL, DOCUMENT_EL);
200: }
201: contentHandler.endElement("",
202: FAILED_BECAUSE_ACCESS_DENIED_EL,
203: FAILED_BECAUSE_ACCESS_DENIED_EL);
204: }
205:
206: if (failedOtherReason.size() > 0) {
207: contentHandler.startElement("", FAILURES_EL, FAILURES_EL,
208: new AttributesImpl());
209:
210: AttributesImpl attrs = new AttributesImpl();
211: for (FailureInfo failureInfo : failedOtherReason) {
212: attrs.clear();
213: attrs.addAttribute("", ID_ATTR, ID_ATTR, "CDATA",
214: failureInfo.getVariantKey().getDocumentId());
215: attrs.addAttribute("", BRANCH_ATTR, BRANCH_ATTR,
216: "CDATA", failureInfo.getVariantKey()
217: .getBranch());
218: attrs.addAttribute("", LANG_ATTR, LANG_ATTR, "CDATA",
219: failureInfo.getVariantKey().getLanguage());
220: contentHandler.startElement("", DOCUMENT_EL,
221: DOCUMENT_EL, attrs);
222:
223: contentHandler.startElement("", "description",
224: "description", new AttributesImpl());
225: contentHandler.characters(failureInfo
226: .getErrorDescription().toCharArray(), 0,
227: failureInfo.getErrorDescription().length());
228: contentHandler.endElement("", "description",
229: "description");
230:
231: contentHandler.startElement("", "stackTrace",
232: "stackTrace", new AttributesImpl());
233: contentHandler.characters(failureInfo.getStackTrace()
234: .toCharArray(), 0, failureInfo.getStackTrace()
235: .length());
236: contentHandler.endElement("", "stackTrace",
237: "stackTrace");
238:
239: contentHandler.endElement("", DOCUMENT_EL, DOCUMENT_EL);
240: }
241: contentHandler.endElement("", FAILURES_EL, FAILURES_EL);
242: }
243:
244: if (succeeded.size() > 0) {
245: contentHandler.startElement("", SUCCEEDED_EL, SUCCEEDED_EL,
246: new AttributesImpl());
247:
248: Iterator it = succeeded.iterator();
249: AttributesImpl attrs = new AttributesImpl();
250: while (it.hasNext()) {
251: SuccessInfo sucessInfo = (SuccessInfo) it.next();
252: ImpExpVariantKey variantKey = sucessInfo
253: .getVariantKey();
254: attrs.clear();
255: attrs.addAttribute("", ID_ATTR, ID_ATTR, "CDATA",
256: variantKey.getDocumentId());
257: attrs.addAttribute("", BRANCH_ATTR, BRANCH_ATTR,
258: "CDATA", variantKey.getBranch());
259: attrs.addAttribute("", LANG_ATTR, LANG_ATTR, "CDATA",
260: variantKey.getLanguage());
261: attrs.addAttribute("", "result", "result", "CDATA",
262: sucessInfo.getResult().toString());
263: contentHandler.startElement("", DOCUMENT_EL,
264: DOCUMENT_EL, attrs);
265: contentHandler.endElement("", DOCUMENT_EL, DOCUMENT_EL);
266: }
267: contentHandler.endElement("", SUCCEEDED_EL, SUCCEEDED_EL);
268: }
269:
270: contentHandler.endElement("", IMPORT_RESULT_EL,
271: IMPORT_RESULT_EL);
272: contentHandler.endDocument();
273: }
274:
275: private static final String IMPORT_RESULT_EL = "importResult";
276: private static final String FAILED_BECAUSE_LOCKED_EL = "failedBecauseLocked";
277: private static final String FAILED_BECAUSE_ACCESS_DENIED_EL = "failedBecauseAccessDenied";
278: private static final String FAILURES_EL = "failed";
279: private static final String SUCCEEDED_EL = "succeeded";
280: private static final String DOCUMENT_EL = "document";
281: private static final String ID_ATTR = "id";
282: private static final String BRANCH_ATTR = "branch";
283: private static final String LANG_ATTR = "language";
284: }
|