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.impl;
17:
18: import org.outerj.daisy.books.store.*;
19: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
20: import org.outerx.daisy.x10Bookstoremeta.PublicationsInfoDocument;
21: import org.apache.xmlbeans.XmlOptions;
22:
23: import java.io.InputStream;
24: import java.util.List;
25:
26: /**
27: * Builds a PublicationsInfo object from its XML description.
28: */
29: public class PublicationsInfoBuilder {
30: public static PublicationsInfo build(InputStream is)
31: throws Exception {
32: try {
33: XmlOptions xmlOptions = new XmlOptions()
34: .setLoadUseXMLReader(LocalSAXParserFactory
35: .newXmlReader());
36: return build(PublicationsInfoDocument.Factory.parse(is,
37: xmlOptions));
38: } finally {
39: is.close();
40: }
41: }
42:
43: public static PublicationsInfo build(
44: PublicationsInfoDocument publicationsInfoDocument)
45: throws Exception {
46: String errors = XmlUtil.validate(publicationsInfoDocument);
47: if (errors != null)
48: throw new Exception(
49: "The publications info XML is not valid according to its XML Schema, encountered errors: "
50: + errors);
51:
52: List<PublicationsInfoDocument.PublicationsInfo.PublicationInfo> infosXml = publicationsInfoDocument
53: .getPublicationsInfo().getPublicationInfoList();
54: PublicationInfo[] infos = new PublicationInfo[infosXml.size()];
55: for (int i = 0; i < infos.length; i++) {
56: PublicationsInfoDocument.PublicationsInfo.PublicationInfo infoXml = infosXml
57: .get(i);
58: infos[i] = new PublicationInfo(infoXml.getName(), infoXml
59: .getLabel(), infoXml.getStartResource(), infoXml
60: .getPackage(), infoXml.getPublishedBy(), infoXml
61: .getPublishedOn().getTime());
62: }
63:
64: return new PublicationsInfo(infos);
65: }
66: }
|