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 
10 namespace 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 
43  }
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 }
static QString templateRoot(QString projectRoot)
Appends the root directory name &#39;templates&#39; to the project .
Definition: Project.cpp:2125
void deleteFromDisk()
Delete the template from disk.
Definition: Template.cpp:88
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 * m_xmlHandlerTemplate
A pointer to the Template object to be read or written.
Definition: Template.h:81
The main project for ipce.
Definition: Project.h:289
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
File name manipulation and expansion.
Definition: FileName.h:116
XmlHandler(Template *currentTemplate, FileName templateFolder)
Constructor for the Template object&#39;s XmlHandler.
Definition: Template.cpp:123
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object&#39;s member data to an XML stream.
Definition: Template.cpp:105
QString fileName() const
Get the file name that this Template represents.
Definition: Template.cpp:50
Template(QString fileName, QString templateType, QString importName, QObject *parent=0)
Create a Template from template file&#39;s name.
Definition: Template.cpp:19
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
QString importName() const
Get the name of the TemplateList this file was imported under.
Definition: Template.cpp:68
FileName m_xmlHandlerTemplateFolderName
The name of the folder for the template xml.
Definition: Template.h:83
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
QString templateType() const
Get the type of template.
Definition: Template.cpp:59
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
~Template()
Destroys Template object.
Definition: Template.cpp:42
Manage a stack of content handlers for reading XML files.