From: <lh...@us...> - 2008-11-12 14:03:31
|
Revision: 175 http://tinytim.svn.sourceforge.net/tinytim/?rev=175&view=rev Author: lheuer Date: 2008-11-12 14:03:27 +0000 (Wed, 12 Nov 2008) Log Message: ----------- - Switched back to int[] and IConstruct[] arrays in AbstractMapInputHandler - Added various topic map importers - Added *experimental* XTM writers - Renamed ITopicMapReader to TopicMapReader and reduced the methods to read() - Renamed ITopicMapWriter to TopicMapWriter Modified Paths: -------------- tinytim-mio/trunk/src/main/java/org/tinytim/core/AbstractMapInputHandler.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/CXTMWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/LTMReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/SnelloReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/TMXMLReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLC14NWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTMReader.java Added Paths: ----------- tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMTopicMapReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapImporter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Reader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Writer.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Reader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Writer.java Removed Paths: ------------- tinytim-mio/trunk/src/main/java/org/tinytim/mio/BTMReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapReader.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapWriter.java Modified: tinytim-mio/trunk/src/main/java/org/tinytim/core/AbstractMapInputHandler.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/core/AbstractMapInputHandler.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/core/AbstractMapInputHandler.java 2008-11-12 14:03:27 UTC (rev 175) @@ -46,7 +46,7 @@ */ public abstract class AbstractMapInputHandler implements IMapHandler { - private final int + private static final int INITIAL = 1, TOPIC = 2, ASSOCIATION = 3, @@ -61,13 +61,15 @@ ISA = 12, TYPE = 13; - private final int _CONSTRUCT_SIZE = 6; - private final int _STATE_SIZE = 10; - private final int _SCOPE_SIZE = 6; + private static final int _CONSTRUCT_SIZE = 6; + private static final int _STATE_SIZE = 10; + private static final int _SCOPE_SIZE = 6; - private TopicMapImpl _tm; - private List<Integer> _stateStack; - private List<IConstruct> _constructStack; + private ITopicMap _tm; + private int[] _stateStack; + private int _stateSize; + private IConstruct[] _constructStack; + private int _constructSize; private List<Topic> _scope; protected AbstractMapInputHandler(TopicMap topicMap) { @@ -83,15 +85,17 @@ if (topicMap == null) { throw new IllegalArgumentException("The topic map must not be null"); } - _tm = (TopicMapImpl) topicMap; + _tm = (ITopicMap) topicMap; } /* (non-Javadoc) * @see com.semagia.mio.IMapHandler#startTopicMap() */ public final void startTopicMap() throws MIOException { - _constructStack = CollectionFactory.createList(_CONSTRUCT_SIZE); - _stateStack = CollectionFactory.createList(_STATE_SIZE); + _constructStack = new IConstruct[_CONSTRUCT_SIZE]; + _stateStack = new int[_STATE_SIZE]; + _constructSize = 0; + _stateSize = 0; _scope = CollectionFactory.createList(_SCOPE_SIZE); _enterState(INITIAL, _tm); } @@ -217,7 +221,7 @@ NameImpl name = (NameImpl) _peekConstruct(); IScope scope = variant.getScopeObject(); if (scope.isUnconstrained() || name.getScopeObject() == scope) { - throw new MIOException("The variant has no scope"); + _reportError("The variant has no scope"); } name.addVariant(variant); } @@ -384,7 +388,12 @@ * @param state The state to push ontop of the state stack. */ private void _enterState(int state) { - _stateStack.add(state); + if (_stateSize >= _stateStack.length) { + int[] states = new int[_stateStack.length*2]; + System.arraycopy(_stateStack, 0, states, 0, _stateStack.length); + _stateStack = states; + } + _stateStack[_stateSize++] = state; } /** @@ -396,7 +405,12 @@ */ private void _enterState(int state, IConstruct tmo) { _enterState(state); - _constructStack.add(tmo); + if (_constructSize >= _constructStack.length) { + IConstruct[] constructs = new IConstruct[_constructStack.length*2]; + System.arraycopy(_constructStack, 0, constructs, 0, _constructStack.length); + _constructStack = constructs; + } + _constructStack[_constructSize++] = tmo; } /** @@ -406,10 +420,10 @@ * @throws MIOException If the state is not equals to the current state. */ private void _leaveState(int state) throws MIOException { - final int current = _stateStack.remove(_stateStack.size()-1); - if (state != current) { - _reportError("Unexpected state: " + current + ", expected: " + state); + if (state != _state()) { + _reportError("Unexpected state: " + _state() + ", expected: " + state); } + _stateSize--; } /** @@ -421,7 +435,10 @@ */ private IConstruct _leaveStatePopConstruct(int state) throws MIOException { _leaveState(state); - return _constructStack.remove(_constructStack.size()-1); + final IConstruct construct = _peekConstruct(); + _constructStack[_constructSize] = null; + _constructSize--; + return construct; } /** @@ -430,7 +447,7 @@ * @return The Topic Maps construct. */ private IConstruct _peekConstruct() { - return _constructStack.get(_constructStack.size()-1); + return _constructStack[_constructSize-1]; } /** @@ -448,7 +465,7 @@ * @return The current state. */ private int _state() { - return _stateStack.get(_stateStack.size()-1); + return _stateStack[_stateSize-1]; } /** @@ -477,9 +494,9 @@ * @param target The target topic. */ private void _merge(Topic source, TopicImpl target) { - for (int i=0; i<_constructStack.size(); i++) { - if (_constructStack.get(i) == source) { - _constructStack.set(i, target); + for (int i=0; i <_constructSize; i++) { + if (_constructStack[i] == source) { + _constructStack[i] = target; } } target.mergeIn(source); Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -22,54 +22,95 @@ import org.tmapi.core.TMAPIRuntimeException; import org.tmapi.core.TopicMap; -import org.xml.sax.InputSource; import com.semagia.mio.DeserializerRegistry; import com.semagia.mio.IDeserializer; +import com.semagia.mio.IMapHandler; import com.semagia.mio.MIOException; +import com.semagia.mio.Property; +import com.semagia.mio.Source; import com.semagia.mio.Syntax; /** - * Base class for {@link ITopicMapReader} implementations. - * + * Base class for {@link TopicMapReader} implementations. + * <p> * This class provides a layer to <tt>com.semagia.mio</tt> and handles * the discovery of an appropriate deserializer transparently. + * </p> * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ */ -abstract class AbstractTopicMapReader implements ITopicMapReader { +abstract class AbstractTopicMapReader implements TopicMapReader { protected IDeserializer _deserializer; + private final Source _source; - AbstractTopicMapReader(final TopicMap topicMap, final Syntax syntax) { - _deserializer = DeserializerRegistry.createDeserializer(syntax); - if (_deserializer == null) { - throw new TMAPIRuntimeException("Appropriate deserializer not found for syntax " + syntax.getName()); - } - _deserializer.setMapHandler(new TinyTimMapInputHandler(topicMap)); + /** + * + * + * @param topicMap + * @param syntax + * @param source + * @throws IOException + */ + protected AbstractTopicMapReader(final TopicMap topicMap, final Syntax syntax, + final File source) throws IOException { + this(topicMap, syntax, source, source.toURL().toString()); } - /* (non-Javadoc) - * @see org.tinytim.mio.ITopicMapReader#read(java.io.File, java.lang.String) + /** + * + * + * @param topicMap + * @param syntax + * @param source + * @param docIRI + * @throws IOException */ - public void read(File source, String baseIRI) throws IOException { - read(new FileInputStream(source), baseIRI); + protected AbstractTopicMapReader(final TopicMap topicMap, final Syntax syntax, + final File source, final String docIRI) throws IOException { + this(topicMap, syntax, new Source(new FileInputStream(source), docIRI)); } - /* (non-Javadoc) - * @see org.tinytim.mio.ITopicMapReader#read(java.io.InputStream, java.lang.String) + /** + * + * + * @param topicMap + * @param syntax + * @param source + * @param docIRI */ - public void read(InputStream source, String baseIRI) throws IOException { - read(new InputSource(source), baseIRI); + protected AbstractTopicMapReader(TopicMap topicMap, Syntax syntax, + InputStream source, String docIRI) { + this(topicMap, syntax, new Source(source, docIRI)); } + protected AbstractTopicMapReader(final TopicMap topicMap, final Syntax syntax, + final Source source) { + this(new TinyTimMapInputHandler(topicMap), syntax, source); + } + + protected AbstractTopicMapReader(final IMapHandler handler, final Syntax syntax, + final Source source) { + this(handler, DeserializerRegistry.createDeserializer(syntax), source); + } + + protected AbstractTopicMapReader(final IMapHandler handler, final IDeserializer deserializer, final Source source) { + if (_deserializer == null) { + throw new IllegalArgumentException("Deserializer not found"); + } + _deserializer.setProperty(Property.VALIDATE, Boolean.FALSE); + _deserializer.setMapHandler(handler); + _source = source; + } + /* (non-Javadoc) - * @see org.tinytim.mio.ITopicMapReader#read(org.xml.sax.InputSource, java.lang.String) + * @see org.tinytim.mio.ITopicMapReader#read() */ - public void read(InputSource source, String baseIRI) throws IOException { + public void read() throws IOException { try { - _deserializer.parse(source, baseIRI); + _deserializer.parse(_source); } catch (MIOException ex) { if (ex.getException() instanceof IOException) { @@ -83,5 +124,4 @@ _deserializer = null; } } - } Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapWriter.java (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapWriter.java 2008-11-12 14:03:27 UTC (rev 175) @@ -0,0 +1,56 @@ +/* + * Copyright 2008 Lars Heuer (heuer[at]semagia.com) + * + * 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.tinytim.mio; + +import org.tmapi.core.Locator; +import org.tmapi.core.Topic; + +/** + * + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev:$ - $Date:$ + */ +abstract class AbstractTopicMapWriter implements TopicMapWriter { + + protected final String _baseIRI; + + protected AbstractTopicMapWriter(final String baseIRI) { + _baseIRI = baseIRI; + } + + protected String _getId(Topic tmo) { + String id = null; + for (Locator loc: tmo.getItemIdentifiers()) { + String reference = loc.getReference(); + if (!reference.startsWith(_baseIRI)) { + continue; + } + int fragIdx = reference.indexOf('#'); + if (fragIdx < 0) { + continue; + } + id = reference.substring(fragIdx+1); + if (id.startsWith("id")) { + id = null; + } + if (id != null) { + break; + } + } + return id != null ? id : "id-" + tmo.getId(); + } +} Property changes on: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractTopicMapWriter.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMTopicMapReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMTopicMapReader.java (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMTopicMapReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -0,0 +1,107 @@ +/* + * Copyright 2008 Lars Heuer (heuer[at]semagia.com) + * + * 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.tinytim.mio; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import org.tmapi.core.TopicMap; + +import com.semagia.mio.IMapHandler; +import com.semagia.mio.Property; +import com.semagia.mio.Source; +import com.semagia.mio.Syntax; + +/** + * Common superclass for all XTM readers which provides some additional methods + * to configure the reader. + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev:$ - $Date:$ + */ +abstract class AbstractXTMTopicMapReader extends AbstractTopicMapReader { + + public AbstractXTMTopicMapReader(TopicMap topicMap, Syntax syntax, + File source) throws IOException { + super(topicMap, syntax, source); + } + + public AbstractXTMTopicMapReader(TopicMap topicMap, Syntax syntax, + File source, String docIRI) throws IOException { + super(topicMap, syntax, source, docIRI); + } + + public AbstractXTMTopicMapReader(TopicMap topicMap, Syntax syntax, + InputStream source, String docIRI) { + super(topicMap, syntax, source, docIRI); + } + + public AbstractXTMTopicMapReader(TopicMap topicMap, Syntax syntax, + Source source) { + super(topicMap, syntax, source); + } + + public AbstractXTMTopicMapReader(IMapHandler handler, Syntax syntax, + Source source) { + super(handler, syntax, source); + } + + /** + * Enables / disables processing of the "mergeMap" element. + * <p> + * The reader won't deserialize topic maps referenced by mergeMap if + * this feature is enabled (disabled by default). + * </p> + * + * @param ignore <tt>true</tt> to ignore mergeMap elements, otherwise <tt>false</tt>. + */ + public void setIgnoreMergeMap(boolean ignore) { + _deserializer.setProperty(Property.IGNORE_MERGEMAP, ignore); + } + + /** + * Returns if this reader ignores mergeMap elements. + * + * @return <tt>true</tt> if mergeMap is ignored, otherwise <tt>false</tt>. + */ + public boolean isIgnoringMergeMap() { + return Boolean.TRUE.equals(_deserializer.getProperty(Property.IGNORE_MERGEMAP)); + } + + /** + * Enables / disables validation of the source. + * <p> + * The reader validates the XML document if this feature is enabled + * (disabled by default). + * </p> + * + * @param validate <tt>true</tt> to enable validation, <tt>false</tt> to + * disable validation. + */ + public void setValidation(boolean validate) { + _deserializer.setProperty(Property.VALIDATE, validate); + } + + /** + * Returns if this reader validates the source. + * + * @return <tt>true</tt> if this reader validates the source, otherwise <tt>false</tt>. + */ + public boolean isValidating() { + return Boolean.TRUE.equals(_deserializer.getProperty(Property.VALIDATE)); + } +} Property changes on: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMTopicMapReader.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMWriter.java (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMWriter.java 2008-11-12 14:03:27 UTC (rev 175) @@ -0,0 +1,42 @@ +/* + * Copyright 2008 Lars Heuer (heuer[at]semagia.com) + * + * 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.tinytim.mio; + +import org.xml.sax.Attributes; +import org.xml.sax.helpers.AttributesImpl; + +/** + * + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev:$ - $Date:$ + */ +abstract class AbstractXTMWriter extends AbstractTopicMapWriter { + + protected final Attributes _EMPTY_ATTRS = XMLWriter.EMPTY_ATTRS; + + protected AttributesImpl _attrs; + protected XMLWriter _out; + /** + * + * + * @param baseIRI + */ + public AbstractXTMWriter(String baseIRI) { + super(baseIRI); + } + +} Property changes on: tinytim-mio/trunk/src/main/java/org/tinytim/mio/AbstractXTMWriter.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Deleted: tinytim-mio/trunk/src/main/java/org/tinytim/mio/BTMReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/BTMReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/BTMReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -1,40 +0,0 @@ -/* - * Copyright 2008 Lars Heuer (heuer[at]semagia.com) - * - * 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.tinytim.mio; - -import org.tmapi.core.TopicMap; - -import com.semagia.mio.Syntax; - -/** - * {@link ITopicMapReader} implementation that is able to deserialize - * <a href="http://www.semagia.com/tr/btm/1.0/">Binary Topic Maps (BTM) 1.0</a>. - * - * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev$ - $Date$ - */ -public final class BTMReader extends AbstractTopicMapReader { - - /** - * Constructs a new instance. - * - * @param topicMap The topic map to which the content is added to. - */ - public BTMReader(final TopicMap topicMap) { - super(topicMap, Syntax.BTM); - } - -} Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -15,13 +15,18 @@ */ package org.tinytim.mio; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + import org.tmapi.core.TopicMap; +import com.semagia.mio.Source; import com.semagia.mio.Syntax; /** - * {@link ITopicMapReader} implementation that is able to deserialize - * <a href="http://www.isotopicmaps.org/ctm">Compact Topic Maps (CTM) 1.0</a>. + * {@link TopicMapReader} implementation that is able to deserialize + * <a href="http://www.isotopicmaps.org/ctm/">Compact Topic Maps (CTM) 1.0</a>. * <p> * Note that this reader implements the CTM draft dtd. 2008-05-15. * </p> @@ -35,9 +40,48 @@ * Constructs a new instance. * * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + * @throws IOException If an error occurs. */ - public CTMReader(final TopicMap topicMap) { - super(topicMap, Syntax.CTM); + public CTMReader(final TopicMap topicMap, final File source, final String docIRI) throws IOException { + super(topicMap, Syntax.CTM, source, docIRI); } + /** + * Constructs a new instance. + * <p> + * The <tt>source</tt> is converted into an absolute IRI which will be + * utilised as document IRI + * </p> + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @throws IOException If an error occurs. + */ + public CTMReader(final TopicMap topicMap, final File source) throws IOException { + super(topicMap, Syntax.CTM, source); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + */ + public CTMReader(final TopicMap topicMap, final InputStream source, final String docIRI) { + super(topicMap, Syntax.CTM, source, docIRI); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the serialized topic map from. + */ + public CTMReader(final TopicMap topicMap, final Source source) { + super(topicMap, Syntax.CTM, source); + } + } Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/CXTMWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/CXTMWriter.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/CXTMWriter.java 2008-11-12 14:03:27 UTC (rev 175) @@ -29,7 +29,6 @@ import java.util.Set; import java.util.logging.Logger; -import org.tinytim.core.TopicMapImpl; import org.tinytim.internal.utils.CollectionFactory; import org.tinytim.utils.DuplicateRemovalUtils; import org.tinytim.voc.TMDM; @@ -74,36 +73,35 @@ * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ */ -public final class CXTMWriter implements ITopicMapWriter { +public final class CXTMWriter implements TopicMapWriter { private static final Logger LOG = Logger.getLogger(CXTMWriter.class.getName()); private static final Role[] _EMPTY_ROLES = new Role[0]; - private AttributesImpl _attrs; + private final AttributesImpl _attrs; private Topic _type; private Topic _instance; private Topic _typeInstance; - private XMLC14NWriter _out; + private final XMLC14NWriter _out; private final String _normBase; - private Map<Construct, Integer> _construct2Id; - private Map<Topic, List<Role>> _topic2Roles; - private Map<Locator, String> _locator2Norm; + private final Map<Construct, Integer> _construct2Id; + private final Map<Topic, List<Role>> _topic2Roles; + private final Map<Locator, String> _locator2Norm; + private final Map<Association, Role[]> _assoc2Roles; - private Comparator<Topic> _topicComparator; - private Comparator<Association> _assocComparator; - private Comparator<Role> _roleComparator; - private Comparator<Occurrence> _occComparator; - private Comparator<Name> _nameComparator; - private Comparator<Variant> _variantComparator; - private Comparator<Set<Locator>> _locSetComparator; - private Comparator<Locator> _locComparator; - private Comparator<Set<Topic>> _scopeComparator; + private final Comparator<Topic> _topicComparator; + private final Comparator<Association> _assocComparator; + private final Comparator<Role> _roleComparator; + private final Comparator<Occurrence> _occComparator; + private final Comparator<Name> _nameComparator; + private final Comparator<Variant> _variantComparator; + private final Comparator<Set<Locator>> _locSetComparator; + private final Comparator<Locator> _locComparator; + private final Comparator<Set<Topic>> _scopeComparator; - private Map<Association, Role[]> _assoc2Roles; - /** * Creates a canonicalizer. * @@ -118,6 +116,10 @@ _out = new XMLC14NWriter(out); _attrs = new AttributesImpl(); _normBase = _normalizeBaseLocator(baseLocator); + _construct2Id = CollectionFactory.createIdentityMap(); + _locator2Norm = CollectionFactory.createIdentityMap(); + _assoc2Roles = CollectionFactory.createIdentityMap(); + _topic2Roles = CollectionFactory.createIdentityMap(); _topicComparator = new TopicComparator(); _assocComparator = new AssociationComparator(); _roleComparator = new RoleComparator(); @@ -141,11 +143,7 @@ */ public void write(TopicMap topicMap) throws IOException { DuplicateRemovalUtils.removeDuplicates(topicMap); - _construct2Id = CollectionFactory.createIdentityMap(); - _locator2Norm = CollectionFactory.createIdentityMap(); - _assoc2Roles = CollectionFactory.createIdentityMap(); - _topic2Roles = CollectionFactory.createIdentityMap(); - TypeInstanceIndex typeInstanceIndex = ((TopicMapImpl) topicMap).getIndexManager().getTypeInstanceIndex(); + TypeInstanceIndex typeInstanceIndex = topicMap.getIndex(TypeInstanceIndex.class); if (!typeInstanceIndex.isAutoUpdated()) { typeInstanceIndex.reindex(); } @@ -168,22 +166,22 @@ _out.endElement("topicMap"); _out.newline(); _out.endDocument(); - _out = null; - _attrs = null; - _construct2Id = null; - _locator2Norm = null; - _assoc2Roles = null; - _topic2Roles = null; + _attrs.clear(); + _construct2Id.clear(); + _topic2Roles.clear(); + _locator2Norm.clear(); + _assoc2Roles.clear(); } /** * Returns an unsorted array of topics which should be included into * the output. - * + * <p> * This method may return more topics than {@link TopicMap#getTopics()} * since this method creates virtual topics to model type-instance * relationships properly. - * + * </p> + * * @param topicMap The topic map from which the topic should be serialized. * @param idx A (upto date) type instance index. * @return All topics which must be included into the output. @@ -555,9 +553,10 @@ /** * Serializes the <tt>locators</tt> using the <tt>localName</tt> as * element name. - * + * <p> * If the set of <tt>locators</tt> is empty, this method does nothing. - * + * </p> + * * @param localName The element's name. * @param locators The locators to serialize. * @throws IOException If an error occurs. Deleted: tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -1,66 +0,0 @@ -/* - * Copyright 2008 Lars Heuer (heuer[at]semagia.com) - * - * 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.tinytim.mio; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - -import org.xml.sax.InputSource; - -/** - * This interface represents a reader to deserialize a topic map from a source. - * <p> - * The reader is not meant to be reused and should be thrown away once one - * of the <tt>read</tt> methods were invoked. - * </p> - * - * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev$ - $Date$ - */ -public interface ITopicMapReader { - - /** - * Reads a topic map from <tt>source</tt> using the provided <tt>docIRI</tt> - * to resolve IRIs against. - * - * @param source The source to read the serialized topic map from. - * @param docIRI The IRI which is used to resolve IRIs against. - * @throws IOException If an error occurs. - */ - public void read(InputSource source, String docIRI) throws IOException; - - /** - * Reads a topic map from <tt>source</tt> using the provided <tt>docIRI</tt> - * to resolve IRIs against. - * - * @param source The file to read the serialized topic map from. - * @param docIRI The IRI which is used to resolve IRIs against. - * @throws IOException If an error occurs. - */ - public void read(File source, String docIRI)throws IOException; - - /** - * Reads a topic map from <tt>source</tt> using the provided <tt>docIRI</tt> - * to resolve IRIs against. - * - * @param source The stream to read the serialized topic map from. - * @param docIRI The IRI which is used to resolve IRIs against. - * @throws IOException If an error occurs. - */ - public void read(InputStream source, String docIRI)throws IOException; - -} Deleted: tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapWriter.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/ITopicMapWriter.java 2008-11-12 14:03:27 UTC (rev 175) @@ -1,31 +0,0 @@ -/* - * Copyright 2008 Lars Heuer (heuer[at]semagia.com) - * - * 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.tinytim.mio; - -import java.io.IOException; - -import org.tmapi.core.TopicMap; - -/** - * - * - * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev$ - $Date$ - */ -public interface ITopicMapWriter { - - public void write(TopicMap topicMap) throws IOException; -} Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/LTMReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/LTMReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/LTMReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -15,12 +15,17 @@ */ package org.tinytim.mio; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + import org.tmapi.core.TopicMap; +import com.semagia.mio.Source; import com.semagia.mio.Syntax; /** - * {@link ITopicMapReader} implementation that is able to deserialize the + * {@link TopicMapReader} implementation that is able to deserialize the * <a href="http://www.ontopia.net/download/ltm.html">Linear Topic Map Notation (LTM) 1.3</a>. * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> @@ -32,9 +37,95 @@ * Constructs a new instance. * * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + * @throws IOException If an error occurs. */ - public LTMReader(final TopicMap topicMap) { - super(topicMap, Syntax.LTM); + public LTMReader(final TopicMap topicMap, final File source, final String docIRI) throws IOException { + super(topicMap, Syntax.LTM, source, docIRI); } + /** + * Constructs a new instance. + * <p> + * The <tt>source</tt> is converted into an absolute IRI which will be + * utilised as document IRI + * </p> + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @throws IOException If an error occurs. + */ + public LTMReader(final TopicMap topicMap, final File source) throws IOException { + super(topicMap, Syntax.LTM, source); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + */ + public LTMReader(final TopicMap topicMap, final InputStream source, final String docIRI) { + super(topicMap, Syntax.LTM, source, docIRI); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the serialized topic map from. + */ + public LTMReader(final TopicMap topicMap, final Source source) { + super(topicMap, Syntax.LTM, source); + } + +// /** +// * Enables / disables processing of the "#MERGEMAP" directive. +// * <p> +// * The reader won't deserialize topic maps referenced by mergeMap if +// * this feature is enabled (disabled by default). +// * </p> +// * +// * @param ignore <tt>true</tt> to ignore "#MERGEMAP" directives, +// * otherwise <tt>false</tt>. +// */ +// public void setIgnoreMergeMap(boolean ignore) { +// _deserializer.setProperty(Property.IGNORE_MERGEMAP, ignore); +// } +// +// /** +// * Returns if this reader ignores "#MERGEMAP" directives. +// * +// * @return <tt>true</tt> if "#MERGEMAP" is ignored, otherwise <tt>false</tt>. +// */ +// public boolean isIgnoringMergeMap() { +// Object property = _deserializer.getProperty(Property.IGNORE_MERGEMAP); +// return property instanceof Boolean && Boolean.TRUE.equals(property); +// } + +// /** +// * Enables / disables processing of the "#INCLUDE" directive. +// * <p> +// * The reader won't deserialize topic maps referenced by "#INCLUDE" if +// * this feature is enabled (disabled by default). +// * </p> +// * +// * @param ignore <tt>true</tt> to ignore "#INCLUDE" directives, +// * otherwise <tt>false</tt>. +// */ +// public void setIgnoreInclude(boolean ignore) { +// _deserializer.setProperty(Property.IGNORE_INCLUDE, ignore); +// } +// +// /** +// * Returns if this reader ignores "#INCLUDE" directives. +// * +// * @return <tt>true</tt> if "#INCLUDE" is ignored, otherwise <tt>false</tt>. +// */ +// public boolean isIgnoringInclude() { +// Object property = _deserializer.getProperty(Property.IGNORE_INCLUDE); +// return property instanceof Boolean && Boolean.TRUE.equals(property); +// } } Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/SnelloReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/SnelloReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/SnelloReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -15,12 +15,17 @@ */ package org.tinytim.mio; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + import org.tmapi.core.TopicMap; +import com.semagia.mio.Source; import com.semagia.mio.Syntax; /** - * {@link ITopicMapReader} implementation that is able to deserialize + * {@link TopicMapReader} implementation that is able to deserialize * <a href="http://www.semagia.com/tr/snello/1.0/">Snello Topic Maps (STM) 1.0</a>. * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> @@ -32,9 +37,48 @@ * Constructs a new instance. * * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + * @throws IOException If an error occurs. */ - public SnelloReader(final TopicMap topicMap) { - super(topicMap, Syntax.SNELLO); + public SnelloReader(final TopicMap topicMap, final File source, final String docIRI) throws IOException { + super(topicMap, Syntax.SNELLO, source, docIRI); } + /** + * Constructs a new instance. + * <p> + * The <tt>source</tt> is converted into an absolute IRI which will be + * utilised as document IRI + * </p> + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @throws IOException If an error occurs. + */ + public SnelloReader(final TopicMap topicMap, final File source) throws IOException { + super(topicMap, Syntax.SNELLO, source); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + */ + public SnelloReader(final TopicMap topicMap, final InputStream source, final String docIRI) { + super(topicMap, Syntax.SNELLO, source, docIRI); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the serialized topic map from. + */ + public SnelloReader(final TopicMap topicMap, final Source source) { + super(topicMap, Syntax.SNELLO, source); + } + } Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TMXMLReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TMXMLReader.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/TMXMLReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -15,13 +15,20 @@ */ package org.tinytim.mio; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + import org.tmapi.core.TopicMap; +import com.semagia.mio.Property; +import com.semagia.mio.Source; import com.semagia.mio.Syntax; /** + * {@link TopicMapReader} implementation that is able to deserialize + * <a href="http://www.ontopia.net/topicmaps/tmxml.html">TM/XML</a> topic maps. * - * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> * @version $Rev$ - $Date$ */ @@ -31,9 +38,71 @@ * Constructs a new instance. * * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + * @throws IOException If an error occurs. */ - public TMXMLReader(TopicMap topicMap) { - super(topicMap, Syntax.TMXML); + public TMXMLReader(final TopicMap topicMap, final File source, final String docIRI) throws IOException { + super(topicMap, Syntax.TMXML, source, docIRI); } + /** + * Constructs a new instance. + * <p> + * The <tt>source</tt> is converted into an absolute IRI which will be + * utilised as document IRI + * </p> + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @throws IOException If an error occurs. + */ + public TMXMLReader(final TopicMap topicMap, final File source) throws IOException { + super(topicMap, Syntax.TMXML, source); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the topic map from. + * @param docIRI The document IRI which is used to resolve IRIs against. + */ + public TMXMLReader(final TopicMap topicMap, final InputStream source, final String docIRI) { + super(topicMap, Syntax.TMXML, source, docIRI); + } + + /** + * Constructs a new instance. + * + * @param topicMap The topic map to which the content is added to. + * @param source The source to read the serialized topic map from. + */ + public TMXMLReader(final TopicMap topicMap, final Source source) { + super(topicMap, Syntax.TMXML, source); + } + + /** + * Enables / disables validation of the source. + * <p> + * The reader validates the XML document if this feature is enabled + * (disabled by default). + * </p> + * + * @param validate <tt>true</tt> to enable validation, <tt>false</tt> to + * disable validation. + */ + public void setValidation(boolean validate) { + _deserializer.setProperty(Property.VALIDATE, validate); + } + + /** + * Returns if this reader validates the source. + * + * @return <tt>true</tt> if this reader validates the source, otherwise <tt>false</tt>. + */ + public boolean isValidating() { + return Boolean.TRUE.equals(_deserializer.getProperty(Property.VALIDATE)); + } + } Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java 2008-11-11 14:38:30 UTC (rev 174) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java 2008-11-12 14:03:27 UTC (rev 175) @@ -27,12 +27,11 @@ public final class TinyTimMapInputHandler extends AbstractMapInputHandler { /** - * Intitializes a new <tt>MapInputHandler</tt> instance with the specified - * <tt>topicMap</tt>. + * Intitializes a new instance with the specified <tt>topicMap</tt>. * * @param topicMap The {@link TopicMap} instance. */ - public TinyTimMapInputHandler(final TopicMap topicMap) { + public TinyTimMapInputHandler(TopicMap topicMap) { super(topicMap); } Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapImporter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapImporter.java (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapImporter.java 2008-11-12 14:03:27 UTC (rev 175) @@ -0,0 +1,274 @@ +/* + * Copyright 2008 Lars Heuer (heuer[at]semagia.com) + * + * 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.tinytim.mio; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; + +import org.tmapi.core.TopicMap; +import org.xml.sax.InputSource; + +import com.semagia.mio.Source; +import com.semagia.mio.Syntax; + +/** + * Functions to import serialized topic maps. + * <p> + * This class is kept for backward compatibility, some methods are already + * deprectated, maybe the whole class will be deprecated in the near future; + * use {@link TopicMapReader} and its implementations. Actually, this class has + * become a wrapper around different {@link TopicMapReader} implementations. + * </p> + * <p> + * This class may be deprected since it provides a high-level view on + * {@link TopicMapReader}s. A {@link TopicMapReader} instance may provide + * methods to configure its behaviour while this class does not support any + * configuration. + * </p> + * <p> + * Applications which use this class should possibly implement something + * equivalent or a smarter utility class since this class will never support + * any configuration of {@link TopicMapReader}s. + * </p> + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev$ - $Date$ + */ +public final class TopicMapImporter { + + private TopicMapImporter() { + // noop. + } + + /** + * Reads a XML topic map from <tt>input</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param input The stream to read the serialized topic map from. + * @throws IOException If an error occurs. + */ + public static void importInto(TopicMap topicMap, String docIRI, InputStream input) throws IOException { + _import(Syntax.XTM, topicMap, docIRI, input); + } + + /** + * Reads a topic map from <tt>file</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * + * The syntax of the serialized topic map is guessed by the file name. If + * the file extension gives no indication of the used syntax, XTM is + * assumed. + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param file The file to read the serialized topic map from. + * @throws IOException If an error occurs. + */ + public static void importInto(TopicMap topicMap, String docIRI, File file) throws IOException { + _import(_guessSyntax(file), topicMap, docIRI, new FileInputStream(file)); + } + + /** + * Reads a topic map from <tt>file</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * + * The <tt>syntax</tt> is a string with the abbreviated Topic Maps syntax + * name; i.e. "xtm", "ltm", "ctm". The name is matched case-insensitve, that + * means "xtm" is the same as "xTm", "XTM" etc. + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param file The file to read the serialized topic map from. + * @param syntax The name of the syntax of the encoded topic map. I.e. "xtm". + * @throws IOException If an error occurs. + */ + public static void importInto(TopicMap topicMap, String docIRI, File file, String syntax) throws IOException { + importInto(topicMap, docIRI, new FileInputStream(file), syntax); + } + + /** + * Reads a topic map from <tt>input</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * + * The <tt>syntax</tt> is a string with the abbreviated Topic Maps syntax + * name; i.e. "xtm", "ltm", "ctm". The name is matched case-insensitve, that + * means "xtm" is the same as "xTm", "XTM" etc. + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param input The stream to read the serialized topic map from. + * @param syntax The name of the syntax of the encoded topic map. I.e. "xtm". + * @throws IOException If an error occurs. + */ + public static void importInto(TopicMap topicMap, String docIRI, InputStream input, String syntax) throws IOException { + Syntax syntax_ = Syntax.valueOf(syntax); + if (syntax_ == null) { + throw new RuntimeException("The syntax '" + syntax + "' is unknown"); + } + _import(syntax_, topicMap, docIRI, input); + } + + /** + * Returns a {@link Syntax} instance. + * + * @param file The file to guess the syntax from. + * @return A syntax which matches the file extension or {@link Syntax#XTM} + * if the file extension is not available or gives no indication + * about the used syntax. + */ + private static Syntax _guessSyntax(File file) { + String name = file.getName(); + int i = name.lastIndexOf('.'); + return i == -1 ? Syntax.XTM + : Syntax.forFileExtension(name.substring(i+1), Syntax.XTM); + } + + /** + * Reads a topic map from <tt>input</tt> and adds the content to the + * <tt>topicMap</tt>. + * + * @param syntax A syntax instance. + * @param topicMap A topic map instance. + * @param docIRI The IRI which is used to resolve locators against. + * @param input The source to read the topic map from. + * @throws IOException If an error occurs. + */ + private static void _import(Syntax syntax, TopicMap topicMap, String docIRI, + InputStream input) throws IOException { + _import(syntax, topicMap, new Source(input, docIRI)); + } + + /** + * Reads a topic map from <tt>input</tt> and adds the content to the + * <tt>topicMap</tt>. + * + * @param syntax A syntax instance. + * @param topicMap A topic map instance. + * @param docIRI The IRI which is used to resolve locators against. + * @param input The source to read the topic map from. + * @throws IOException If an error occurs. + */ + private static void _import(Syntax syntax, TopicMap topicMap, Source input) throws IOException { + TopicMapReader tmReader = null; + if (Syntax.XTM.equals(syntax)) { + tmReader = new XTMReader(topicMap, input); + } + else if (Syntax.CTM.equals(syntax)) { + tmReader = new CTMReader(topicMap, input); + } + else if (Syntax.LTM.equals(syntax)) { + tmReader = new LTMReader(topicMap, input); + } + else if (Syntax.TMXML.equals(syntax)) { + tmReader = new TMXMLReader(topicMap, input); + } + else if (Syntax.SNELLO.equals(syntax)) { + tmReader = new SnelloReader(topicMap, input); + } + else if (Syntax.BTM.equals(syntax)) { + tmReader = new BTMReader(topicMap, input); + } + if (tmReader == null) { + throw new IOException("Unknown syntax " + syntax.getName()); + } + tmReader.read(); + } + + /** + * Reads a XML topic map from <tt>input</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param input The source to read the serialized topic map from. + * @throws IOException If an error occurs. + */ + @Deprecated + public static void importInto(TopicMap topicMap, String docIRI, InputSource input) throws IOException { + _import(Syntax.XTM, topicMap, docIRI, input); + } + + /** + * Reads a topic map from <tt>input</tt> and adds the content to the + * specified <tt>topicMap</tt>. The <tt>docIRI</tt> is used to + * resolve IRIs against. + * <p> + * The <tt>syntax</tt> is a string with the abbreviated Topic Maps syntax + * name; i.e. "xtm", "ltm", "ctm". The name is matched case-insensitve, that + * means "xtm" is the same as "xTm", "XTM" etc. + * </p> + * + * @param topicMap The topic map instance which receives the + * Topic Maps constructs. + * @param docIRI The IRI which is used to resolve IRIs against. + * @param input The source to read the serialized topic map from. + * @param syntax The name of the syntax of the encoded topic map. I.e. "xtm". + * @throws IOException If an error occurs. + */ + @Deprecated + public static void importInto(TopicMap topicMap, String docIRI, InputSource input, String syntax) throws IOException { + Syntax syntax_ = Syntax.valueOf(syntax); + if (syntax_ == null) { + throw new RuntimeException("The syntax '" + syntax + "' is unknown"); + } + _import(syntax_, topicMap, docIRI, input); + } + + /** + * Reads a topic map from <tt>input</tt> and adds the content to the + * <tt>topicMap</tt>. + * + * @param syntax A syntax instance. + * @param topicMap A topic map instance. + * @param docIRI The IRI which is used to resolve locators against. + * @param input The source to read the topic map from. + * @throws IOException If an error occurs. + */ + @Deprecated + private static void _import(Syntax syntax, TopicMap topicMap, + String docIRI, InputSource input) throws IOException { + Source src = null; + if (input.getByteStream() != null) { + src = new Source(input.getByteStream(), docIRI, input.getEncoding()); + } + else { + Reader reader = input.getCharacterStream(); + if (reader != null) { + src = new Source(reader, docIRI, input.getEncoding()); + } + else { + src = new Source(input.getSystemId()); + } + } + _import(syntax, topicMap, src); + } + +} Property changes on: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapImporter.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapReader.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapReader.java (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapReader.java 2008-11-12 14:03:27 UTC (rev 175) @@ -0,0 +1,39 @@ +/* + * Copyright 2008 Lars Heuer (heuer[at]semagia.com) + * + * 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.tinytim.mio; + +import java.io.IOException; + +/** + * This interface represents a reader to deserialize a topic map from a source. + * <p> + * The reader is not meant to be reused and should be thrown away once the + * {@link #read()} method was invoked. + * </p> + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev$ - $Date$ + */ +public interface TopicMapReader { + + /** + * Reads a topic map. + * + * @throws IOException If an error occurs. + */ + public void read() throws IOException; + +} Property changes on: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapReader.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TopicMapWrit... [truncated message content] |