Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Freemarker как получить переменные xml шаблона


Автор: jvoid 24.8.2010, 11:34
Есть шаблон документа это файл content.xml из odt файла OpenOffice 
Файл шаблона:
Код

<office:document-content office:version="1.2" grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl">
<office:scripts/>
−
<office:font-face-decls>
<style:font-face style:name="Mangal1" svg:font-family="Mangal"/>
<style:font-face style:name="Times New Roman" svg:font-family="'Times New Roman'" style:font-family-generic="roman" style:font-pitch="variable"/>
<style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/>
<style:font-face style:name="Arial Unicode MS" svg:font-family="'Arial Unicode MS'" style:font-family-generic="system" style:font-pitch="variable"/>
<style:font-face style:name="Mangal" svg:font-family="Mangal" style:font-family-generic="system" style:font-pitch="variable"/>
</office:font-face-decls>
−
<office:automatic-styles>
−
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="10.98cm" fo:margin-right="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="en" fo:country="US"/>
</style:style>
−
<style:style style:name="P2" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="10.98cm" fo:margin-right="0cm" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="en" fo:country="US"/>
</style:style>
−
<style:style style:name="P3" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="-0.053cm" fo:margin-right="0cm" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="en" fo:country="US"/>
</style:style>
−
<style:style style:name="P4" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="8.07cm" fo:margin-right="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="ru" fo:country="RU"/>
</style:style>
−
<style:style style:name="P5" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="0.026cm" fo:margin-right="0cm" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="en" fo:country="US"/>
</style:style>
−
<style:style style:name="P6" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="0.026cm" fo:margin-right="0cm" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="ru" fo:country="RU"/>
</style:style>
−
<style:style style:name="P7" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="0.026cm" fo:margin-right="0cm" fo:text-align="justify" style:justify-single-word="false" fo:text-indent="0.953cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="ru" fo:country="RU"/>
</style:style>
−
<style:style style:name="P8" style:family="paragraph" style:parent-style-name="Standard">
<style:paragraph-properties fo:margin-left="8.07cm" fo:margin-right="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/>
<style:text-properties fo:language="ru" fo:country="RU"/>
</style:style>
</office:automatic-styles>
−
<office:body>
−
<office:text>
−
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
</text:sequence-decls>
<text:p text:style-name="P4">Міська рада</text:p>
<text:p text:style-name="P4"/>
<text:p text:style-name="P4">${фамилия} </text:p>
<text:p text:style-name="P4">${имя} ${отчество}</text:p>
<text:p text:style-name="P1"/>
<text:p text:style-name="P1"/>
<text:p text:style-name="P5"/>
<text:p text:style-name="P5"/>
<text:p text:style-name="P6">ЗАЯВЛЕНИЕ</text:p>
<text:p text:style-name="P6"/>
<text:p text:style-name="P7">Хочу земли, срочно.</text:p>
<text:p text:style-name="P2"/>
<text:p text:style-name="P3"/>
</office:text>
</office:body>
</office:document-content>

В OO это выглядит как:
Код

Міська рада

${фамилия} 
${имя} ${отчество}

ЗАЯВЛЕНИЕ

Задача такая:
Получить все переменные которые используются в этом шаблоне. Я пытаюсь сделать это так:
Код

Template temp = cfg.getTemplate(fileExtend.getTemplate(),
                    encodingTemplate);
            
            // получаем переменные из шаблона
            TemplateElement yy = temp.getRootTreeNode();
            System.out.println(yy.getChildCount());
            int p = 0;
            while (p <= yy.getChildCount() - 1) {
                System.out.println(yy.getChildAt(p));
                p++;
            }

Но в  ответ кроме своих трех переменных получаю еще куски XML кода:
Код

Template/1.odt
odtUTF8
odtUTF8
log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
7
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" ..........
${фамилия}
 </text:p><text:p text:style-name="P4">
${имя}
 
${отчество}
</text:p><text:p text:style-name="P1"/><text:p text:style-name="P1"/><text:p text:style-name="P5"/><text:p text:style-name="P5"/>


Как это все исправит? Freеmarker считает что число элементов в шаблоне 7, вместо моих трех, кто то с таким сталкивался?

Автор: aleksandy 25.8.2010, 07:39
Тебе нужно просто заполнить заранее подготовленный шаблон? Тогда проще это http://i-rs.ru/O-kompanii/Stat-i/Java-OpenOffice.org-generator-otchetov OpenOffice.org.

Автор: jvoid 25.8.2010, 13:47
Цитата(aleksandy @  25.8.2010,  07:39 Найти цитируемый пост)
Тебе нужно просто заполнить заранее подготовленный шаблон? Тогда проще это сделать через родные инструменты OpenOffice.org. 

Да именно так, нужно заполнить заранее подготовленный шаблон, я видел эту статью, мне это не подходит. Мне нужно работать не только с шаблонами odt, но и с docx, а желательно и с doc, к тому же сейчас все работает нормально, шаблоны odt и docx заполняются никаких притензий к этой части приложения нет.
Сейчас возникла другая необходимость  мне нужно с помощью freemarker получить переменные которые используются в уже существующем шаблоне. Хотелось бы сделать это средствами freemarker что б свой велосипед не изобретать.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)