Java tutorial
/* * Copyright (C) 2012 Nicolas A. Brard-Nault * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sitemap4j.dom4j; import java.io.InputStream; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; import org.sitemap4j.reader.RawSiteMap; import com.google.common.base.Function; import com.google.common.base.Optional; abstract class Dom4jHelper { static Document getDocumentFromInputStream(final InputStream stream) throws UnableToParseStreamException { final SAXReader reader = new SAXReader(); final Document document; try { document = reader.read(stream); } catch (final DocumentException e) { throw new UnableToParseStreamException(e); } return document; } static Function<InputStream, Document> representationFunction() { return new Function<InputStream, Document>() { @Override public Document apply(final InputStream input) { try { return getDocumentFromInputStream(input); } catch (UnableToParseStreamException e) { throw new RuntimeException(e); } } }; } static Document getDocumentFromRawSiteMap(final RawSiteMap rawSiteMap) { if (!rawSiteMap.hasRepresentation(Document.class)) { rawSiteMap.addRepresentation(representationFunction(), Optional.of(Document.class)); } return rawSiteMap.getRepresentation(Document.class).get(); } private Dom4jHelper() { } }