From: <lh...@us...> - 2008-11-19 15:43:02
|
Revision: 211 http://tinytim.svn.sourceforge.net/tinytim/?rev=211&view=rev Author: lheuer Date: 2008-11-19 15:42:50 +0000 (Wed, 19 Nov 2008) Log Message: ----------- Added *experimental* support for tinyTiM 1.5 (TMAPI 1.0) Added Paths: ----------- tinytim-mio/trunk/build-15.properties tinytim-mio/trunk/build-15.xml tinytim-mio/trunk/lib/tinytim-1.5.0beta.jar tinytim-mio/trunk/lib/tmapi-1_0SP1.jar tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler-15.txt Added: tinytim-mio/trunk/build-15.properties =================================================================== --- tinytim-mio/trunk/build-15.properties (rev 0) +++ tinytim-mio/trunk/build-15.properties 2008-11-19 15:42:50 UTC (rev 211) @@ -0,0 +1,5 @@ +version=1.5.0 +version_suffix=b +#release_type= +debug=off +optimize=on Added: tinytim-mio/trunk/build-15.xml =================================================================== --- tinytim-mio/trunk/build-15.xml (rev 0) +++ tinytim-mio/trunk/build-15.xml 2008-11-19 15:42:50 UTC (rev 211) @@ -0,0 +1,197 @@ +<?xml version="1.0" encoding="utf-8"?> +<project name="tinyTiM" default="jar" basedir="."> + <property file="build-15.properties"/> + <tstamp/> + <property name="release_type" value="-snapshot-${DSTAMP}${TSTAMP}"/> + + <property name="dir.src" value="${basedir}/src/main/java"/> + <property name="dir.test" value="${basedir}/src/test/java"/> + <property name="dir.lib" value="${basedir}/lib"/> + + <property name="lib.junit" value="${dir.lib}/junit-4.5.jar"/> + <property name="lib.tmapi" value="${dir.lib}/tmapi-1_0SP1.jar"/> + <property name="lib.tinytim" value="${dir.lib}/tinytim-1.5.0beta.jar"/> + <property name="lib.logging" value="${dir.lib}/slf4j-api-1.5.2.jar"/> + <property name="lib.mio" value="${dir.lib}/semagia-mio-0.9.3.jar"/> + + <target name="help"> + <echo message="------------------------"/> + <echo message="tinyTiM MIO - Build file"/> + <echo message="------------------------"/> + <echo message=""/> + <echo message="Available targets:"/> + <echo message=""/> + <echo message=" jar Creates the jar"/> + <echo message=" doc Generates the Java Docs"/> + <echo message=" release Creates the jar and a distributable file"/> + </target> + + <target name="init"> + <property name="dist.version" value="${version}${version_suffix}${release_type}"/> + <property name="dist.name" value="tinytim-mio-${dist.version}"/> + + <property name="tinytim-mio.jar" value="${dist.name}.jar"/> + <property name="tinytim-mio.tar" value="${dist.name}.tar"/> + <property name="tinytim-mio.tar.gz" value="${tinytim-mio.tar}.gz"/> + <property name="tinytim-mio.zip" value="${dist.name}.zip"/> + + <property name="dir.build" value="${basedir}/build"/> + <property name="dir.dist.root" value="${dir.build}/dist"/> + <property name="dir.dist" value="${dir.dist.root}/${dist.name}"/> + <property name="dir.build.classes" value="${dir.build}/classes"/> + <property name="dir.build.tests" value="${dir.build}/tests"/> + <property name="dir.javadocs" value="${dir.dist}/docs/api"/> + </target> + + <!-- =================================================================== --> + <!-- Clean targets --> + <!-- =================================================================== --> + <target name="clean" depends="init"> + <delete dir="${dir.build}"/> + <delete dir="${dir.javadocs}"/> + </target> + + <!-- =================================================================== --> + <!-- Prepares the build directory --> + <!-- =================================================================== --> + <target name="prepare" depends="init"> + <mkdir dir="${dir.build}"/> + <mkdir dir="${dir.build.classes}"/> + </target> + + <!-- =================================================================== --> + <!-- Creates the Java Docs --> + <!-- =================================================================== --> + <target name="doc" depends="init"> + <mkdir dir="${dir.javadocs}"/> + <javadoc destdir="${dir.javadocs}" + packagenames="org.tinytim.*" + author="true" + version="false" + use="true" + splitindex="true" + noindex="false" + windowtitle="tinyTiM MIO API v${dist.version}" + doctitle="tinyTiM API MIO v${dist.version}"> + <fileset dir="${dir.src}"> + </fileset> + <link href="http://www.tmapi.org/apiDocs/"/> + </javadoc> + </target> + + <!-- =================================================================== --> + <!-- Tests --> + <!-- =================================================================== --> + <target name="test" depends="compile"> + <mkdir dir="${dir.build.tests}"/> + <javac destdir="${dir.build.tests}" + debug="${debug}" + optimize="${optimize}" + target="1.5"> + <classpath> + <pathelement location="${dir.build.classes}"/> + <pathelement location="${lib.junit}"/> + <pathelement location="${lib.tmapi}"/> + <pathelement location="${lib.tinytim}"/> + <pathelement location="${lib.mio}"/> + </classpath> + <src path="${dir.test}"/> + </javac> + <junit printsummary="true" showoutput="false" + errorProperty="test.failed" failureProperty="test.failed"> + <classpath> + <pathelement location="${dir.build.classes}"/> + <pathelement location="${dir.build.tests}"/> + <pathelement location="${lib.junit}"/> + <pathelement location="${lib.tmapi}"/> + <pathelement location="${lib.tinytim}"/> + <pathelement location="${lib.mio}"/> + </classpath> + <formatter type="brief" usefile="false"/> + <batchtest fork="no" todir="${dir.build}"> + <fileset dir="${dir.build.tests}/"> + <include name="**/Test**.class"/> + </fileset> + </batchtest> + </junit> + <fail message="Tests failed. Check test output." if="test.failed"/> + </target> + + <!-- =================================================================== --> + <!-- Compile source files --> + <!-- =================================================================== --> + <target name="compile" depends="clean, prepare"> + <copy file="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler.java" tofile="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler-20.txt"/> + <copy file="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler-15.txt" tofile="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler.java" overwrite="true"/> + <javac destdir="${dir.build.classes}" + debug="${debug}" + excludes="org/tinytim/mio/**Writer.*" + target="1.5"> + <classpath> + <pathelement location="${lib.tmapi}"/> + <pathelement location="${lib.tinytim}"/> + <pathelement location="${lib.mio}"/> + <pathelement location="${lib.logging}"/> + </classpath> + <src path="${dir.src}"/> + </javac> + <copy file="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler-20.txt" tofile="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler.java" overwrite="true"/> + <delete file="${dir.src}/org/tinytim/mio/TinyTimMapInputHandler-20.txt"/> + </target> + + <!-- =================================================================== --> + <!-- Creates the jar --> + <!-- =================================================================== --> + <target name="jar" depends="compile"> + <jar destfile="${dir.build}/${tinytim-mio.jar}"> + <fileset dir="${dir.build.classes}"> + <include name="**/*.*"/> + </fileset> + <manifest> + <attribute name="Implementation-Title" value="tinyTiM MIO"/> + <attribute name="Implementation-Version" value="${dist.version}"/> + <attribute name="Implementation-URL" value="http://sourceforge.net/projects/tinytim"/> + </manifest> + </jar> + </target> + + <!-- =================================================================== --> + <!-- Prepares a distribution --> + <!-- =================================================================== --> + <target name="dist" depends="jar"> + <mkdir dir="${dir.dist}"/> + + <copy todir="${dir.dist}" file="${dir.build}/${tinytim-mio.jar}"/> + + <copy todir="${dir.dist}/src"> + <fileset dir="${dir.src}"/> + </copy> + <copy todir="${dir.dist}/test"> + <fileset dir="${dir.test}"/> + </copy> + <copy todir="${dir.dist}/lib"> + <fileset dir="${dir.lib}"> + <exclude name="**/tmapi*.jar"/> + <exclude name="**/tinytim*.jar"/> + </fileset> + </copy> + + <copy todir="${dir.dist}" file="CHANGES.txt"/> + <copy todir="${dir.dist}" file="LICENSE.txt"/> + <copy todir="${dir.dist}" file="README.txt"/> + </target> + + <!-- =================================================================== --> + <!-- Creates the distribution files (.zip and .tar.gz) --> + <!-- --> + <!-- Won't create the distribution files if a test fails --> + <!-- =================================================================== --> + <target name="release" depends="jar, test, doc, dist"> + <tar destfile="${dir.dist.root}/${tinytim-mio.tar}" + basedir="${dir.dist.root}"/> + <gzip src="${dir.dist.root}/${tinytim-mio.tar}" + destfile="${dir.build}/${tinytim-mio.tar.gz}" /> + <delete file="${dir.dist.root}/${tinytim-mio.tar}" /> + <zip destfile="${dir.build}/${tinytim-mio.zip}" basedir="${dir.dist.root}"/> + </target> +</project> Added: tinytim-mio/trunk/lib/tinytim-1.5.0beta.jar =================================================================== (Binary files differ) Property changes on: tinytim-mio/trunk/lib/tinytim-1.5.0beta.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: tinytim-mio/trunk/lib/tmapi-1_0SP1.jar =================================================================== (Binary files differ) Property changes on: tinytim-mio/trunk/lib/tmapi-1_0SP1.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler-15.txt =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler-15.txt (rev 0) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/TinyTimMapInputHandler-15.txt 2008-11-19 15:42:50 UTC (rev 211) @@ -0,0 +1,595 @@ +/* + * This is tinyTiM, a tiny Topic Maps engine. + * + * Copyright (C) 2008 Lars Heuer (heuer[at]semagia.com) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ +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: 123 $ - $Date: 2008-08-14 14:05:34 +0200 (Do, 14 Aug 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); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |