Skip to content

Commit

Permalink
Merge branch 'zip-libxml'
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Sep 18, 2024
2 parents ab3b93a + af25c46 commit 8bfd40f
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 162 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/colorer_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ jobs:
icu: [ON, OFF]
zip: [ON, OFF]
libxml: [ON, OFF]
exclude:
- libxml: ON
zip: ON

name: linux-${{ matrix.arch }}-icu_${{ matrix.icu }}-zip_${{ matrix.zip }}-libxml_${{ matrix.libxml }}

Expand Down Expand Up @@ -86,6 +83,7 @@ jobs:
arch: [ x64, x86 ]
icu: [ON, OFF]
zip: [ON, OFF]
libxml: [ON, OFF]
include:
- arch: x64
triplet: x64-windows-static-rel
Expand All @@ -95,7 +93,7 @@ jobs:
- arch: x86
zip: OFF

name: windows-${{ matrix.arch }}-icu_${{ matrix.icu }}-zip_${{ matrix.zip }}
name: windows-${{ matrix.arch }}-icu_${{ matrix.icu }}-zip_${{ matrix.zip }}-libxml_${{ matrix.libxml }}

steps:
- name: Checkout source
Expand Down Expand Up @@ -148,6 +146,7 @@ jobs:
-DVCPKG_FEATURE_FLAGS=manifests,versions
-DCOLORER_USE_ICU_STRINGS=${{ matrix.icu }}
-DCOLORER_USE_ZIPINPUTSOURCE=${{ matrix.zip }}
-DCOLORER_USE_LIBXML=${{ matrix.libxml }}
- name: Build
shell: bash
Expand All @@ -166,9 +165,6 @@ jobs:
icu: [ON, OFF]
zip: [ON, OFF]
libxml: [ON, OFF]
exclude:
- libxml: ON
zip: ON

name: macos-${{ matrix.arch }}-icu_${{ matrix.icu }}-zip_${{ matrix.zip }}-libxml_${{ matrix.libxml }}

Expand Down
24 changes: 17 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ if(COLORER_USE_LIBXML)
colorer/xml/libxml2/LibXmlReader.cpp
colorer/xml/libxml2/LibXmlReader.h
colorer/xml/libxml2/LibXmlInputSource.h
colorer/xml/libxml2/LibXmlInputSource.cpp
)
else()
set(SRC_COLORER_XML
Expand All @@ -118,14 +119,23 @@ else()
)
endif()

if(COLORER_USE_ZIPINPUTSOURCE AND NOT(COLORER_USE_LIBXML))
if(COLORER_USE_ZIPINPUTSOURCE)
set(COLORER_FEATURE_ZIPINPUTSOURCE 1)
set(SRC_COLORER_ZIP
colorer/xml/xercesc/MemoryFile.cpp
colorer/xml/xercesc/MemoryFile.h
colorer/xml/xercesc/ZipXmlInputSource.cpp
colorer/xml/xercesc/ZipXmlInputSource.h
)
if(COLORER_USE_LIBXML)
set(SRC_COLORER_ZIP
colorer/xml/libxml2/SharedXmlInputSource.cpp
colorer/xml/libxml2/SharedXmlInputSource.h
colorer/zip/MemoryFile.cpp
colorer/zip/MemoryFile.h
)
else ()
set(SRC_COLORER_ZIP
colorer/xml/xercesc/ZipXmlInputSource.cpp
colorer/xml/xercesc/ZipXmlInputSource.h
colorer/zip/MemoryFile.cpp
colorer/zip/MemoryFile.h
)
endif ()
endif()

if(COLORER_USE_ICU_STRINGS)
Expand Down
17 changes: 17 additions & 0 deletions src/colorer/utils/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,21 @@ bool Environment::isRegularFile(const UnicodeString* basePath, const UnicodeStri
return fs::is_regular_file(clear_path);
}

UnicodeString Environment::getAbsolutePath(const UnicodeString& basePath, const UnicodeString& relPath)
{
auto root_pos = basePath.lastIndexOf('/');
const auto root_pos2 = basePath.lastIndexOf('\\');
if (root_pos2 > root_pos) {
root_pos = root_pos2;
}
if (root_pos == -1) {
root_pos = 0;
}
else {
root_pos++;
}
UnicodeString newPath(basePath, 0, root_pos);
newPath.append(relPath);
return newPath;
}
} // namespace colorer
1 change: 1 addition & 0 deletions src/colorer/utils/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Environment
static std::vector<UnicodeString> getFilesFromPath(const UnicodeString* basePath, const UnicodeString* relPath,
const UnicodeString& extension);
static bool isRegularFile(const UnicodeString* basePath, const UnicodeString* relPath, UnicodeString& fullPath);
static UnicodeString getAbsolutePath(const UnicodeString& basePath, const UnicodeString& relPath);
};

} // namespace colorer
Expand Down
4 changes: 2 additions & 2 deletions src/colorer/xml/XMLNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <unordered_map>
#include "colorer/Common.h"

inline const UnicodeString empty_string("");

class XMLNode
{
public:
Expand All @@ -16,6 +14,8 @@ class XMLNode

const UnicodeString& getAttrValue(const UnicodeString& key) const
{
static const UnicodeString empty_string("");

const auto found = attributes.find(key);
if (found == attributes.end()) {
return empty_string;
Expand Down
5 changes: 3 additions & 2 deletions src/colorer/xml/XmlInputSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ XmlInputSource::~XmlInputSource()
XmlInputSource::XmlInputSource(const UnicodeString& source_path, const UnicodeString* source_base)
{
#ifdef COLORER_FEATURE_LIBXML
xml_input_source = std::make_unique<LibXmlInputSource>(&source_path, source_base);
xml_input_source = std::make_unique<LibXmlInputSource>(source_path, source_base);
#else
xercesc::XMLPlatformUtils::Initialize();
xml_input_source = XercesXmlInputSource::newInstance(&source_path, source_base);
Expand All @@ -41,7 +41,8 @@ XercesXmlInputSource* XmlInputSource::getInputSource() const

bool XmlInputSource::isFileURI(const UnicodeString& path, const UnicodeString* base)
{
if (path.startsWith(u"jar") || (base && base->startsWith(u"jar"))) {
const UnicodeString jar(u"jar:");
if (path.startsWith(jar) || (base && base->startsWith(jar))) {
return false;
}
return true;
Expand Down
73 changes: 73 additions & 0 deletions src/colorer/xml/libxml2/LibXmlInputSource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "colorer/xml/libxml2/LibXmlInputSource.h"
#include "colorer/Exception.h"
#include "colorer/utils/Environment.h"

LibXmlInputSource::LibXmlInputSource(const UnicodeString& path, const UnicodeString* base)
{
if (path.isEmpty()) {
throw InputSourceException("XmlInputSource::newInstance: path is empty");
}
UnicodeString full_path;
if (path.startsWith(jar) || (base != nullptr && base->startsWith(jar))) {
#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
initZipSource(path, base);
#else
throw InputSourceException("zip input source not supported");
#endif
}
else if (colorer::Environment::isRegularFile(base, &path, full_path)) {
sourcePath = full_path;
}
else {
throw InputSourceException(full_path + " isn't regular file.");
}
}

LibXmlInputSource::~LibXmlInputSource()
{
#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
if (zip_source) {
zip_source->delref();
}
#endif
}

LibXmlInputSource LibXmlInputSource::createRelative(const UnicodeString& relPath) const
{
return LibXmlInputSource(relPath, &sourcePath);
}

UnicodeString& LibXmlInputSource::getPath()
{
return sourcePath;
}

#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
void LibXmlInputSource::initZipSource(const UnicodeString& path, const UnicodeString* base)
{
if (path.startsWith(jar)) {
const auto path_idx = path.lastIndexOf('!');
if (path_idx == -1) {
throw InputSourceException("Bad jar uri format: " + path);
}

const UnicodeString path_to_jar = colorer::Environment::getAbsolutePath(
base ? *base : u"", UnicodeString(path, jar.length(), path_idx - jar.length()));
const UnicodeString path_in_jar(path, path_idx + 1);

sourcePath = jar + path_to_jar + u"!" + path_in_jar;
zip_source = SharedXmlInputSource::getSharedInputSource(path_to_jar);
}
else {
const auto base_idx = base->lastIndexOf('!');
if (base_idx == -1) {
throw InputSourceException("Bad jar uri format: " + path);
}
const UnicodeString path_to_jar(*base, jar.length(), base_idx - jar.length());
const UnicodeString path_in_jar = colorer::Environment::getAbsolutePath(UnicodeString(*base, base_idx + 1), path);

sourcePath = jar + path_to_jar + u"!" + path_in_jar;
zip_source = SharedXmlInputSource::getSharedInputSource(path_to_jar);
}
}
#endif
40 changes: 18 additions & 22 deletions src/colorer/xml/libxml2/LibXmlInputSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@
#define COLORER_LIBXMLINPUTSOURCE_H

#include "colorer/Common.h"
#include "colorer/Exception.h"
#include "colorer/utils/Environment.h"
#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
#include "colorer/xml/libxml2/SharedXmlInputSource.h"
#endif

static const UnicodeString jar(u"jar:");

class LibXmlInputSource
{
public:
explicit LibXmlInputSource(const UnicodeString* path, const UnicodeString* base = nullptr)
{
UnicodeString full_path;
if (colorer::Environment::isRegularFile(base, path, full_path)) {
sourcePath = full_path;
// file is not open yet, only after makeStream
}
else {
throw InputSourceException(full_path + " isn't regular file.");
}
}

LibXmlInputSource createRelative(const UnicodeString& relPath) const
{
return LibXmlInputSource(&relPath, &sourcePath);
}
explicit LibXmlInputSource(const UnicodeString& path, const UnicodeString* base = nullptr);
~LibXmlInputSource();

LibXmlInputSource createRelative(const UnicodeString& relPath) const;

[[nodiscard]]
UnicodeString& getPath()
{
return sourcePath;
}
UnicodeString& getPath();

private:
UnicodeString sourcePath;

#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
public:
void initZipSource(const UnicodeString& path, const UnicodeString* base = nullptr);

private:
SharedXmlInputSource* zip_source {nullptr};
#endif
};

#endif // COLORER_LIBXMLINPUTSOURCE_H
Loading

0 comments on commit 8bfd40f

Please sign in to comment.