Added initial implementation of XmlPaperParser.

This commit is contained in:
Jim Evins
2013-11-03 01:23:09 -04:00
parent 438ea40878
commit cbae7243b8
11 changed files with 254 additions and 38 deletions
+1
View File
@@ -18,6 +18,7 @@ set (libglabels_sources
StrUtil.cpp
Template.cpp
Db.cpp
XmlPaperParser.cpp
)
set (libglabels_qobject_headers
+16 -18
View File
@@ -52,7 +52,7 @@ namespace libglabels
}
else
{
qDebug( "Duplicate paper ID: \"%s\".", paper->id().toStdString().c_str() );
qDebug( "Duplicate paper ID: \"%s\".", qPrintable(paper->id()) );
}
}
@@ -74,7 +74,7 @@ namespace libglabels
}
}
qDebug( "Unknown paper name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown paper name: \"%s\".", qPrintable(name) );
return NULL;
}
@@ -96,7 +96,7 @@ namespace libglabels
}
}
qDebug( "Unknown paper ID: \"%s\".", id.toStdString().c_str() );
qDebug( "Unknown paper ID: \"%s\".", qPrintable(id) );
return NULL;
}
@@ -112,7 +112,7 @@ namespace libglabels
}
}
qDebug( "Unknown paper name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown paper name: \"%s\".", qPrintable(name) );
return mEmpty;
}
@@ -128,7 +128,7 @@ namespace libglabels
}
}
qDebug( "Unknown paper id: \"%s\".", id.toStdString().c_str() );
qDebug( "Unknown paper id: \"%s\".", qPrintable(id) );
return mEmpty;
}
@@ -164,7 +164,7 @@ namespace libglabels
}
else
{
qDebug( "Duplicate category ID: \"%s\".", category->id().toStdString().c_str() );
qDebug( "Duplicate category ID: \"%s\".", qPrintable(category->id()) );
}
}
@@ -186,7 +186,7 @@ namespace libglabels
}
}
qDebug( "Unknown category name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown category name: \"%s\".", qPrintable(name) );
return NULL;
}
@@ -208,7 +208,7 @@ namespace libglabels
}
}
qDebug( "Unknown category ID: \"%s\".", id.toStdString().c_str() );
qDebug( "Unknown category ID: \"%s\".", qPrintable(id) );
return NULL;
}
@@ -224,7 +224,7 @@ namespace libglabels
}
}
qDebug( "Unknown category name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown category name: \"%s\".", qPrintable(name) );
return mEmpty;
}
@@ -240,7 +240,7 @@ namespace libglabels
}
}
qDebug( "Unknown category id: \"%s\".", id.toStdString().c_str() );
qDebug( "Unknown category id: \"%s\".", qPrintable(id) );
return mEmpty;
}
@@ -269,7 +269,7 @@ namespace libglabels
}
else
{
qDebug( "Duplicate vendor name: \"%s\".", vendor->name().toStdString().c_str() );
qDebug( "Duplicate vendor name: \"%s\".", qPrintable(vendor->name()) );
}
}
@@ -291,7 +291,7 @@ namespace libglabels
}
}
qDebug( "Unknown vendor name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown vendor name: \"%s\".", qPrintable(name) );
return NULL;
}
@@ -307,7 +307,7 @@ namespace libglabels
}
}
qDebug( "Unknown vendor name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown vendor name: \"%s\".", qPrintable(name) );
return mEmpty;
}
@@ -335,7 +335,7 @@ namespace libglabels
}
else
{
qDebug( "Duplicate template name: \"%s\".", tmplate->name().toStdString().c_str() );
qDebug( "Duplicate template name: \"%s\".", qPrintable(tmplate->name()) );
}
}
@@ -357,7 +357,7 @@ namespace libglabels
}
}
qDebug( "Unknown template name: \"%s\".", name.toStdString().c_str() );
qDebug( "Unknown template name: \"%s\".", qPrintable(name) );
return NULL;
}
@@ -379,9 +379,7 @@ namespace libglabels
}
}
qDebug( "Unknown template brand, part: \"%s\", \"%s\".",
brand.toStdString().c_str(),
part.toStdString().c_str() );
qDebug( "Unknown template brand, part: \"%s\", \"%s\".", qPrintable(brand), qPrintable(part) );
return NULL;
}
+1 -1
View File
@@ -48,7 +48,7 @@ namespace libglabels
public:
static Db *instance() { return new Db; }
static Db *instance() { static Db *db = new Db(); return db; }
static void registerPaper( Paper *paper );
static const Paper *lookupPaperFromName( const QString &name );
+5 -5
View File
@@ -36,16 +36,16 @@ namespace libglabels
QString dStr = StrUtil::formatFraction( 2 * mR1 * units->unitsPerPoint() );
mSizeDescription = QString().sprintf( "%s %s %s",
dStr.toStdString().c_str(),
units->name().toStdString().c_str(),
tr("diameter").toStdString().c_str() );
qPrintable(dStr),
qPrintable(units->name()),
qPrintable(tr("diameter")) );
}
else
{
mSizeDescription = QString().sprintf( "%.5g %s %s",
2 * mR1 * units->unitsPerPoint(),
units->name().toStdString().c_str(),
tr("diameter").toStdString().c_str() );
qPrintable(units->name()),
qPrintable(tr("diameter")) );
}
return mSizeDescription;
+4 -4
View File
@@ -37,16 +37,16 @@ namespace libglabels
QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() );
mSizeDescription = QString().sprintf( "%s x %s %s",
wStr.toStdString().c_str(),
hStr.toStdString().c_str(),
units->name().toStdString().c_str() );
qPrintable(wStr),
qPrintable(hStr),
qPrintable(units->name()) );
}
else
{
mSizeDescription = QString().sprintf( "%.5g x %.5g %s",
mW * units->unitsPerPoint(),
mH * units->unitsPerPoint(),
units->name().toStdString().c_str() );
qPrintable(units->name()) );
}
}
+4 -4
View File
@@ -37,16 +37,16 @@ namespace libglabels
QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() );
mSizeDescription = QString().sprintf( "%s x %s %s",
wStr.toStdString().c_str(),
hStr.toStdString().c_str(),
units->name().toStdString().c_str() );
qPrintable(wStr),
qPrintable(hStr),
qPrintable(units->name()) );
}
else
{
mSizeDescription = QString().sprintf( "%.5g x %.5g %s",
mW * units->unitsPerPoint(),
mH * units->unitsPerPoint(),
units->name().toStdString().c_str() );
qPrintable(units->name()) );
}
return mSizeDescription;
+5 -5
View File
@@ -36,16 +36,16 @@ namespace libglabels
QString dStr = StrUtil::formatFraction( 2 * mR * units->unitsPerPoint() );
mSizeDescription = QString().sprintf( "%s %s %s",
dStr.toStdString().c_str(),
units->name().toStdString().c_str(),
tr("diameter").toStdString().c_str() );
qPrintable(dStr),
qPrintable(units->name()),
qPrintable(tr("diameter")) );
}
else
{
mSizeDescription = QString().sprintf( "%.5g %s %s",
2 * mR * units->unitsPerPoint(),
units->name().toStdString().c_str(),
tr("diameter").toStdString().c_str() );
qPrintable(units->name()),
qPrintable(tr("diameter")) );
}
return mSizeDescription;
+112
View File
@@ -0,0 +1,112 @@
/* XmlPaperParser.cpp
*
* Copyright (C) 2013 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "XmlPaperParser.h"
#include <QFile>
#include <QDomDocument>
#include <QDomNode>
#include <iostream>
#include "Paper.h"
#include "XmlUtil.h"
#include "Db.h"
namespace libglabels
{
bool XmlPaperParser::readFile( const QString &fileName )
{
QFile file( fileName );
if ( !file.open( QFile::ReadOnly | QFile::Text) )
{
std::cerr << "Error: Cannot read file " << qPrintable(fileName)
<< ": " << qPrintable(file.errorString())
<< std::endl;
return false;
}
QDomDocument doc;
QString errorString;
int errorLine;
int errorColumn;
if ( !doc.setContent( &file, false, &errorString, &errorLine, &errorColumn ) )
{
std::cerr << "Error: Parse error at line " << errorLine
<< "column " << errorColumn
<< ": " << qPrintable(errorString)
<< std::endl;
return false;
}
QDomElement root = doc.documentElement();
if ( root.tagName() != "Glabels-paper-sizes" )
{
std::cerr << "Error: Not a Glabels-paper-sizes file" << std::endl;
return false;
}
parseRootNode( root );
return true;
}
void XmlPaperParser::parseRootNode( const QDomElement &node )
{
QDomNode child = node;
while ( !child.isNull() )
{
if ( child.toElement().tagName() == "Paper-size" )
{
parsePaperSizeNode( child.toElement() );
}
else if ( !child.isComment() )
{
std::cerr << "Warning: bad element: " << qPrintable(child.toElement().tagName())
<< ", Ignored"
<< std::endl;
}
}
}
void XmlPaperParser::parsePaperSizeNode( const QDomElement &node )
{
QString id = XmlUtil::getAttrString( node, "id", "" );
QString name = XmlUtil::getAttrStringI18n( node, "name", "" );
double width = XmlUtil::getAttrLength( node, "width", 0 );
double height = XmlUtil::getAttrLength( node, "height", 0 );
QString pwgSize = XmlUtil::getAttrString( node, "pwg_size", "" );
Paper *paper = new Paper( id, name, width, height, pwgSize );
if ( paper != NULL )
{
Db::registerPaper( paper );
}
}
}
+46
View File
@@ -0,0 +1,46 @@
/* XmlPaperParser.h
*
* Copyright (C) 2013 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef libglabels_XmlPaperParser_h
#define libglabels_XmlPaperParser_h
#include <QString>
#include <QDomElement>
namespace libglabels
{
class XmlPaperParser
{
public:
XmlPaperParser() {}
bool readFile( const QString &fileName );
private:
void parseRootNode( const QDomElement &node );
void parsePaperSizeNode( const QDomElement &node );
};
}
#endif // libglabels_XmlPaperParser_h
+59
View File
@@ -0,0 +1,59 @@
/* XmlUtil.h
*
* Copyright (C) 2013 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef libglabels_XmlUtil_h
#define libglabels_XmlUtil_h
#include <QString>
#include <QDomElement>
#include "Units.h"
namespace libglabels
{
class XmlUtil
{
private:
XmlUtil()
{
mDefaultUnits = Units::point();
}
static void init()
{
static XmlUtil *xmlUtil = new XmlUtil();
}
public:
static QString getAttrString( const QDomElement &node, const QString &name, const QString &default_val );
static QString getAttrStringI18n( const QDomElement &node, const QString &name, const QString &default_val );
static double getAttrLength( const QDomElement &node, const QString &name, double default_val );
private:
static Units *mDefaultUnits;
};
}
#endif // libglabels_XmlUtil_h