home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #3 / K-CD_2002-03.iso / OpenOffice / f_0180 / UCB.xba < prev   
Extensible Markup Language  |  2001-10-12  |  9KB  |  255 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Recursive" script:language="StarBasic">'Option explicit
  4. Public oDocument
  5. Dim oDocInfo as object
  6. Const SBMAXDIRCOUNT = 10
  7. Dim CurDirMaxCount as Integer
  8. Dim sDirArray(SBMAXDIRCOUNT-1) as String
  9. Dim DirIndex As Integer
  10. Dim iDirCount as Integer
  11.  
  12. Sub Main()
  13. Dim LocsfileContent(0) as String
  14.     LocsfileContent(0) = "*"
  15.     ReadDirectories("file:///space", LocsfileContent(), True, False, false)
  16. End Sub
  17.  
  18.  
  19. ' Prozedur, die die rekursive Auslesefunktion anwirft
  20. Function ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean,  bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String)
  21.  
  22. Dim i as integer
  23. Dim Status as Object
  24. Dim FileCountinDir as Integer
  25. Dim RealFileContent as String
  26. Dim FileName as string
  27. Dim oUcbObject as Object
  28. Dim DirContent()
  29. Dim CurIndex as Integer
  30. Dim MaxIndex as Integer
  31. Dim StartUbound as Integer
  32. Dim FileExtension as String
  33.     StartUbound = 5
  34.     MaxIndex = StartUBound
  35.     CurDirMaxCount = SBMAXDIRCOUNT
  36. Dim sFileArray(StartUbound,1) as String
  37.     CurIndex = -1
  38.     ' Todo: Is the last separator valid?
  39.     DirIndex = 0
  40.     sDirArray(iDirIndex) = AnchorDir
  41.     iDirCount = 1
  42.     oDocInfo = CreateUnoService("com.sun.star.document.DocumentProperties")
  43.     oUcbObject = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  44.     Do
  45.         AnchorDir = sDirArray(DirIndex)
  46.         DirContent() = oUcbObject.GetFolderContents(AnchorDir,True)
  47.         DirIndex = DirIndex + 1
  48.         If Ubound(DirContent()) <> -1 Then
  49.             FileCountinDir = Ubound(DirContent())+ 1
  50.             For i = 0 to FilecountinDir -1
  51.                 Filename = DirContent(i)
  52.                 If oUcbObject.IsFolder(FileName) Then
  53.                     If brecursive Then
  54.                         AddFoldertoList(FileName, DirIndex)
  55.                     End If
  56.                 Else
  57.                     If bcheckFileType Then
  58.                         RealFileContent  = GetRealFileContent(oDocInfo, FileName)
  59.                     Else
  60.                         RealFileContent = GetFileNameExtension(FileName)
  61.                     End If
  62.                     If RealFileContent <> "" Then
  63.                         ' Retrieve the Index in the Array, where a Filename is positioned
  64.                         If Not IsMissing(sFileContent()) Then
  65.                             If (FieldinArray(sFileContent(), Ubound(sFileContent), RealFileContent)) Then
  66.                                 ' The extension of the current file passes the filter and is therefor admitted to the
  67.                                 ' fileList
  68.                                 If Not IsMissing(sExtension) Then
  69.                                     If sExtension <> "" Then
  70.                                         ' Consider that some Formats like old StarOffice Templates with the extension ".vor" can only be
  71.                                         ' precisely identified by their mimetype and their extension
  72.                                         FileExtension = GetFileNameExtension(FileName)
  73.                                         If FileExtension = sExtension Then
  74.                                             AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)    
  75.                                         End If
  76.                                     Else
  77.                                         AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)                                    
  78.                                     End If
  79.                                 Else
  80.                                     AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  81.                                 End If
  82.                             End If
  83.                         Else
  84.                             AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  85.                         End If
  86.                         If CurIndex = MaxIndex Then
  87.                             MaxIndex = MaxIndex + StartUbound
  88.                             ReDim Preserve sFileArray(MaxIndex,1) as String
  89.                         End If
  90.                     End If
  91.                 End If
  92.             Next i
  93.         End If
  94.     Loop Until DirIndex >= iDirCount
  95.     If CurIndex > -1 Then
  96.         ReDim Preserve sFileArray(CurIndex,1) as String
  97.     Else 
  98.         ReDim sFileArray() as String
  99.     End If
  100.     ReadDirectories = sFileArray()
  101. End Function
  102.  
  103.  
  104. Sub AddFoldertoList(sDirURL as String, iDirIndex)
  105.     iDirCount = iDirCount + 1
  106.     If iDirCount = CurDirMaxCount Then
  107.         CurDirMaxCount = CurDirMaxCount + SBMAXDIRCOUNT
  108.         ReDim Preserve sDirArray(CurDirMaxCount) as String
  109.     End If
  110.     sDirArray(iDirCount-1) = sDirURL
  111. End Sub
  112.  
  113.  
  114. Sub AddFileNameToList(sFileArray(), FileName as String, FileContent as String, bGetByTitle as Boolean, CurIndex)
  115. Dim FileCount As Integer
  116.     CurIndex = CurIndex + 1
  117.     sFileArray(CurIndex,0) = FileName
  118.     If bGetByTitle Then
  119.         sFileArray(CurIndex,1) = RetrieveDocTitle(oDocInfo, FileName)
  120.         ' Add the documenttitles to the Filearray
  121.     Else
  122.         sFileArray(CurIndex,1) = FileContent
  123.     End If    
  124. End Sub
  125.  
  126.  
  127. Function RetrieveDocTitle(oDocInfo as Object, sFileName as String) As String
  128. Dim sDocTitle as String
  129.     On Local Error Goto NOFILE
  130.     oDocInfo.Read(sFileName)
  131.     sDocTitle = oDocInfo.Title
  132.     NOFILE:
  133.     If Err <> 0 Then
  134.         GetRealFileContent = ""
  135.         RESUME CLR_ERROR
  136.     End If
  137.     CLR_ERROR:
  138.     If sDocTitle = "" Then
  139.         sDocTitle = GetFileNameWithoutExtension(sFilename, "/")
  140.     End If
  141.     RetrieveDocTitle = sDocTitle
  142. End Function
  143.  
  144.  
  145. ' Retrieves The Filecontent of a Document by extracting the content
  146. ' from the Header of the document
  147. Function GetRealFileContent(oDocInfo as Object, FileName as String) As String
  148.     On Local Error Goto NOFILE
  149.     oDocInfo.Read(FileName)
  150.     GetRealFileContent = oDocInfo.MIMEType
  151.     NOFILE:
  152.     If Err <> 0 Then
  153.         GetRealFileContent = ""
  154.         resume CLR_ERROR
  155.     End If
  156.     CLR_ERROR:
  157. End Function
  158.  
  159.  
  160. Function CopyRecursively(SourceFilePath as String, SourceStemDir as String, TargetStemDir as String)
  161. Dim oUcb as Object
  162. Dim TargetDir as String
  163. Dim TargetFile as String
  164.  
  165.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  166.     TargetFile= ReplaceString(SourceFilePath, TargetStemDir, SourceStemDir)
  167.     TargetFileName = FileNameoutofPath(TargetFile,"/")
  168.     TargetDir = DeleteStr(TargetFile, TargetFileName)
  169.     If Not oUcb.Exists(TargetDir) Then
  170.         oUcb.CreateFolder(TargetDir)
  171.     End If
  172.     CopyRecursively() = TargetFile
  173. End Function
  174.  
  175.  
  176. ' Opens a help url referenced by a Help ID that is retrieved from the calling button tag
  177. Sub ShowHelperDialog(aEvent)
  178. Dim oSystemNode as Object
  179. Dim sSystem as String
  180. Dim oLanguageNode as Object
  181. Dim sLocale as String
  182. Dim sLocaleList() as String
  183. Dim sLanguage as String
  184. Dim sHelpUrl as String
  185. Dim sDocType as String
  186.     HelpID = aEvent.Source.Model.Tag
  187.     oLocDocument = StarDesktop.ActiveFrame.Controller.Model
  188.     sDocType = GetDocumentType(oLocDocument)
  189.     oSystemNode = GetRegistryKeyContent("org.openoffice.Office.Common/Help")
  190.     sSystem = oSystemNode.GetByName("System")
  191.     oLanguageNode = GetRegistryKeyContent("org.openoffice.Setup/L10N/")
  192.     sLocale = oLanguageNode.getByName("ooLocale")
  193.     sLocaleList() = ArrayoutofString(sLocale, "-")
  194.     sLanguage = sLocaleList(0)
  195.     sHelpUrl = "vnd.sun.star.help://" & sDocType & "/" & HelpID & "?Language=" & sLanguage & "&System=" & sSystem
  196.     StarDesktop.LoadComponentfromUrl(sHelpUrl, "OFFICE_HELP", 63, NoArgs())
  197. End Sub
  198.  
  199.  
  200. Sub SaveDataToFile(FilePath as String, DataList())
  201. Dim FileChannel as Integer
  202. Dim i as Integer
  203. Dim oFile as Object
  204. Dim oOutputStream as Object
  205. Dim oStreamString as Object
  206. Dim oUcb as Object
  207. Dim sCRLF as String
  208.  
  209.     sCRLF = CHR(10) & CHR(13)
  210.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  211.     oOutputStream = createUnoService("com.sun.star.io.TextOutputStream")
  212.     If oUcb.Exists(FilePath) Then
  213.         oUcb.Kill(FilePath)
  214.     End If
  215.     oFile = oUcb.OpenFileReadWrite(FilePath)
  216.     oOutputStream.SetOutputStream(oFile.GetOutputStream)
  217.     For i = 0 To Ubound(DataList())
  218.         oOutputStream.WriteString(DataList(i) & sCRLF)
  219.     Next i
  220.     oOutputStream.CloseOutput()
  221. End Sub
  222.  
  223.  
  224. Function LoadDataFromFile(FilePath as String, DataList()) as Boolean
  225. Dim oInputStream as Object
  226. Dim i as Integer
  227. Dim oUcb as Object
  228. Dim oFile as Object
  229. Dim MaxIndex as Integer
  230.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  231.     If oUcb.Exists(FilePath) Then
  232.         MaxIndex = 10
  233.         oInputStream = createUnoService("com.sun.star.io.TextInputStream")
  234.         oFile = oUcb.OpenFileReadWrite(FilePath)
  235.         oInputStream.SetInputStream(oFile.GetInputStream)
  236.         i = -1
  237.         Redim Preserve DataList(MaxIndex)
  238.         While Not oInputStream.IsEOF
  239.             i = i + 1
  240.             If i > MaxIndex Then
  241.                 MaxIndex = MaxIndex + 10
  242.                 Redim Preserve DataList(MaxIndex)
  243.             End If
  244.             DataList(i) = oInputStream.ReadLine
  245.         Wend
  246.         If i > -1 And i <> MaxIndex Then
  247.             Redim Preserve DataList(i)
  248.         End If
  249.         LoadDataFromFile() = True
  250.         oOutputStream.CloseInput()
  251.     Else
  252.         LoadDataFromFile() = False
  253.     End If
  254. End Function
  255. </script:module>