1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.help.internal.toc;
15 
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 
20 import javax.xml.parsers.ParserConfigurationException;
21 
22 import org.eclipse.help.internal.dynamic.DocumentReader;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.helpers.DefaultHandler;
25 
26 public class TocFileParser extends DefaultHandler {
27 
28 	private DocumentReader reader;
29 
parse(TocFile tocFile)30 	public TocContribution parse(TocFile tocFile) throws IOException, SAXException, ParserConfigurationException {
31 		if (reader == null) {
32 			reader = new DocumentReader();
33 		}
34 		try (InputStream in = tocFile.getInputStream()) {
35 			if (in != null) {
36 				Toc toc = (Toc) reader.read(in);
37 				TocContribution contribution = new TocContribution();
38 				contribution.setCategoryId(tocFile.getCategory());
39 				contribution.setContributorId(tocFile.getPluginId());
40 				contribution.setExtraDocuments(DocumentFinder.collectExtraDocuments(tocFile));
41 				contribution.setId(HrefUtil.normalizeHref(tocFile.getPluginId(), tocFile.getFile()));
42 				contribution.setLocale(tocFile.getLocale());
43 				contribution.setToc(toc);
44 				contribution.setPrimary(tocFile.isPrimary());
45 				return contribution;
46 			} else {
47 				throw new FileNotFoundException();
48 			}
49 		}
50 	}
51 }
52