2015-09-03 00:55:04 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# DOC MODULE
|
|
|
|
####################################################################################################################################
|
2015-10-28 11:10:36 +02:00
|
|
|
package BackRestDoc::Common::Doc;
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
# use Exporter qw(import);
|
|
|
|
# our @EXPORT = qw();
|
2015-09-03 00:55:04 +02:00
|
|
|
use File::Basename qw(dirname);
|
2015-10-28 11:10:36 +02:00
|
|
|
use Scalar::Util qw(blessed);
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
use lib dirname($0) . '/../lib';
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::FileCommon;
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Operation constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant OP_DOC => 'Doc';
|
|
|
|
|
|
|
|
use constant OP_DOC_BUILD => OP_DOC . '->build';
|
2015-09-08 13:31:24 +02:00
|
|
|
use constant OP_DOC_NAME_GET => OP_DOC . '->nameGet';
|
2015-09-03 00:55:04 +02:00
|
|
|
use constant OP_DOC_NEW => OP_DOC . '->new';
|
2015-09-08 13:31:24 +02:00
|
|
|
use constant OP_DOC_NODE_BLESS => OP_DOC . '->nodeBless';
|
2015-09-03 00:55:04 +02:00
|
|
|
use constant OP_DOC_NODE_GET => OP_DOC . '->nodeGet';
|
2015-09-08 13:31:24 +02:00
|
|
|
use constant OP_DOC_NODE_LIST => OP_DOC . '->nodeList';
|
2015-11-22 23:44:01 +02:00
|
|
|
use constant OP_DOC_NODE_REMOVE => OP_DOC . '->nodeRemove';
|
2015-09-08 13:31:24 +02:00
|
|
|
use constant OP_DOC_PARAM_GET => OP_DOC . '->paramGet';
|
|
|
|
use constant OP_DOC_PARAM_SET => OP_DOC . '->paramSet';
|
2015-11-22 23:44:01 +02:00
|
|
|
use constant OP_DOC_PARAM_TEST => OP_DOC . '->paramSet';
|
2015-09-03 00:55:04 +02:00
|
|
|
use constant OP_DOC_PARSE => OP_DOC . '->parse';
|
2015-09-08 13:31:24 +02:00
|
|
|
use constant OP_DOC_VALUE_GET => OP_DOC . '->valueGet';
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant OP_DOC_VALUE_SET => OP_DOC . '->valueSet';
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
$self->{strClass} = $class;
|
|
|
|
|
2015-09-03 00:55:04 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
|
|
|
my $strOperation,
|
2015-11-22 23:44:01 +02:00
|
|
|
$self->{strFileName},
|
|
|
|
my $bCached
|
2015-09-03 00:55:04 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_NEW, \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strFileName', required => false},
|
|
|
|
{name => 'bCached', default => false}
|
2015-09-03 00:55:04 +02:00
|
|
|
);
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
# Load the doc from a file if one has been defined
|
|
|
|
if (defined($self->{strFileName}))
|
|
|
|
{
|
2015-11-22 23:44:01 +02:00
|
|
|
if ($bCached)
|
|
|
|
{
|
|
|
|
$self->oDoc = XMLin(fileStringRead($self->{strFileName}));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $oParser = XML::Checker::Parser->new(ErrorContext => 2, Style => 'Tree');
|
2015-09-03 00:55:04 +02:00
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
if (-e dirname($0) . '/dtd')
|
|
|
|
{
|
|
|
|
$oParser->set_sgml_search_path(dirname($0) . '/dtd')
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oParser->set_sgml_search_path(dirname($0) . '/xml/dtd');
|
|
|
|
}
|
2015-09-03 00:55:04 +02:00
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
my $oTree;
|
|
|
|
|
|
|
|
eval
|
2015-10-28 11:10:36 +02:00
|
|
|
{
|
2016-05-19 16:25:04 +02:00
|
|
|
local $XML::Checker::FAIL;
|
|
|
|
$XML::Checker::FAIL = sub
|
2015-11-22 23:44:01 +02:00
|
|
|
{
|
|
|
|
my $iCode = shift;
|
|
|
|
|
|
|
|
die XML::Checker::error_string($iCode, @_);
|
|
|
|
};
|
2015-09-03 00:55:04 +02:00
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
$oTree = $oParser->parsefile($self->{strFileName});
|
2015-10-28 11:10:36 +02:00
|
|
|
};
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
# Report any error that stopped parsing
|
|
|
|
if ($@)
|
|
|
|
{
|
|
|
|
$@ =~ s/at \/.*?$//s; # remove module line number
|
|
|
|
die "malformed xml in '$self->{strFileName}':\n" . trim($@);
|
|
|
|
}
|
2015-09-03 00:55:04 +02:00
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
# Parse and build the doc
|
|
|
|
$self->{oDoc} = $self->build($self->parse(${$oTree}[0], ${$oTree}[1]));
|
2015-10-28 11:10:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else create a blank doc
|
|
|
|
else
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
2015-10-28 11:10:36 +02:00
|
|
|
$self->{oDoc} = {name => 'doc', children => []};
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
$self->{strName} = 'root';
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# parse
|
|
|
|
#
|
|
|
|
# Parse the xml doc into a more usable hash and array structure.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub parse
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
|
|
|
$oyNode
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_PARSE, \@_,
|
|
|
|
{name => 'strName', trace => true},
|
|
|
|
{name => 'oyNode', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my %oOut;
|
|
|
|
my $iIndex = 0;
|
2015-11-22 23:44:01 +02:00
|
|
|
my $bText = $strName eq 'text' || $strName eq 'li' || $strName eq 'p' || $strName eq 'title' ||
|
|
|
|
$strName eq 'summary' || $strName eq 'table-cell' || $strName eq 'table-column';
|
2015-09-03 00:55:04 +02:00
|
|
|
|
|
|
|
# Store the node name
|
|
|
|
$oOut{name} = $strName;
|
|
|
|
|
2016-05-19 16:25:04 +02:00
|
|
|
if (keys(%{$$oyNode[$iIndex]}))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
$oOut{param} = $$oyNode[$iIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
$iIndex++;
|
|
|
|
|
|
|
|
# Look for strings and children
|
|
|
|
while (defined($$oyNode[$iIndex]))
|
|
|
|
{
|
|
|
|
# Process string data
|
|
|
|
if (ref(\$$oyNode[$iIndex]) eq 'SCALAR' && $$oyNode[$iIndex] eq '0')
|
|
|
|
{
|
|
|
|
$iIndex++;
|
|
|
|
my $strBuffer = $$oyNode[$iIndex++];
|
|
|
|
|
|
|
|
# Strip tabs, CRs, and LFs
|
|
|
|
$strBuffer =~ s/\t|\r//g;
|
|
|
|
|
|
|
|
# If anything is left
|
|
|
|
if (length($strBuffer) > 0)
|
|
|
|
{
|
|
|
|
# If text node then create array entries for strings
|
|
|
|
if ($bText)
|
|
|
|
{
|
|
|
|
if (!defined($oOut{children}))
|
|
|
|
{
|
|
|
|
$oOut{children} = [];
|
|
|
|
}
|
|
|
|
|
2016-05-19 16:25:04 +02:00
|
|
|
push(@{$oOut{children}}, $strBuffer);
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
# Don't allow strings mixed with children
|
|
|
|
elsif (length(trim($strBuffer)) > 0)
|
|
|
|
{
|
|
|
|
if (defined($oOut{children}))
|
|
|
|
{
|
|
|
|
confess "text mixed with children in node ${strName} (spaces count)";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($oOut{value}))
|
|
|
|
{
|
|
|
|
confess "value is already defined in node ${strName} - this shouldn't happen";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Don't allow text mixed with
|
|
|
|
$oOut{value} = $strBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Process a child
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (defined($oOut{value}) && $bText)
|
|
|
|
{
|
|
|
|
confess "text mixed with children in node ${strName} before child " . $$oyNode[$iIndex++] . " (spaces count)";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($oOut{children}))
|
|
|
|
{
|
|
|
|
$oOut{children} = [];
|
|
|
|
}
|
|
|
|
|
2016-05-19 16:25:04 +02:00
|
|
|
push(@{$oOut{children}}, $self->parse($$oyNode[$iIndex++], $$oyNode[$iIndex++]));
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDoc', value => \%oOut, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# build
|
|
|
|
#
|
|
|
|
# Restructure the doc to make walking it easier.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub build
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oDoc
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_BUILD, \@_,
|
|
|
|
{name => 'oDoc', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Initialize the node object
|
2015-10-28 11:10:36 +02:00
|
|
|
my $oOut = {name => $$oDoc{name}, children => [], value => $$oDoc{value}};
|
2015-09-03 00:55:04 +02:00
|
|
|
my $strError = "in node $$oDoc{name}";
|
|
|
|
|
|
|
|
# Get all params
|
|
|
|
if (defined($$oDoc{param}))
|
|
|
|
{
|
2016-05-19 16:25:04 +02:00
|
|
|
for my $strParam (keys %{$$oDoc{param}})
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
$$oOut{param}{$strParam} = $$oDoc{param}{$strParam};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
if ($$oDoc{name} eq 'p' || $$oDoc{name} eq 'title' || $$oDoc{name} eq 'summary' ||
|
|
|
|
$$oDoc{name} eq 'table-cell' || $$oDoc{name} eq 'table-column')
|
2015-10-28 11:10:36 +02:00
|
|
|
{
|
|
|
|
$$oOut{field}{text} = $oDoc;
|
|
|
|
}
|
|
|
|
elsif (defined($$oDoc{children}))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
for (my $iIndex = 0; $iIndex < @{$$oDoc{children}}; $iIndex++)
|
|
|
|
{
|
|
|
|
my $oSub = $$oDoc{children}[$iIndex];
|
|
|
|
my $strName = $$oSub{name};
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
if ($strName eq 'text')
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
$$oOut{field}{text} = $oSub;
|
|
|
|
}
|
2015-10-28 11:10:36 +02:00
|
|
|
elsif (defined($$oSub{value}) && !defined($$oSub{param}))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
$$oOut{field}{$strName} = $$oSub{value};
|
|
|
|
}
|
2015-10-28 11:10:36 +02:00
|
|
|
elsif (!defined($$oSub{children}) && !defined($$oSub{value}) && !defined($$oSub{param}))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
$$oOut{field}{$strName} = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-19 16:25:04 +02:00
|
|
|
push(@{$$oOut{children}}, $self->build($oSub));
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDoc', value => $oOut, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# nodeGet
|
|
|
|
#
|
|
|
|
# Return a node by name - error if more than one exists
|
|
|
|
####################################################################################################################################
|
2015-09-08 13:31:24 +02:00
|
|
|
sub nodeGetById
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
2015-09-08 13:31:24 +02:00
|
|
|
$strId,
|
2015-09-03 00:55:04 +02:00
|
|
|
$bRequired,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_NODE_GET, \@_,
|
|
|
|
{name => 'strName', trace => true},
|
2015-09-08 13:31:24 +02:00
|
|
|
{name => 'strId', required => false, trace => true},
|
|
|
|
{name => 'bRequired', default => true, trace => true}
|
2015-09-03 00:55:04 +02:00
|
|
|
);
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
my $oDoc = $self->{oDoc};
|
2015-09-03 00:55:04 +02:00
|
|
|
my $oNode;
|
|
|
|
|
|
|
|
for (my $iIndex = 0; $iIndex < @{$$oDoc{children}}; $iIndex++)
|
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
if ((defined($strName) && $$oDoc{children}[$iIndex]{name} eq $strName) &&
|
|
|
|
(!defined($strId) || $$oDoc{children}[$iIndex]{param}{id} eq $strId))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
|
|
|
if (!defined($oNode))
|
|
|
|
{
|
|
|
|
$oNode = $$oDoc{children}[$iIndex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess "found more than one child ${strName} in node $$oDoc{name}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($oNode) && $bRequired)
|
|
|
|
{
|
2015-10-28 11:10:36 +02:00
|
|
|
confess "unable to find child ${strName}" . (defined($strId) ? " (${strId})" : '') . " in node $$oDoc{name}";
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2015-09-08 13:31:24 +02:00
|
|
|
{name => 'oNodeDoc', value => $self->nodeBless($oNode), trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# nodeGet
|
|
|
|
#
|
|
|
|
# Return a node by name - error if more than one exists
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->nodeGetById(shift, undef, shift);
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# nodeTest
|
|
|
|
#
|
|
|
|
# Test that a node exists
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeTest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return defined($self->nodeGetById(shift, undef, false));
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# nodeAdd
|
|
|
|
#
|
|
|
|
# Add a node to to the current doc's child list
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeAdd
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strName = shift;
|
|
|
|
my $strValue = shift;
|
|
|
|
my $oParam = shift;
|
|
|
|
my $oField = shift;
|
|
|
|
|
|
|
|
my $oDoc = $self->{oDoc};
|
|
|
|
my $oNode = {name => $strName, value => $strValue, param => $oParam, field => $oField};
|
|
|
|
|
|
|
|
push(@{$$oDoc{children}}, $oNode);
|
|
|
|
|
|
|
|
return $self->nodeBless($oNode);
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# nodeBless
|
|
|
|
#
|
|
|
|
# Make a new Doc object from a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeBless
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oNode
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_NODE_BLESS, \@_,
|
|
|
|
{name => 'oNode', required => false, trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $oDoc;
|
|
|
|
|
|
|
|
if (defined($oNode))
|
|
|
|
{
|
|
|
|
$oDoc = {};
|
|
|
|
bless $oDoc, $self->{strClass};
|
|
|
|
|
|
|
|
$oDoc->{strClass} = $self->{strClass};
|
|
|
|
$oDoc->{strName} = $$oNode{name};
|
|
|
|
$oDoc->{oDoc} = $oNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDoc', value => $oDoc, trace => true}
|
2015-09-03 00:55:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# nodeList
|
|
|
|
#
|
|
|
|
# Get a list of nodes.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeList
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
|
|
|
$bRequired,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2015-09-08 13:31:24 +02:00
|
|
|
OP_DOC_NODE_LIST, \@_,
|
2015-09-03 00:55:04 +02:00
|
|
|
{name => 'strName', required => false, trace => true},
|
|
|
|
{name => 'bRequired', default => true, trace => true},
|
|
|
|
);
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
my $oDoc = $self->{oDoc};
|
2015-09-03 00:55:04 +02:00
|
|
|
my @oyNode;
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
if (defined($$oDoc{children}))
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
for (my $iIndex = 0; $iIndex < @{$$oDoc{children}}; $iIndex++)
|
2015-09-03 00:55:04 +02:00
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
if (!defined($strName) || $$oDoc{children}[$iIndex]{name} eq $strName)
|
|
|
|
{
|
|
|
|
if (ref(\$$oDoc{children}[$iIndex]) eq "SCALAR")
|
|
|
|
{
|
|
|
|
push(@oyNode, $$oDoc{children}[$iIndex]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
push(@oyNode, $self->nodeBless($$oDoc{children}[$iIndex]));
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (@oyNode == 0 && $bRequired)
|
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
confess 'unable to find ' . (defined($strName) ? "children named '${strName}'" : 'any children') . " in node $$oDoc{name}";
|
2015-09-03 00:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2015-09-08 13:31:24 +02:00
|
|
|
{name => 'oyNode', value => \@oyNode, trace => true}
|
2015-09-03 00:55:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# nodeRemove
|
|
|
|
#
|
|
|
|
# Remove a child node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nodeRemove
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oChildRemove
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_NODE_REMOVE, \@_,
|
|
|
|
{name => 'oChildRemove', required => false, trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $bRemove = false;
|
|
|
|
my $oDoc = $self->{oDoc};
|
|
|
|
|
|
|
|
# Error if there are no children
|
|
|
|
if (!defined($$oDoc{children}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "node has no children");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (my $iIndex = 0; $iIndex < @{$$oDoc{children}}; $iIndex++)
|
|
|
|
{
|
|
|
|
if ($$oDoc{children}[$iIndex] == $oChildRemove->{oDoc})
|
|
|
|
{
|
|
|
|
splice(@{$$oDoc{children}}, $iIndex, 1);
|
|
|
|
$bRemove = true;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bRemove)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "child was not found in node");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# nameGet
|
|
|
|
####################################################################################################################################
|
|
|
|
sub nameGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my $strOperation = logDebugParam(OP_DOC_NAME_GET);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strName', value => ${$self->{oDoc}}{name}, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# valueGet
|
|
|
|
####################################################################################################################################
|
|
|
|
sub valueGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my $strOperation = logDebugParam(OP_DOC_VALUE_GET);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strValue', value => ${$self->{oDoc}}{value}, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# valueSet
|
|
|
|
####################################################################################################################################
|
|
|
|
sub valueSet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strValue = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my $strOperation = logDebugParam(OP_DOC_VALUE_SET);
|
|
|
|
|
|
|
|
# Set the value
|
|
|
|
${$self->{oDoc}}{value} = $strValue;
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# paramGet
|
|
|
|
#
|
|
|
|
# Get a parameter from a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub paramGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
|
|
|
$bRequired,
|
2015-11-22 23:44:01 +02:00
|
|
|
$strDefault,
|
2015-09-08 13:31:24 +02:00
|
|
|
$strType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_PARAM_GET, \@_,
|
|
|
|
{name => 'strName', trace => true},
|
|
|
|
{name => 'bRequired', default => true, trace => true},
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strDefault', required => false, trace => true},
|
2015-09-08 13:31:24 +02:00
|
|
|
{name => 'strType', default => 'param', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $strValue = ${$self->{oDoc}}{$strType}{$strName};
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
if (!defined($strValue))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
2015-11-22 23:44:01 +02:00
|
|
|
if ($bRequired)
|
|
|
|
{
|
2016-05-19 23:10:34 +02:00
|
|
|
confess "${strType} '${strName}' is required in node '$self->{strName}'";
|
2015-11-22 23:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$strValue = $strDefault;
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strValue', value => $strValue, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# paramTest
|
|
|
|
#
|
|
|
|
# Test that a parameter exists or has a certain value.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub paramTest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
|
|
|
$strExpectedValue,
|
|
|
|
$strType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_PARAM_TEST, \@_,
|
|
|
|
{name => 'strName', trace => true},
|
|
|
|
{name => 'strExpectedValue', required => false, trace => true},
|
|
|
|
{name => 'strType', default => 'param', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $bResult = true;
|
|
|
|
my $strValue = $self->paramGet($strName, false, undef, $strType);
|
|
|
|
|
|
|
|
if (!defined($strValue))
|
|
|
|
{
|
|
|
|
$bResult = false;
|
|
|
|
}
|
|
|
|
elsif (defined($strExpectedValue) && $strValue ne $strExpectedValue)
|
|
|
|
{
|
|
|
|
$bResult = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bResult', value => $bResult, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# paramSet
|
|
|
|
#
|
|
|
|
# Set a parameter in a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub paramSet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName,
|
|
|
|
$strValue,
|
|
|
|
$strType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DOC_PARAM_SET, \@_,
|
|
|
|
{name => 'strName', trace => true},
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strValue', required => false, trace => true},
|
2015-09-08 13:31:24 +02:00
|
|
|
{name => 'strType', default => 'param', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
${$self->{oDoc}}{$strType}{$strName} = $strValue;
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# fieldGet
|
|
|
|
#
|
|
|
|
# Get a field from a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub fieldGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
return $self->paramGet(shift, shift, shift, 'field');
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# fieldTest
|
|
|
|
#
|
|
|
|
# Test if a field exists.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub fieldTest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->paramTest(shift, shift, 'field');
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# textGet
|
|
|
|
#
|
|
|
|
# Get a field from a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub textGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
return $self->nodeBless($self->paramGet('text', shift, shift, 'field'));
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# textSet
|
|
|
|
#
|
|
|
|
# Get a field from a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub textSet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $oText = shift;
|
|
|
|
|
|
|
|
if (blessed($oText) && $oText->isa('BackRestDoc::Common::Doc'))
|
|
|
|
{
|
|
|
|
$oText = $oText->{oDoc};
|
|
|
|
}
|
|
|
|
elsif (ref($oText) ne 'HASH')
|
|
|
|
{
|
|
|
|
$oText = {name => 'text', children => [$oText]};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->paramSet('text', $oText, 'field');
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# fieldSet
|
|
|
|
#
|
|
|
|
# Set a parameter in a node.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub fieldSet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->paramSet(shift, shift, 'field');
|
|
|
|
}
|
|
|
|
|
2015-09-03 00:55:04 +02:00
|
|
|
1;
|