From: <lh...@us...> - 2009-07-02 13:16:52
|
Revision: 304 http://tinytim.svn.sourceforge.net/tinytim/?rev=304&view=rev Author: lheuer Date: 2009-07-02 12:24:57 +0000 (Thu, 02 Jul 2009) Log Message: ----------- Preparation for the final release of tinyTiM 1.5. Yes. Markus, final. Aus die Maus. ;) Modified Paths: -------------- tinytim/tags/release-1_5_0/CHANGES.txt tinytim/tags/release-1_5_0/build.properties tinytim/tags/release-1_5_0/build.xml Added Paths: ----------- tinytim/tags/release-1_5_0/lib/semagia-mio-0.9.4.jar tinytim/tags/release-1_5_0/lib/trove-2.0.4.jar tinytim/tags/release-1_5_0/src/main/java/org/tinytim/mio/ tinytim/tags/release-1_5_0/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java tinytim/tags/release-1_5_0/src/test/java/org/tinytim/mio/ tinytim/tags/release-1_5_0/src/test/java/org/tinytim/mio/TestTinyTimMapInputHandler.java Removed Paths: ------------- tinytim/tags/release-1_5_0/lib/trove-2.0.3.jar Modified: tinytim/tags/release-1_5_0/CHANGES.txt =================================================================== --- tinytim/tags/release-1_5_0/CHANGES.txt 2009-07-02 11:48:24 UTC (rev 303) +++ tinytim/tags/release-1_5_0/CHANGES.txt 2009-07-02 12:24:57 UTC (rev 304) @@ -2,6 +2,15 @@ Changes Log =========== +1.5.0 (02.07.2009) +------------------ +This is the final release of tinyTiM for TMAPI 1.0 + +Bugfixes: +--------- +* Bug #2561306 -- Move TinyTimMapInputHandler to the core + + 1.5.0 b2 (20.11.2008) ----------------------- Bugfixes: Modified: tinytim/tags/release-1_5_0/build.properties =================================================================== --- tinytim/tags/release-1_5_0/build.properties 2009-07-02 11:48:24 UTC (rev 303) +++ tinytim/tags/release-1_5_0/build.properties 2009-07-02 12:24:57 UTC (rev 304) @@ -1,4 +1,4 @@ version=1.5.0 -version_suffix=b2 +version_suffix= debug=off optimize=on Modified: tinytim/tags/release-1_5_0/build.xml =================================================================== --- tinytim/tags/release-1_5_0/build.xml 2009-07-02 11:48:24 UTC (rev 303) +++ tinytim/tags/release-1_5_0/build.xml 2009-07-02 12:24:57 UTC (rev 304) @@ -29,7 +29,8 @@ <property name="dir.res" value="${basedir}/src/main/resources"/> <property name="lib.junit" value="${dir.lib}/junit-4.4.jar"/> - <property name="lib.trove" value="${dir.lib}/trove-2.0.3.jar"/> + <property name="lib.mio" value="${dir.lib}/semagia-mio-0.9.4.jar"/> + <property name="lib.trove" value="${dir.lib}/trove-2.0.4.jar"/> <property name="lib.tmapi" value="${dir.lib}/tmapi-1_0SP1.jar"/> <property name="lib.tmapi.tests" value="${dir.lib}/tmapi-test-1_0SP1.jar"/> @@ -93,6 +94,7 @@ <pathelement location="${lib.tmapi}"/> <pathelement location="${lib.tmapi.tests}"/> <pathelement location="${lib.trove}"/> + <pathelement location="${lib.mio}"/> </classpath> <src path="${dir.test}"/> </javac> @@ -105,6 +107,7 @@ <pathelement location="${lib.tmapi}"/> <pathelement location="${lib.tmapi.tests}"/> <pathelement location="${lib.trove}"/> + <pathelement location="${lib.mio}"/> </classpath> <formatter type="brief" usefile="false"/> <batchtest fork="no" todir="${dir.build}"> @@ -127,6 +130,7 @@ <classpath> <pathelement location="${lib.tmapi}"/> <pathelement location="${lib.trove}"/> + <pathelement location="${lib.mio}"/> </classpath> <src path="${dir.src}"/> </javac> @@ -193,7 +197,14 @@ <fileset dir="${dir.test}"/> </copy> <copy todir="${dir.dist}/lib"> - <fileset dir="${dir.lib}"/> + <fileset dir="${dir.lib}"> + <include name="tmapi*"/> + <include name="junit*"/> + <include name="trove*"/> + <include name="LICENSE.tmapi*"/> + <include name="LICENSE.junit*"/> + <include name="LICENSE.trove*"/> + </fileset> </copy> <copy todir="${dir.dist}" file="CHANGES.txt"/> Added: tinytim/tags/release-1_5_0/lib/semagia-mio-0.9.4.jar =================================================================== (Binary files differ) Property changes on: tinytim/tags/release-1_5_0/lib/semagia-mio-0.9.4.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Deleted: tinytim/tags/release-1_5_0/lib/trove-2.0.3.jar =================================================================== (Binary files differ) Added: tinytim/tags/release-1_5_0/lib/trove-2.0.4.jar =================================================================== (Binary files differ) Property changes on: tinytim/tags/release-1_5_0/lib/trove-2.0.4.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: tinytim/tags/release-1_5_0/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java =================================================================== --- tinytim/tags/release-1_5_0/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java (rev 0) +++ tinytim/tags/release-1_5_0/src/main/java/org/tinytim/mio/TinyTimMapInputHandler.java 2009-07-02 12:24:57 UTC (rev 304) @@ -0,0 +1,590 @@ +/* + * 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.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.logging.Logger; + +import org.tinytim.IReifiable; +import org.tinytim.ITyped; +import org.tinytim.TopicMapImpl; +import org.tinytim.TypeInstanceConverter; +import org.tinytim.voc.TMDM; +import org.tmapi.core.Association; +import org.tmapi.core.AssociationRole; +import org.tmapi.core.Locator; +import org.tmapi.core.Occurrence; +import org.tmapi.core.ScopedObject; +import org.tmapi.core.Topic; +import org.tmapi.core.TopicMap; +import org.tmapi.core.TopicMapObject; +import org.tmapi.core.TopicName; +import org.tmapi.core.Variant; + +import com.semagia.mio.IMapHandler; +import com.semagia.mio.IRef; +import com.semagia.mio.MIOException; +import com.semagia.mio.voc.XSD; + +/** + * {@link com.semagia.mio.IMapHandler} implementation. + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev: 223 $ - $Date: 2008-11-21 14:17:29 +0100 (Fr, 21 Nov 2008) $ + */ +public class TinyTimMapInputHandler implements IMapHandler { + + private enum State { + INITIAL, TOPIC, ASSOCIATION, ROLE, OCCURRENCE, NAME, VARIANT, + SCOPE, THEME, REIFIER, PLAYER, ISA, TYPE; + } + + private static final Logger LOG = Logger.getLogger(TinyTimMapInputHandler.class.getName()); + + private TopicMapImpl _tm; + private List<State> _stateStack; + private List<TopicMapObject> _constructStack; + + public TinyTimMapInputHandler() { + } + + public TinyTimMapInputHandler(TopicMap topicMap) { + this(); + setTopicMap(topicMap); + } + + /** + * Sets the topic map instance to operate on. + * + * @param topicMap The topic map. + */ + public void setTopicMap(TopicMap topicMap) { + if (topicMap == null) { + throw new IllegalArgumentException("The topic map must not be null"); + } + _tm = (TopicMapImpl) topicMap; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startTopicMap() + */ + public void startTopicMap() throws MIOException { + _constructStack = new ArrayList<TopicMapObject>(); + _stateStack = new ArrayList<State>(); + _enterState(State.INITIAL, _tm); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endTopicMap() + */ + public void endTopicMap() throws MIOException { + TypeInstanceConverter.convertAssociationsToTypes(_tm); + _constructStack = null; + _stateStack = null; + _tm = null; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startTopic(com.semagia.mio.IRef) + */ + public void startTopic(IRef identity) throws MIOException { + _enterState(State.TOPIC, _createTopic(identity)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endTopic() + */ + public void endTopic() throws MIOException { + Topic topic = (Topic) _leaveStatePopConstruct(State.TOPIC); + _handleTopic(topic); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startAssociation() + */ + public void startAssociation() throws MIOException { + _enterState(State.ASSOCIATION, _tm.createAssociation()); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endAssociation() + */ + public void endAssociation() throws MIOException { + _leaveStatePopConstruct(State.ASSOCIATION); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startRole() + */ + public void startRole() throws MIOException { + assert _state() == State.ASSOCIATION; + _enterState(State.ROLE, ((Association) _peekConstruct()).createAssociationRole(null, null)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endRole() + */ + public void endRole() throws MIOException { + _leaveStatePopConstruct(State.ROLE); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startPlayer() + */ + public void startPlayer() throws MIOException { + assert _state() == State.ROLE; + _enterState(State.PLAYER); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endPlayer() + */ + public void endPlayer() throws MIOException { + _leaveState(State.PLAYER); + assert _state() == State.ROLE; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startOccurrence() + */ + public void startOccurrence() throws MIOException { + _enterState(State.OCCURRENCE, _peekTopic().createOccurrence((Locator) null, null, null)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endOccurrence() + */ + public void endOccurrence() throws MIOException { + _leaveStatePopConstruct(State.OCCURRENCE); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startName() + */ + public void startName() throws MIOException { + _enterState(State.NAME, _peekTopic().createTopicName(null, null)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endName() + */ + public void endName() throws MIOException { + TopicName name = (TopicName) _leaveStatePopConstruct(State.NAME); + if (name.getType() == null) { + name.setType(_topicBySubjectIdentifier(TMDM.TOPIC_NAME)); + } + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startVariant() + */ + public void startVariant() throws MIOException { + assert _state() == State.NAME; + _enterState(State.VARIANT, ((TopicName) _peekConstruct()).createVariant((Locator) null, null)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endVariant() + */ + @SuppressWarnings("unchecked") + public void endVariant() throws MIOException { + Variant variant = (Variant) _leaveStatePopConstruct(State.VARIANT); + Collection<Topic> scope = variant.getScope(); + if (scope.isEmpty() || variant.getTopicName().getScope().equals(scope)) { + throw new MIOException("The variant has no scope"); + } + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startType() + */ + public void startType() throws MIOException { + assert _peekConstruct() instanceof ITyped; + _enterState(State.TYPE); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endType() + */ + public void endType() throws MIOException { + _leaveState(State.TYPE); + assert _peekConstruct() instanceof ITyped; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startScope() + */ + public void startScope() throws MIOException { + assert _peekConstruct() instanceof ScopedObject; + _enterState(State.SCOPE); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endScope() + */ + public void endScope() throws MIOException { + _leaveState(State.SCOPE); + assert _peekConstruct() instanceof ScopedObject; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startTheme() + */ + public void startTheme() throws MIOException { + assert _state() == State.SCOPE; + _enterState(State.THEME); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endTheme() + */ + public void endTheme() throws MIOException { + _leaveState(State.THEME); + assert _state() == State.SCOPE; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#subjectIdentifier(java.lang.String) + */ + public void subjectIdentifier(String subjectIdentifier) throws MIOException { + Locator sid = _tm.createLocator(subjectIdentifier); + Topic topic = _peekTopic(); + Topic existing = _tm.getTopicBySubjectIdentifier(sid); + if (existing != null && !existing.equals(topic)) { + _merge(existing, topic); + } + else { + TopicMapObject tmo = _tm.getObjectByItemIdentifier(sid); + if (tmo != null && tmo instanceof Topic && !tmo.equals(topic)) { + _merge((Topic) tmo, topic); + } + } + topic.addSubjectIdentifier(sid); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#subjectLocator(java.lang.String) + */ + public void subjectLocator(String subjectLocator) throws MIOException { + Locator slo = _tm.createLocator(subjectLocator); + Topic topic = _peekTopic(); + Topic existing = _tm.getTopicBySubjectLocator(slo); + if (existing != null && !existing.equals(topic)) { + _merge(existing, topic); + } + topic.addSubjectLocator(slo); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#itemIdentifier(java.lang.String) + */ + public void itemIdentifier(String itemIdentifier) throws MIOException { + Locator iid = _tm.createLocator(itemIdentifier); + TopicMapObject tmo = _peekConstruct(); + if (_state() == State.TOPIC) { + TopicMapObject existing = _tm.getObjectByItemIdentifier(iid); + if (existing != null && existing instanceof Topic && !existing.equals(tmo)) { + _merge((Topic) existing, (Topic) tmo); + } + else { + Topic topic = _tm.getTopicBySubjectIdentifier(iid); + if (topic != null && !topic.equals(tmo)) { + _merge(topic, (Topic) tmo); + } + } + } + tmo.addSourceLocator(iid); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startIsa() + */ + public void startIsa() throws MIOException { + assert _state() == State.TOPIC; + _enterState(State.ISA); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endIsa() + */ + public void endIsa() throws MIOException { + _leaveState(State.ISA); + assert _state() == State.TOPIC; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#startReifier() + */ + public void startReifier() throws MIOException { + assert _peekConstruct() instanceof IReifiable; + _enterState(State.REIFIER); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#endReifier() + */ + public void endReifier() throws MIOException { + _leaveState(State.REIFIER); + assert _peekConstruct() instanceof IReifiable; + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#topicRef(com.semagia.mio.IRef) + */ + public void topicRef(IRef identity) throws MIOException { + _handleTopic(_createTopic(identity)); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#value(java.lang.String) + */ + public void value(String value) throws MIOException { + assert _state() == State.NAME; + ((TopicName) _peekConstruct()).setValue(value); + } + + /* (non-Javadoc) + * @see com.semagia.mio.IMapHandler#value(java.lang.String, java.lang.String) + */ + public void value(String value, String datatype) throws MIOException { + boolean isLocator = XSD.ANY_URI.equals(datatype); + if (!isLocator && !XSD.STRING.equals(datatype)) { + LOG.warning("The datatype '" + datatype + "' was converted into xsd:string"); + } + if (_state() == State.OCCURRENCE) { + Occurrence occ = (Occurrence) _peekConstruct(); + if (isLocator) { + occ.setResource(_tm.createLocator(value)); + } + else { + occ.setValue(value); + } + } + else { + assert _state() == State.VARIANT; + Variant variant = (Variant) _peekConstruct(); + if (isLocator) { + variant.setResource(_tm.createLocator(value)); + } + else { + variant.setValue(value); + } + } + } + + /** + * Enters a state. + * + * @param state The state to push ontop of the state stack. + */ + private void _enterState(State state) { + _stateStack.add(state); + } + + /** + * Enters a state and pushes the Topic Maps construct ontop of the construct + * stack. + * + * @param state The state to enter. + * @param tmo The Topic Maps construct which should be pushed to the stack. + */ + private void _enterState(State state, TopicMapObject tmo) { + _enterState(state); + _constructStack.add(tmo); + } + + /** + * Leaves a state. + * + * @param state The state to leave. + * @throws MIOException If the state is not equals to the current state. + */ + private void _leaveState(State state) throws MIOException { + State current = _stateStack.remove(_stateStack.size()-1); + if (state != current) { + _reportError("Unexpected state: " + current + ", expected: " + state); + } + } + + /** + * Leaves a state and removed the Topic Maps construct from the top of the + * construct stack. + * + * @param state The state to leave. + * @return The removed construct. + * @throws MIOException If the state is not equals to the current state. + */ + private TopicMapObject _leaveStatePopConstruct(State state) throws MIOException { + _leaveState(state); + return _constructStack.remove(_constructStack.size()-1); + } + + /** + * Returns the Topic Maps construct on top of the stack. + * + * @return The Topic Maps construct. + */ + private TopicMapObject _peekConstruct() { + return _constructStack.get(_constructStack.size()-1); + } + + /** + * Returns the topic on top of the stack. + * + * @return The topic. + */ + private Topic _peekTopic() { + return (Topic) _peekConstruct(); + } + + /** + * Returns the current state. + * + * @return The current state. + */ + private State _state() { + return _stateStack.get(_stateStack.size()-1); + } + + /** + * Handles the topic dependent on the current state. + * + * @param topic The topic to handle. + */ + private void _handleTopic(Topic topic) { + switch (_state()) { + case ISA: _peekTopic().addType(topic); break; + case TYPE: ((ITyped) _peekConstruct()).setType(topic); break; + case PLAYER: ((AssociationRole) _peekConstruct()).setPlayer(topic); break; + case THEME: ((ScopedObject) _peekConstruct()).addScopingTopic(topic); break; + case REIFIER: ((IReifiable) _peekConstruct()).setReifier(topic); break; + } + } + + /** + * Merges the <tt>source</tt> topic with the <tt>target</tt>. + * + * Further, this method ensures that the construct stack stays valid: If + * the <tt>source</tt> is part of the stack, it is replaced with + * <tt>target</tt>. + * + * @param source The source topic (will be removed). + * @param target The target topic. + */ + private void _merge(Topic source, Topic target) { + int i = _constructStack.indexOf(source); + while (i > -1) { + _constructStack.set(i, target); + i = _constructStack.indexOf(source); + } + target.mergeIn(source); + } + + /** + * Returns either an existing topic with the specified identity or creates + * a topic with the given identity. + * + * @param ref The identity of the topic. + * @return A topic instance. + * @throws MIOException + */ + private Topic _createTopic(IRef ref) throws MIOException { + Locator loc = _tm.createLocator(ref.getIRI()); + switch (ref.getType()) { + case IRef.ITEM_IDENTIFIER: return _topicByItemIdentifier(loc); + case IRef.SUBJECT_IDENTIFIER: return _topicBySubjectIdentifier(loc); + case IRef.SUBJECT_LOCATOR: return _topicBySubjectLocator(loc); + default: _reportError("Unknown reference type " + ref.getType()); + } + // Never returned, an exception was thrown + return null; + } + + /** + * Returns either an existing topic with the specified item identfier, + * or creates a topic with the given item identifier. + * + * @param iid The item identifier of the topic. + * @return A topic instance. + */ + private Topic _topicByItemIdentifier(Locator iid) { + TopicMapObject tmo = _tm.getObjectByItemIdentifier(iid); + Topic topic = (tmo instanceof Topic) ? (Topic) tmo : null; + if (topic == null) { + topic = _tm.getTopicBySubjectIdentifier(iid); + if (topic != null) { + topic.addSourceLocator(iid); + } + } + if (topic == null) { + topic = _tm.createTopic(); + topic.addSourceLocator(iid); + } + return topic; + } + + /** + * Returns either an existing topic with the specified subject identfier, + * or creates a topic with the given subject identifier. + * + * @param sid The subject identifier of the topic. + * @return A topic instance. + */ + private Topic _topicBySubjectIdentifier(Locator sid) { + Topic topic = _tm.getTopicBySubjectIdentifier(sid); + if (topic == null) { + TopicMapObject tmo = _tm.getObjectByItemIdentifier(sid); + if (tmo instanceof Topic) { + topic = (Topic) tmo; + topic.addSubjectIdentifier(sid); + } + } + if (topic == null) { + topic = _tm.createTopic(); + topic.addSubjectIdentifier(sid); + } + return topic; + } + + /** + * Returns either an existing topic with the specified subject locator, + * or creates a topic with the given subject locator. + * + * @param slo The subject locator of the topic. + * @return A topic instance. + */ + private Topic _topicBySubjectLocator(Locator slo) { + Topic topic = _tm.getTopicBySubjectLocator(slo); + if (topic == null) { + topic = _tm.createTopic(); + topic.addSubjectLocator(slo); + } + return topic; + } + + /** + * Reports an error. + * + * @param msg The error message. + * @throws MIOException Thrown in any case. + */ + private static void _reportError(String msg) throws MIOException { + throw new MIOException(msg); + } + +} Added: tinytim/tags/release-1_5_0/src/test/java/org/tinytim/mio/TestTinyTimMapInputHandler.java =================================================================== --- tinytim/tags/release-1_5_0/src/test/java/org/tinytim/mio/TestTinyTimMapInputHandler.java (rev 0) +++ tinytim/tags/release-1_5_0/src/test/java/org/tinytim/mio/TestTinyTimMapInputHandler.java 2009-07-02 12:24:57 UTC (rev 304) @@ -0,0 +1,333 @@ +/* + * 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.util.Properties; + +import org.tinytim.TinyTimTestCase; +import org.tinytim.Property; +import org.tinytim.TopicMapImpl; +import org.tinytim.voc.TMDM; +import org.tmapi.core.Locator; +import org.tmapi.core.Occurrence; +import org.tmapi.core.Topic; +import org.tmapi.core.TopicMapSystem; +import org.tmapi.core.TopicMapSystemFactory; +import org.tmapi.core.TopicName; + +import com.semagia.mio.MIOException; +import com.semagia.mio.helpers.Ref; + +import junit.framework.TestCase; + +/** + * Tests against the {@link TinyTimMapInputHandler}. + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev: 226 $ - $Date: 2008-11-21 16:16:07 +0100 (Fr, 21 Nov 2008) $ + */ +public class TestTinyTimMapInputHandler extends TinyTimTestCase { + + private static final String _XSD_STRING = "http://www.w3.org/2001/XMLSchema#string"; + private static final String _XSD_ANY_URI = "http://www.w3.org/2001/XMLSchema#anyURI"; + + private TinyTimMapInputHandler _handler; + + @Override + protected Properties getAdditionalProperties() { + Properties props = new Properties(); + props.setProperty(Property.XTM10_REIFICATION, "false"); + return props; + } + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + _handler = new TinyTimMapInputHandler(); + _handler.setTopicMap(_tm); + } + + /** + * Simple startTopicMap, followed by an endTopicMap event. + */ + public void testEmpty() throws Exception { + assertEquals(0, _tm.getTopics().size()); + assertEquals(0, _tm.getAssociations().size()); + _handler.startTopicMap(); + _handler.endTopicMap(); + assertEquals(0, _tm.getTopics().size()); + assertEquals(0, _tm.getAssociations().size()); + } + + /** + * Tests reifying a topic map. + */ + public void testTMReifier() throws Exception { + String itemIdent = "http://sf.net/projects/tinytim/test#1"; + assertEquals(0, _tm.getTopics().size()); + assertEquals(0, _tm.getAssociations().size()); + _handler.startTopicMap(); + _handler.startReifier(); + _handler.startTopic(Ref.createItemIdentifier(itemIdent)); + _handler.endTopic(); + _handler.endReifier(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + assertEquals(0, _tm.getAssociations().size()); + Topic topic = (Topic) _tm.getObjectByItemIdentifier(_tm.createLocator(itemIdent)); + assertNotNull(topic); + assertNotNull(_tm.getReifier()); + assertEquals(topic, _tm.getReifier()); + } + + /** + * Tests topic creation with an item identifier. + */ + public void testTopicIdentityItemIdentifier() throws Exception { + String itemIdent = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createItemIdentifier(itemIdent)); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Topic topic = (Topic) _tm.getObjectByItemIdentifier(_tm.createLocator(itemIdent)); + assertNotNull(topic); + } + + /** + * Tests topic creation with a subject identifier. + */ + public void testTopicIdentitySubjectIdentifier() throws Exception { + String subjIdent = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(subjIdent)); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(subjIdent)); + assertNotNull(topic); + } + + /** + * Tests topic creation with a subject locator. + */ + public void testTopicIdentitySubjectLocator() throws Exception { + String subjLoc = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectLocator(subjLoc)); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Topic topic = _tm.getTopicBySubjectLocator(_tm.createLocator(subjLoc)); + assertNotNull(topic); + } + + /** + * Tests transparent merging. + */ + public void testTopicMerging() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String itemIdent = "http://example.org/1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + // Topic in topic event + _handler.startTopic(Ref.createItemIdentifier(itemIdent)); + _handler.itemIdentifier(ref); + _handler.endTopic(); + _handler.startOccurrence(); + _handler.value("tinyTiM", _XSD_STRING); + _handler.endOccurrence(); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(ref)); + assertNotNull(topic); + assertEquals(topic, _tm.getObjectByItemIdentifier(_tm.createLocator(ref))); + assertEquals(topic, _tm.getObjectByItemIdentifier(_tm.createLocator(itemIdent))); + assertEquals(1, topic.getOccurrences().size()); + Occurrence occ = (Occurrence) topic.getOccurrences().iterator().next(); + assertEquals("tinyTiM", occ.getValue()); + } + + /** + * Tests assigning identities to a topic. + */ + public void testTopicIdentities1() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.itemIdentifier(ref); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Locator loc = _tm.createLocator(ref); + Topic topic = _tm.getTopicBySubjectIdentifier(loc); + assertNotNull(topic); + assertEquals(topic, _tm.getObjectByItemIdentifier(loc)); + } + + /** + * Tests assigning identities to a topic. + */ + public void testTopicIdentities2() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createItemIdentifier(ref)); + _handler.subjectIdentifier(ref); + _handler.endTopic(); + _handler.endTopicMap(); + assertEquals(1, _tm.getTopics().size()); + Locator loc = _tm.createLocator(ref); + Topic topic = _tm.getTopicBySubjectIdentifier(loc); + assertNotNull(topic); + assertEquals(topic, _tm.getObjectByItemIdentifier(loc)); + } + + /** + * Tests reifying the topic map. + */ + public void testTopicMapReifier() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + _handler.startTopicMap(); + _handler.startReifier(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.endTopic(); + _handler.endReifier(); + _handler.endTopicMap(); + assertNotNull(_tm.getReifier()); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(ref)); + assertNotNull(topic); + assertEquals(topic, _tm.getReifier()); + } + + /** + * Tests occurrence creation with a value of datatype xsd:string. + */ + public void testOccurrenceValueString() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String val = "tinyTiM"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.startOccurrence(); + _handler.value(val, _XSD_STRING); + _handler.endOccurrence(); + _handler.endTopic(); + _handler.endTopicMap(); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(ref)); + assertNotNull(topic); + Occurrence occ = (Occurrence) topic.getOccurrences().iterator().next(); + assertEquals(val, occ.getValue()); + } + + /** + * Tests occurrence creation with a value of datatype xsd:anyURI. + */ + public void testOccurrenceValueURI() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String val = "http://sf.net/projects/tinytim"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.startOccurrence(); + _handler.value(val, _XSD_ANY_URI); + _handler.endOccurrence(); + _handler.endTopic(); + _handler.endTopicMap(); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(ref)); + assertNotNull(topic); + Occurrence occ = (Occurrence) topic.getOccurrences().iterator().next(); + assertNull(occ.getValue()); + assertEquals(val, occ.getResource().getReference()); + } + + /** + * Tests if the name type is automatically set. + */ + public void testDefaultNameType() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String val = "tinyTiM"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.startName(); + _handler.value(val); + _handler.endName(); + _handler.endTopic(); + _handler.endTopicMap(); + Topic topic = _tm.getTopicBySubjectIdentifier(_tm.createLocator(ref)); + assertNotNull(topic); + TopicName name = (TopicName) topic.getTopicNames().iterator().next(); + assertEquals(val, name.getValue()); + assertNotNull(name.getType()); + assertTrue(name.getType().getSubjectIdentifiers().contains(TMDM.TOPIC_NAME)); + } + + /** + * Tests if a variant with no scope is reported as error. + */ + public void testVariantNoScopeError() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String val = "tinyTiM"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.startName(); + _handler.value(val); + _handler.startVariant(); + _handler.value(val, _XSD_STRING); + try { + _handler.endVariant(); + fail("A variant with no scope shouldn't be allowed"); + } + catch (MIOException ex) { + // noop. + } + } + + /** + * Tests if a variant with a scope equals to the parent's scope is rejected. + */ + public void testVariantNoScopeError2() throws Exception { + String ref = "http://sf.net/projects/tinytim/test#1"; + String theme = "http://sf.net/projects/tinytim/test#theme"; + String val = "tinyTiM"; + _handler.startTopicMap(); + _handler.startTopic(Ref.createSubjectIdentifier(ref)); + _handler.startName(); + _handler.startScope(); + _handler.startTheme(); + _handler.topicRef(Ref.createItemIdentifier(theme)); + _handler.endTheme(); + _handler.endScope(); + _handler.value(val); + + _handler.startVariant(); + _handler.value(val, _XSD_STRING); + _handler.startScope(); + _handler.startTheme(); + _handler.topicRef(Ref.createItemIdentifier(theme)); + _handler.endTheme(); + _handler.endScope(); + try { + _handler.endVariant(); + fail("A variant with a scope equals to the parent's scope shouldn't be allowed"); + } + catch (MIOException ex) { + // noop. + } + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |