source: trunk/zoo-project/zoo-services/openoffice/cgi-env/oo_service.py @ 777

Last change on this file since 777 was 420, checked in by djay, 11 years ago

Update header of some Python files.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-python
File size: 5.8 KB
Line 
1#
2# Author : Gérald FENOY
3#
4# Copyright 2008-2013 GeoLabs SARL. All rights reserved.
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23#
24
25import uno
26import getopt, sys, os
27
28from unohelper import Base, systemPathToFileUrl, absolutize
29
30from com.sun.star.beans import PropertyValue
31from com.sun.star.script import CannotConvertException
32from com.sun.star.lang import IllegalArgumentException
33from com.sun.star.task import ErrorCodeIOException
34from com.sun.star.io import IOException, XOutputStream
35from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER
36from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
37
38from xml.dom import minidom
39import sys 
40
41keep_trace=''
42
43def addToText(cursor,text,level,value):
44    if level==1:
45        cursor.NumberingStyleName = "NONE"
46        cursor.ParaStyleName="Heading 1"
47        text.insertString( cursor, value , 0 )
48        text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 )
49        #print  >> sys.stderr,' * Main Title : ' + value
50    else:
51        i=0
52        prefix=''
53        while i < level-1:
54            prefix+=' '
55            i+=1
56        cursor.NumberingStyleName="List "+str(level-1)
57        text.insertString( cursor, prefix+value , 0 )
58        text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 )
59        cursor.NumberingStyleName = "NONE"
60        #print  >> sys.stderr,dir(sys.stderr)
61        #print >> sys.stderr,prefix+' * NumberingStyleName '+str(level-1)+' '+value.encode('iso-8859-15')
62
63def printChildren(cursor,text,node,level,keep_trace):
64    if node.nodeType==3:
65        level-=1
66
67    if not(node.nodeValue!=None and len(node.nodeValue.replace(' ',''))!=1 and keep_trace!='' and keep_trace!=None):
68        if keep_trace!='':
69            addToText(cursor,text,level-1,keep_trace)
70        keep_trace=node.nodeName
71
72    if node.hasChildNodes():
73        for i in node.childNodes:
74            printChildren(cursor,text,i,level+1,keep_trace)
75            keep_trace=''
76    else:
77        if node.nodeValue != None and len(node.nodeValue.replace(' ',''))>1:
78            addToText(cursor,text,level-1,keep_trace+' : '+node.nodeValue)
79            keep_trace=''
80        else:
81            if node.nodeValue != None and len(node.nodeValue.replace(' ',''))>1:
82                addToText(cursor,text,level-1,keep_trace+' : '+node.nodeValue)
83                keep_trace=''
84            else:
85                if keep_trace!='#text':
86                    addToText(cursor,text,level-1,keep_trace)
87                    keep_trace=''
88
89    if node.nodeType==1 and node.hasAttributes():
90        i=0
91        while i<node.attributes.length:
92            addToText(cursor,text,level,'(attr) '+node.attributes.keys()[i] + ' : ' + node.attributes[node.attributes.keys()[i]].value)
93            i+=1
94
95       
96
97def Xml2Pdf(conf,input,output):
98    localContext = uno.getComponentContext()
99    resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
100    ctx = resolver.resolve( "uno:socket,host=127.0.0.1,port=3662;urp;StarOffice.ComponentContext" )
101    smgr = ctx.ServiceManager
102    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
103    adressDoc=systemPathToFileUrl(input["doc"]["value"])
104    propFich=PropertyValue("Hidden", 0, True, 0),
105    try:
106        myDocument = desktop.loadComponentFromURL(adressDoc,"_blank",0,propFich)
107        #Prefer to create a new document without any style ?
108        #myDocument = desktop.loadComponentFromURL("private:factory/writer","_blank",0,propFich)
109    except:
110        conf["lenv"]["message"]='Unable to load input document'
111        return 4
112    text = myDocument.Text
113    cursor = text.createTextCursor()
114    cursor.gotoStart(0)
115    cursor.gotoEnd(1)
116    xmldoc = minidom.parseString(input['xml']['value'])
117
118    if xmldoc.hasChildNodes():
119        for i in xmldoc.childNodes:
120            if i.nodeType==1:
121                cursor.ParaStyleName="Title"
122                text.insertString( cursor, i.nodeName , 0 )
123                text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 )
124                #print >> sys.stderr,' * 1st level' + i.nodeName
125                if i.hasChildNodes():
126                    for j in i.childNodes:
127                        printChildren(cursor,text,j,2,'')
128
129    tmp=myDocument.StyleFamilies.getByName("NumberingStyles")
130
131    tmp1=tmp.getByName("Puce 1")
132
133    prop1Fich = ( PropertyValue( "FilterName" , 0, "writer_pdf_Export", 0 ),PropertyValue( "Overwrite" , 0, True , 0 ) )
134    outputDoc=systemPathToFileUrl("/tmp/output.pdf")
135    myDocument.storeToURL(outputDoc,prop1Fich)
136
137    myDocument.close(True)
138    ctx.ServiceManager
139    output["Document"]["value"]= open('/tmp/output.pdf', 'r').read()
140    print >> sys.stderr,len(output["Document"]["value"])
141    return 3
142
143#To run test from command line uncomment the following line:
144#xml2pdf({},{"file":{"value":"/tmp/demo.xml"},"doc":{"value":"/tmp/demo.odt"}},{})
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png