You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
990 B
46 lines
990 B
6 years ago
|
# Pleroma: A lightweight social networking server
|
||
5 years ago
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||
6 years ago
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
||
8 years ago
|
defmodule Pleroma.Web.XML do
|
||
7 years ago
|
require Logger
|
||
|
|
||
7 years ago
|
def string_from_xpath(_, :error), do: nil
|
||
7 years ago
|
|
||
8 years ago
|
def string_from_xpath(xpath, doc) do
|
||
7 years ago
|
try do
|
||
|
{:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
|
||
8 years ago
|
|
||
7 years ago
|
res =
|
||
|
res
|
||
|
|> to_string
|
||
|
|> String.trim()
|
||
8 years ago
|
|
||
7 years ago
|
if res == "", do: nil, else: res
|
||
|
catch
|
||
6 years ago
|
_e ->
|
||
7 years ago
|
Logger.debug("Couldn't find xpath #{xpath} in XML doc")
|
||
|
nil
|
||
|
end
|
||
8 years ago
|
end
|
||
|
|
||
|
def parse_document(text) do
|
||
7 years ago
|
try do
|
||
7 years ago
|
{doc, _rest} =
|
||
|
text
|
||
|
|> :binary.bin_to_list()
|
||
6 years ago
|
|> :xmerl_scan.string(quiet: true)
|
||
8 years ago
|
|
||
7 years ago
|
doc
|
||
6 years ago
|
rescue
|
||
|
_e ->
|
||
7 years ago
|
Logger.debug("Couldn't parse XML: #{inspect(text)}")
|
||
7 years ago
|
:error
|
||
6 years ago
|
catch
|
||
|
:exit, _error ->
|
||
6 years ago
|
Logger.debug("Couldn't parse XML: #{inspect(text)}")
|
||
|
:error
|
||
7 years ago
|
end
|
||
8 years ago
|
end
|
||
|
end
|