'--------------------------------------------------- ' xtm2mm Is free software; you can redistribute it And/Or ' modify it under the terms of the GNU General Public License ' As published by the Free Software Foundation. ' ' This software 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. ' ' You should have received a copy of the GNU General Public License ' along With this software. If Not, Write To the Free Software ' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111- ' 1307, USA. ' ' See the GPL In the COPYING.GPL file For more details. ' ' Author: Michel Kern (mkern@club-internet.fr) ' Description: Import Xml Topic Map to MindManager ' Last modification: 1 july 2003 '--------------------------------------------------- Const LEXRULE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzéèëêàâäïîöôùüû" Const LEXRULE_DIGIT = "0123456789" Const LEXRULE_ALPHANUMERIC = LEXRULE_ALPHABET + LEXRULE_DIGIT Const LEXRULE_NON_ALPHA_CHARS = "_?!-+*/&'$;:,.()[]|{}=@~\^%" Const LEXRULE_TAG_NAME = LEXRULE_ALPHANUMERIC + "_" + ":" Const LEXRULE_ATTRIBUTE_NAME = LEXRULE_TAG_NAME Const LEXRULE_ATTRIBUTE_VALUE = LEXRULE_ATTRIBUTE_NAME + LEXRULE_NON_ALPHA_CHARS Const LEXRULE_TAG_CDATA = LEXRULE_ATTRIBUTE_VALUE + " " + "{}\-" Const LEXRULE_SPACES = " " + Chr$(10) + Chr$(13) Type Topic id As String baseName As String End Type Type Tag Name As String AttributeValue As String IsBeginTag As Boolean End Type Const XTM_TOPIC_ID_PROPERTY = 1 Dim aDocument As Document '------------------------------------------------------------------------ ' Main '------------------------------------------------------------------------ Sub Main Dim fileName As Variant Dim fileId As Integer Dim IsEOF As Boolean Dim aCharacter As String Dim tagCount As Long Dim aTag As Tag Dim currentBranch As Branch Dim state As String Dim topicCount As Long Dim topicID As String Dim resourceData As String Dim cdata As String Dim hyperlink As String Dim topicRef As String Dim parentTagClass As String Dim bookmark As String Dim inTopics As Boolean Dim inAssociation As Boolean Dim fromRef As String Dim toRef As String Dim topics() As Topic Dim parentTag As Tag Dim parentTagId As String Dim InParentMember As Boolean 'Determine if there is a document open in MindManager If Documents.Count > 0 Then Set aDocument = ActiveDocument Else Set aDocument = Documents.AddNew("") End If fileName = aDocument.FullName fileName = GetFilePath$(fileName,"xml",,"Merge File",0) fileId = FreeFile ' Create a Branch from the input file, then test to make sure that it's the ' Same in the document. If it's not, then Open fileName For Binary Access Read As #fileId IsEOF = False nbBranch = 1 state = "READ" topicCount = 0 topicID = "" resourceData = "" cdata = "" topicRef = "" parentTagClass = "" bookmark = "" inTopics = False inAssociation = False fromRef = "" toRef = "" InParentMember = False Debug.Clear While Not IsEOF IsEOF = GetNextCharacter(fileId, aCharacter) '---------start of Tag If (InStr(aCharacter, "<") <> 0) Then aTag = ParseTag(fileId) '--- Tag '--------------------------------------------------------- If (aTag.IsBeginTag = True And aTag.Name = "topic") Then topicID = aTag.AttributeValue Debug.Print "topicID=""" & topicID & """" inTopics = True ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "instanceOf") Then parentTagClass = "instanceOf" '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "instanceOf") Then parentTagClass = "" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "topicRef") Then Debug.Print "topicRef=""" & topicRef & """" Debug.Print "cData=""" & cdata & """" Debug.Print "parentTagClass=""" & parentTagClass & """" If (inTopics) Then If (parentTagClass = "instanceOf") Then topicRef = aTag.AttributeValue End If ElseIf (inAssociation) Then If (topicRef = "#parent") Then InParentMember = True Debug.Print aTag.AttributeValue parentTagId = Mid(aTag.AttributeValue,2) End If topicRef = aTag.AttributeValue End If ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "resourceData") Then If (parentTagClass = "variantName") Then state = "READ BOOKMARK" Else state = "READ RESOURCE DATA" End If ElseIf (aTag.IsBeginTag = False And aTag.Name = "resourceData") Then state = "READ" parentTagClass = "" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "resourceRef") Then hyperlink = aTag.AttributeValue ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "variantName") Then parentTagClass = "variantName" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "baseNameString") Then state = "READ BASENAME STRING" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "baseNameString") Then state = "READ" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "member") Then parentTagClass = "member" ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = True And aTag.Name = "roleSpec") Then If (parentTagClass = "member") Then End If ' '--- Tag '--------------------------------------------------------- ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ElseIf (aTag.IsBeginTag = True And aTag.Name = "association") Then inAssociation = True ' ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "association") Then inAssociation = True ' '--- Tag '--------------------------------------------------------- ElseIf (aTag.IsBeginTag = False And aTag.Name = "topic") Then Dim aBranch As Branch Set aBranch = aDocument.MapTitle() 'Debug.Print "-----------------------------" 'Debug.Print "New branch id=""" & topicID & """ cdata=""" & cdata & """" If (Left(topicID,3) <> "tga" _ And topicID <> "parent" And topicID <> "child" And topicID <> "is-child-of") Then topicCount = topicCount + 1 ReDim Preserve topics(topicCount +1) topics(topicCount + 1) = CreateTopic (topicID, cdata) If (topicCount >= 1) Then Dim parentBranch As Branch Dim s As String If (parentTagId <>"") Then Set parentBranch = FindBranch(aDocument.MapTitle(), parentTagId) Set aBranch = aDocument.AddBranch(aTag.AttributeValue, parentBranch) End If aBranch.SetUserProperty(XTM_TOPIC_ID_PROPERTY, topicID) If (hyperlink <> "") Then aBranch.Hyperlink = hyperlink End If 'Debug.Print aTag.Name;" ";cdata 'Debug.Print aTag.AttributeValue 'Debug.Print resourceData 'Debug.Print "parentTagId=""" & parentTagId & """" If (resourceData <> "") Then aBranch.TextNotes.TextRTF = resourceData If (bookmark <> "") Then aBranch.Bookmark = bookmark aBranch.Text = cdata inTopics = False End If resourceData = "" cdata = "" topicRef = "" parentTagClass = "" bookmark = "" hyperlink = "" topicID = "" InParentMember = False parentTagId = "" End If '---------Tag CDATA '--------------------------------------------------------- Else If (MatchLexicalRule(LEXRULE_TAG_CDATA, aCharacter)) Then If state = "READ BASENAME STRING" Then cdata = cdata + aCharacter 'Debug.Print "cdata=";cdata ElseIf state = "READ RESOURCE DATA" Then resourceData = resourceData + aCharacter ElseIf state = "READ BOOKMARK" Then bookmark = bookmark + aCharacter End If End If End If Wend Close #fileId End Sub '------------------------------------------------------------------------ ' GetNextCharacter '------------------------------------------------------------------------ Function GetNextCharacter(fileId As Integer, aCharacter As String) As Boolean Dim aByte As Byte On Error GoTo endOfFile Get #fileId, , aByte aCharacter = Chr$(aByte) GetNextCharacter = False Exit Function endOfFile: GetNextCharacter = True End Function '------------------------------------------------------------------------ ' ParseTag '------------------------------------------------------------------------ Function ParseTag(fileId As Integer) As Tag Dim aCharacter As String Dim tagName As String Dim state As String Dim IsEOF As Boolean state = "START" ParseTag.Name = "" ParseTag.AttributeValue = "" ParseTag.IsBeginTag = True While state <> "END" IsEOF = GetNextCharacter(fileId, aCharacter) 'Debug.Print "ParseTag: c=";aCharacter;" ";Asc(aCharacter) If aCharacter = "/" And state <> "READ ATTRIBUTE VALUE" Then ParseTag.IsBeginTag = False End If ' tag If (aCharacter = "?") Then While aCharacter <> ">" IsEOF = GetNextCharacter(fileId, aCharacter) Wend ParseTag.Name = "" Exit Function End If ' If (aCharacter = "!") Then While aCharacter <> "-" IsEOF = GetNextCharacter(fileId, aCharacter) Wend While aCharacter <> ">" IsEOF = GetNextCharacter(fileId, aCharacter) Wend ParseTag.Name = "" Exit Function End If If aCharacter = ">" And state <> "READ ATTRIBUTE VALUE" Then state = "END" ElseIf aCharacter = "/" And state <> "READ ATTRIBUTE VALUE" Then If ParseTag.Name = "topic>Ref" Then state = "END" IsEOF = GetNextCharacter(fileId, aCharacter) End If Else If state = "START" And MatchLexicalRule(LEXRULE_TAG_NAME,aCharacter) Then state = "READ TAG NAME" ElseIf state = "READ TAG NAME" And MatchLexicalRule(LEXRULE_SPACES,aCharacter) Then state = "TAG SPACE" ElseIf state = "TAG SPACE" And MatchLexicalRule(LEXRULE_ATTRIBUTE_NAME,aCharacter) Then state = "READ ATTRIBUTE NAME" ElseIf state = "READ ATTRIBUTE NAME" And MatchLexicalRule(LEXRULE_SPACES,aCharacter) Then state = "ATTRIBUTE SPACE" ElseIf state = "READ ATTRIBUTE NAME" And MatchLexicalRule("=",aCharacter) Then state = "ATTRIBUTE SPACE" ElseIf state = "ATTRIBUTE SPACE" And MatchLexicalRule("""",aCharacter) Then state = "READ ATTRIBUTE VALUE" IsEOF = GetNextCharacter(fileId, aCharacter) ElseIf state = "READ ATTRIBUTE VALUE" And MatchLexicalRule("""",aCharacter) Then state = "TAG SPACE" Debug.Print "tagID:";ParseTag.AttributeValue End If 'Debug.Print "state:" + state If state = "READ TAG NAME" Then ParseTag.Name = ParseTag.Name + aCharacter 'Debug.Print "tagName:";ParseTag.Name ElseIf state = "READ ATTRIBUTE VALUE" Then ParseTag.AttributeValue = ParseTag.AttributeValue + aCharacter 'Debug.Print "tagID:";ParseTag.ID End If End If Wend End Function '------------------------------------------------------------------------ ' MatchLexicalRule '------------------------------------------------------------------------ Function MatchLexicalRule(lexicalRule As String, aCharacter As String) As Boolean If InStr(lexicalRule, aCharacter) <> 0 Then MatchLexicalRule = True Else MatchLexicalRule = False End If End Function '------------------------------------------------------------------------ ' FindBranch '------------------------------------------------------------------------ Function FindBranch(aBranch As Branch, referenceTopicID As String) As Branch Dim nextBranch As Branch Dim branchCount As Integer Dim topicID As String Dim parent As Branch Set FindBranch = aDocument.MapTitle() topicID = aBranch.GetUserProperty(XTM_TOPIC_ID_PROPERTY, "") branchCount = aBranch.Count 'Debug.Print "Look subbranch of:";topicID;" sub branch=";aBranch.Count For i = 1 To branchCount Set nextBranch = aBranch.Item( i ) topicID = nextBranch.GetUserProperty(XTM_TOPIC_ID_PROPERTY, "") 'Debug.Print "i=";i;" next=";topicID If (topicID = referenceTopicID) Then Set parent = nextBranch Else Set parent = FindBranch( nextBranch, referenceTopicID ) End If topicID = parent.GetUserProperty(XTM_TOPIC_ID_PROPERTY, "") If (topicID = referenceTopicID) Then Set FindBranch = parent Exit Function End If Next i End Function '------------------------------------------------------------------------ ' CreateTopic '------------------------------------------------------------------------ Function CreateTopic(id As String, baseName As String) As Topic CreateTopic.ID = id CreateTopic.baseName = baseName End Function