Isis 3 Programmer Reference
Template.cpp
1#include "Template.h"
2
3#include <QFile>
4
5#include "FileName.h"
6#include "IException.h"
7#include "Project.h"
8#include "XmlStackedHandlerReader.h"
9
10namespace Isis{
19 Template::Template(QString fileName, QString templateType, QString importName, QObject *parent) : QObject(parent) {
20 m_fileName = fileName;
21 m_templateType = templateType;
22 m_importName = importName;
23
24 }
25
33 Template::Template(FileName templateFolder, XmlStackedHandlerReader *xmlReader, QObject *parent) :
34 QObject(parent) {
35 xmlReader->pushContentHandler(new XmlHandler(this, templateFolder));
36 }
37
38
44
45
50 QString Template::fileName() const {
51 return m_fileName;
52 }
53
54
59 QString Template::templateType() const {
60 return m_templateType;
61 }
62
63
68 QString Template::importName() const {
69 return m_importName;
70 }
71
72
79 m_fileName = project->templateRoot() + "/" + m_templateType + "/" + m_importName +"/" + FileName(m_fileName).name();
80 }
81
82
89 if (!QFile::remove(m_fileName)) {
91 tr("Could not remove file [%1]").arg(m_fileName),
92 _FILEINFO_);
93 }
94 }
95
96
105 void Template::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
106 const {
107 stream.writeStartElement("template");
108 stream.writeAttribute("fileName", FileName(m_fileName).name());
109 stream.writeAttribute("templateType", m_templateType);
110 stream.writeAttribute("importName", m_importName);
111 stream.writeEndElement();
112
113 }
114
115
123 Template::XmlHandler::XmlHandler(Template *currentTemplate, FileName templateFolder) {
124 m_xmlHandlerTemplate = currentTemplate;
125 m_xmlHandlerTemplateFolderName = templateFolder;
126 }
127
128
141 bool Template::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
142 const QString &qName, const QXmlAttributes &atts) {
143 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
144 if (localName == "template") {
145 QString fileName = atts.value("fileName");
146 QString templateType = atts.value("templateType");
147 QString importName = atts.value("importName");
148
149 if (!fileName.isEmpty()) {
150 m_xmlHandlerTemplate->m_fileName = m_xmlHandlerTemplateFolderName.expanded()
151 + "/" + templateType
152 + "/" + importName
153 + "/" + fileName;
154 }
155
156 if (!templateType.isEmpty()) {
157 m_xmlHandlerTemplate->m_templateType = templateType;
158 }
159
160 if (!importName.isEmpty()) {
161 m_xmlHandlerTemplate->m_importName = importName;
162 }
163 }
164 }
165 return true;
166 }
167}
File name manipulation and expansion.
Definition FileName.h:100
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
Isis exception class.
Definition IException.h:91
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
The main project for ipce.
Definition Project.h:289
static QString templateRoot(QString projectRoot)
Appends the root directory name 'templates' to the project .
Definition Project.cpp:2124
FileName m_xmlHandlerTemplateFolderName
The name of the folder for the template xml.
Definition Template.h:69
Template * m_xmlHandlerTemplate
A pointer to the Template object to be read or written.
Definition Template.h:67
XmlHandler(Template *currentTemplate, FileName templateFolder)
Constructor for the Template object's XmlHandler.
Definition Template.cpp:123
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Method to read the given XML formatted attribute for a Template object into the XmlHandler.
Definition Template.cpp:141
Template(QString fileName, QString templateType, QString importName, QObject *parent=0)
Create a Template from template file's name.
Definition Template.cpp:19
QString fileName() const
Get the file name that this Template represents.
Definition Template.cpp:50
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object's member data to an XML stream.
Definition Template.cpp:105
QString templateType() const
Get the type of template.
Definition Template.cpp:59
void deleteFromDisk()
Delete the template from disk.
Definition Template.cpp:88
~Template()
Destroys Template object.
Definition Template.cpp:42
void updateFileName(Project *project)
Change the file name for this template to be where it now is with the given project.
Definition Template.cpp:78
QString importName() const
Get the name of the TemplateList this file was imported under.
Definition Template.cpp:68
Manage a stack of content handlers for reading XML files.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16