01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.books.store;
17:
18: import org.outerj.daisy.books.store.impl.XmlUtil;
19: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
20: import org.outerx.daisy.x10Bookstoremeta.BookAclDocument;
21: import org.apache.xmlbeans.XmlOptions;
22:
23: import java.io.InputStream;
24: import java.io.Reader;
25: import java.util.List;
26:
27: /**
28: * Builds a BookAcl object from its XML description.
29: */
30: public class BookAclBuilder {
31: public static BookAcl build(InputStream is) throws Exception {
32: try {
33: XmlOptions xmlOptions = new XmlOptions()
34: .setLoadUseXMLReader(LocalSAXParserFactory
35: .newXmlReader());
36: BookAclDocument aclDocument = BookAclDocument.Factory
37: .parse(is, xmlOptions);
38: return build(aclDocument);
39: } finally {
40: if (is != null)
41: is.close();
42: }
43: }
44:
45: public static BookAcl build(Reader reader) throws Exception {
46: try {
47: BookAclDocument aclDocument = BookAclDocument.Factory
48: .parse(reader);
49: return build(aclDocument);
50: } finally {
51: if (reader != null)
52: reader.close();
53: }
54: }
55:
56: public static BookAcl build(BookAclDocument aclDocument)
57: throws Exception {
58: String errors = XmlUtil.validate(aclDocument);
59: if (errors != null)
60: throw new Exception(
61: "The ACL XML is not valid according to its XML Schema, encountered errors: "
62: + errors);
63:
64: List<BookAclDocument.BookAcl.BookAclEntry> entriesXml = aclDocument
65: .getBookAcl().getBookAclEntryList();
66: BookAclEntry[] entries = new BookAclEntry[entriesXml.size()];
67: for (int i = 0; i < entriesXml.size(); i++) {
68: BookAclDocument.BookAcl.BookAclEntry entryXml = entriesXml
69: .get(i);
70: BookAclSubjectType subjectType = BookAclSubjectType
71: .fromString(entryXml.getSubjectType().toString());
72: BookAclActionType readPermission = BookAclActionType
73: .fromString(entryXml.getPermRead().toString());
74: BookAclActionType managePermission = BookAclActionType
75: .fromString(entryXml.getPermManage().toString());
76: entries[i] = new BookAclEntry(subjectType, entryXml
77: .getSubjectValue(), readPermission,
78: managePermission);
79: }
80:
81: return new BookAcl(entries);
82: }
83: }
|