001: /*
002: * Copyright 2007 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.publisher.serverimpl.requestmodel;
017:
018: import org.xml.sax.ContentHandler;
019: import org.xml.sax.InputSource;
020: import org.xml.sax.Attributes;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.helpers.DefaultHandler;
023: import org.xml.sax.helpers.AttributesImpl;
024: import org.outerj.daisy.repository.Version;
025: import org.outerj.daisy.repository.Part;
026: import org.outerj.daisy.repository.schema.RepositorySchema;
027: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
028: import org.outerj.daisy.publisher.serverimpl.PublisherImpl;
029:
030: import javax.xml.parsers.SAXParser;
031: import java.io.InputStream;
032: import java.io.IOException;
033: import java.util.Set;
034: import java.util.HashSet;
035: import java.util.Arrays;
036: import java.text.Collator;
037:
038: public class IdsRequest extends AbstractRequest {
039: public IdsRequest(LocationInfo locationInfo) {
040: super (locationInfo);
041: }
042:
043: public void processInt(ContentHandler contentHandler,
044: PublisherContext publisherContext) throws Exception {
045: String[] ids = extractIds(publisherContext);
046: Arrays.sort(ids, Collator.getInstance(publisherContext
047: .getLocale()));
048:
049: AttributesImpl emptyAttrs = new AttributesImpl();
050:
051: contentHandler.startElement(PublisherImpl.NAMESPACE, "ids",
052: "p:ids", emptyAttrs);
053:
054: for (String id : ids) {
055: contentHandler.startElement(PublisherImpl.NAMESPACE, "id",
056: "p:id", emptyAttrs);
057: contentHandler.characters(id.toCharArray(), 0, id.length());
058: contentHandler.endElement(PublisherImpl.NAMESPACE, "id",
059: "p:id");
060: }
061:
062: contentHandler.endElement(PublisherImpl.NAMESPACE, "ids",
063: "p:ids");
064: }
065:
066: private String[] extractIds(PublisherContext publisherContext)
067: throws Exception {
068: Version version = publisherContext.getVersion();
069: InputStream is = null;
070: try {
071: SAXParser parser = LocalSAXParserFactory
072: .getSAXParserFactory().newSAXParser();
073: Set<String> ids = new HashSet<String>();
074: Part[] parts = version.getParts().getArray();
075: RepositorySchema schema = publisherContext.getRepository()
076: .getRepositorySchema();
077: for (Part part : parts) {
078: if (part.getMimeType().equals("text/xml")
079: && schema.getPartTypeById(part.getTypeId(),
080: false).isDaisyHtml()) {
081: is = part.getDataStream();
082: try {
083: parser.parse(new InputSource(is),
084: new IdCollector(ids));
085: } catch (SAXException e) {
086: // ignore
087: } catch (IOException e) {
088: // ignore
089: }
090: is.close();
091: }
092: }
093: return ids.toArray(new String[0]);
094: } finally {
095: if (is != null)
096: try {
097: is.close();
098: } catch (Throwable e) {
099: publisherContext.getLogger().error(
100: "Error closing part input stream.", e);
101: }
102: }
103: }
104:
105: private static class IdCollector extends DefaultHandler {
106: private final Set<String> ids;
107:
108: public IdCollector(Set<String> ids) {
109: this .ids = ids;
110: }
111:
112: public void startElement(String uri, String localName,
113: String qName, Attributes attributes)
114: throws SAXException {
115: String id = attributes.getValue("id");
116: if (id != null)
117: ids.add(id);
118: }
119: }
120: }
|