Kaydet (Commit) 24d6ac27 authored tarafından Miklos Vajna's avatar Miklos Vajna

sfx2 classification: initial policy parser

Change-Id: Ia7406bdc94cbceb5b66ab9d12355c1e9f4061206
üst 1f119957
...@@ -13,12 +13,24 @@ ...@@ -13,12 +13,24 @@
#include <com/sun/star/beans/XPropertyContainer.hpp> #include <com/sun/star/beans/XPropertyContainer.hpp>
#include <com/sun/star/document/XDocumentProperties.hpp> #include <com/sun/star/document/XDocumentProperties.hpp>
#include <com/sun/star/xml/sax/Parser.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/SAXParseException.hpp>
#include <sfx2/objsh.hxx> #include <sfx2/objsh.hxx>
#include <o3tl/make_unique.hxx> #include <o3tl/make_unique.hxx>
#include <comphelper/processfactory.hxx>
#include <rtl/bootstrap.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <unotools/streamwrap.hxx>
#include <cppuhelper/implbase.hxx>
#include <config_folders.h>
using namespace com::sun::star; using namespace com::sun::star;
namespace
{
/// Represents one category of a classification policy. /// Represents one category of a classification policy.
class SfxClassificationCategory class SfxClassificationCategory
{ {
...@@ -26,14 +38,274 @@ public: ...@@ -26,14 +38,274 @@ public:
std::map<OUString, OUString> m_aLabels; std::map<OUString, OUString> m_aLabels;
}; };
/// Parses a policy XML conforming to the TSCP BAF schema.
class SfxClassificationParser : public cppu::WeakImplHelper<xml::sax::XDocumentHandler>
{
public:
std::map<OUString, SfxClassificationCategory> m_aCategories;
OUString m_aPolicyAuthorityName;
bool m_bInPolicyAuthorityName;
OUString m_aPolicyName;
bool m_bInPolicyName;
OUString m_aProgramID;
bool m_bInProgramID;
OUString m_aScale;
bool m_bInScale;
OUString m_aConfidentalityValue;
bool m_bInConfidentalityValue;
OUString m_aIdentifier;
bool m_bInIdentifier;
OUString m_aValue;
bool m_bInValue;
/// Pointer to a value in m_aCategories, the currently parsed category.
SfxClassificationCategory* m_pCategory;
SfxClassificationParser();
virtual ~SfxClassificationParser();
virtual void SAL_CALL startDocument() throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL endDocument() throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL startElement(const OUString& aName, const uno::Reference<xml::sax::XAttributeList>& xAttribs)
throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL endElement(const OUString& aName) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL characters(const OUString& aChars) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL processingInstruction(const OUString& aTarget, const OUString& aData) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
virtual void SAL_CALL setDocumentLocator(const uno::Reference<xml::sax::XLocator>& xLocator)
throw (xml::sax::SAXException, uno::RuntimeException, std::exception) override;
};
SfxClassificationParser::SfxClassificationParser()
: m_bInPolicyAuthorityName(false)
, m_bInPolicyName(false)
, m_bInProgramID(false)
, m_bInConfidentalityValue(false)
, m_bInIdentifier(false)
, m_bInValue(false)
, m_pCategory(nullptr)
{
}
SfxClassificationParser::~SfxClassificationParser()
{
}
void SAL_CALL SfxClassificationParser::startDocument() throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
}
void SAL_CALL SfxClassificationParser::endDocument() throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
}
void SAL_CALL SfxClassificationParser::startElement(const OUString& rName, const uno::Reference<xml::sax::XAttributeList>& xAttribs)
throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
if (rName == "baf:PolicyAuthorityName")
{
m_aPolicyAuthorityName.clear();
m_bInPolicyAuthorityName = true;
}
else if (rName == "baf:PolicyName")
{
m_aPolicyName.clear();
m_bInPolicyName = true;
}
else if (rName == "baf:ProgramID")
{
m_aProgramID.clear();
m_bInProgramID = true;
}
else if (rName == "baf:BusinessAuthorizationCategory")
{
OUString aIdentifier = xAttribs->getValueByName("Identifier");
OUString aName = xAttribs->getValueByName("Name");
if (!m_pCategory && !aName.isEmpty())
{
// Create a new category and initialize it with the data that's true for all categories.
SfxClassificationCategory& rCategory = m_aCategories[aName];
rCategory.m_aLabels["urn:bails:IntellectualProperty:PolicyAuthority:Name"] = m_aPolicyAuthorityName;
rCategory.m_aLabels["urn:bails:IntellectualProperty:Policy:Name"] = m_aPolicyName;
// Also initialize defaults.
rCategory.m_aLabels["urn:bails:IntellectualProperty:PolicyAuthority:Identifier"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:PolicyAuthority:Country"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Policy:Identifier"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorization:Name"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorization:Identifier"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorization:Locator"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Name"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Identifier"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Identifier:OID"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Locator"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:BusinessAuthorization:Locator"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:MarkingPrecedence"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-summary"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-warning-statement"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-warning-statement:ext:2"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-warning-statement:ext:3"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-warning-statement:ext:4"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-distribution-statement"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-distribution-statement:ext:2"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-distribution-statement:ext:3"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:general-distribution-statement:ext:4"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:document-footer"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:document-header"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:document-watermark"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:email-first-line-of-text"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:email-last-line-of-text"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:email-subject-prefix"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Marking:email-subject-suffix"] = "";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Authorization:StartValidity"] = "None";
rCategory.m_aLabels["urn:bails:IntellectualProperty:Authorization:StopValidity"] = "None";
m_pCategory = &rCategory;
}
}
else if (rName == "baf:Scale")
{
m_aScale.clear();
m_bInScale = true;
}
else if (rName == "baf:ConfidentalityValue")
{
m_aConfidentalityValue.clear();
m_bInConfidentalityValue = true;
}
else if (rName == "baf:Identifier")
{
m_aIdentifier.clear();
m_bInIdentifier = true;
}
else if (rName == "baf:Value")
{
m_aValue.clear();
m_bInValue = true;
}
}
void SAL_CALL SfxClassificationParser::endElement(const OUString& rName) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
if (rName == "baf:PolicyAuthorityName")
m_bInPolicyAuthorityName = false;
else if (rName == "baf:PolicyName")
m_bInPolicyName = false;
else if (rName == "baf:ProgramID")
m_bInProgramID = false;
else if (rName == "baf:BusinessAuthorizationCategory")
m_pCategory = nullptr;
else if (rName == "baf:Scale")
{
m_bInScale = false;
if (m_pCategory)
m_pCategory->m_aLabels["urn:bails:IntellectualProperty:Impact:Scale"] = m_aScale;
}
else if (rName == "baf:ConfidentalityValue")
{
m_bInConfidentalityValue = false;
if (m_pCategory)
{
std::map<OUString, OUString>& rLabels = m_pCategory->m_aLabels;
rLabels["urn:bails:IntellectualProperty:Impact:Level:Confidentiality"] = m_aConfidentalityValue;
// Set the two other type of levels as well, if they're not set
// yet: they're optional in BAF, but not in BAILS.
if (rLabels.find("urn:bails:IntellectualProperty:Impact:Level:Integrity") == rLabels.end())
rLabels["urn:bails:IntellectualProperty:Impact:Level:Integrity"] = m_aConfidentalityValue;
if (rLabels.find("urn:bails:IntellectualProperty:Impact:Level:Availability") == rLabels.end())
rLabels["urn:bails:IntellectualProperty:Impact:Level:Availability"] = m_aConfidentalityValue;
}
}
else if (rName == "baf:Identifier")
m_bInIdentifier = false;
else if (rName == "baf:Value")
{
if (m_pCategory)
{
if (m_aIdentifier == "Document: Header")
m_pCategory->m_aLabels["urn:bails:IntellectualProperty:Marking:document-header"] = m_aValue;
else if (m_aIdentifier == "Document: Footer")
m_pCategory->m_aLabels["urn:bails:IntellectualProperty:Marking:document-footer"] = m_aValue;
else if (m_aIdentifier == "Document: Watermark")
m_pCategory->m_aLabels["urn:bails:IntellectualProperty:Marking:document-watermark"] = m_aValue;
}
}
}
void SAL_CALL SfxClassificationParser::characters(const OUString& rChars) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
if (m_bInPolicyAuthorityName)
m_aPolicyAuthorityName += rChars;
else if (m_bInPolicyName)
m_aPolicyName += rChars;
else if (m_bInProgramID)
m_aProgramID += rChars;
else if (m_bInScale)
m_aScale += rChars;
else if (m_bInConfidentalityValue)
m_aConfidentalityValue += rChars;
else if (m_bInIdentifier)
m_aIdentifier += rChars;
else if (m_bInValue)
m_aValue += rChars;
}
void SAL_CALL SfxClassificationParser::ignorableWhitespace(const OUString& /*rWhitespace*/) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
}
void SAL_CALL SfxClassificationParser::processingInstruction(const OUString& /*rTarget*/, const OUString& /*rData*/) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
}
void SAL_CALL SfxClassificationParser::setDocumentLocator(const uno::Reference<xml::sax::XLocator>& /*xLocator*/) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
{
}
} // anonymous namespace
/// Implementation details of SfxClassificationHelper. /// Implementation details of SfxClassificationHelper.
struct SfxClassificationHelper::Impl class SfxClassificationHelper::Impl
{ {
public:
std::map<OUString, OUString> m_aLabels; std::map<OUString, OUString> m_aLabels;
/// Possible categories of a policy to choose from. /// Possible categories of a policy to choose from.
std::map<OUString, SfxClassificationCategory> m_aCategories; std::map<OUString, SfxClassificationCategory> m_aCategories;
void parsePolicy();
}; };
void SfxClassificationHelper::Impl::parsePolicy()
{
OUString aPath("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/classification/example.xml");
rtl::Bootstrap::expandMacros(aPath);
SvStream* pStream = utl::UcbStreamHelper::CreateStream(aPath, StreamMode::READ);
uno::Reference<io::XInputStream> xInputStream(new utl::OStreamWrapper(*pStream));
xml::sax::InputSource aParserInput;
aParserInput.aInputStream = xInputStream;
uno::Reference<xml::sax::XParser> xParser = xml::sax::Parser::create(comphelper::getProcessComponentContext());
rtl::Reference<SfxClassificationParser> xClassificationParser(new SfxClassificationParser());
uno::Reference<xml::sax::XDocumentHandler> xHandler(xClassificationParser.get());
xParser->setDocumentHandler(xHandler);
try
{
xParser->parseStream(aParserInput);
}
catch (const xml::sax::SAXParseException& rException)
{
SAL_WARN("sfx.view", "parsePolicy() failed: " << rException.Message);
}
m_aCategories = xClassificationParser->m_aCategories;
}
bool SfxClassificationHelper::IsClassified(SfxObjectShell& rObjectShell) bool SfxClassificationHelper::IsClassified(SfxObjectShell& rObjectShell)
{ {
uno::Reference<document::XDocumentProperties> xDocumentProperties = rObjectShell.getDocProperties(); uno::Reference<document::XDocumentProperties> xDocumentProperties = rObjectShell.getDocProperties();
...@@ -161,6 +433,8 @@ OUString SfxClassificationHelper::GetDocumentWatermark() ...@@ -161,6 +433,8 @@ OUString SfxClassificationHelper::GetDocumentWatermark()
void SfxClassificationHelper::SetBACName(const OUString& /*rName*/) void SfxClassificationHelper::SetBACName(const OUString& /*rName*/)
{ {
if (m_pImpl->m_aCategories.empty())
m_pImpl->parsePolicy();
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment