Как проанализировать файл свойств Java и создать элементы, заполняющие значения каждого раздела и помещающие его в массив

у меня есть catalog.properties файл формы, как показано ниже

catalog.package.1234.asset_name=I am the Galaxians
catalog.package.1234.asset_id=1234
catalog.package.1234.version_minor=75
catalog.package.1234.version_major=1
catalog.package.1234.custom_number=27
catalog.package.1234.custom.0.name=Title
catalog.package.1234.custom.0.value=I am the Galaxians
catalog.package.1234.custom.1.name=Summary_Short
catalog.package.1234.custom.1.value=blah!! blah!! blah.. 2014
catalog.package.1234.custom.2.name=StartDate
catalog.package.1234.custom.2.value=2014-11-23 22:00:00
catalog.package.1234.custom.3.name=EndDate
catalog.package.1234.custom.3.value=2015-01-07 04:00:00
catalog.package.1234.custom.5.name=ReleaseDate
catalog.package.1234.custom.5.value=
catalog.package.1234.custom.6.name=Advisories
catalog.package.1234.custom.6.value=V
catalog.package.1234.custom.7.name=Actors
catalog.package.1234.custom.7.value=Pratt,Chris
catalog.package.1234.custom.8.name=Actors
catalog.package.1234.custom.9.value=Bautista,Dave
catalog.package.1234.custom.10.name=Genre
catalog.package.1234.custom.10.value=Movies/Action
catalog.package.1234.custom.11.name=Genre
catalog.package.1234.custom.11.value=Movies/All Movies
catalog.package.1234.asset_number=3
catalog.package.1234.asset.0.name=I am the Galaxians
catalog.package.1234.asset.0.asset_id=file://x.jpg
catalog.package.1234.asset.0.type=poster
catalog.package.1234.asset.0.version_minor=1
catalog.package.1234.asset.0.version_major=1
catalog.package.1234.asset.0.description=blah!! blah!! blah.. 2014
catalog.package.1234.asset.1.name=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.1234.asset.1.asset_id=qwerty1234
catalog.package.1234.asset.1.type=scrambled_video
catalog.package.1234.asset.1.version_minor=2
catalog.package.1234.asset.1.version_major=1
catalog.package.1234.asset.1.description=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.1234.asset.1.custom_number=7
catalog.package.1234.asset.1.custom.0.name=Audio_Type
catalog.package.1234.asset.1.custom.0.value=Dolby Digital
catalog.package.1234.asset.1.custom.1.name=HDContent
catalog.package.1234.asset.1.custom.1.value=Y
catalog.package.1234.asset.1.custom.2.name=Dubbed_Languages
catalog.package.1234.asset.1.custom.2.value=
catalog.package.1234.asset.1.ca.product_number=1
catalog.package.1234.asset.1.ca.product.0.id=4102
catalog.package.1234.asset.1.ca.product.0.rental_period=48

...

n items of the same format as shown above and below

catalog.package.****.asset_name=xyz
.
.
.
catalog.package.****.asset.1.ca.product.0.rental_period=48

...catalog.package.5678.asset_name=I am the Superman
catalog.package.5678.asset_id=5678
catalog.package.5678.version_minor=75
catalog.package.5678.version_major=1
catalog.package.5678.custom_number=27
catalog.package.5678.custom.0.name=Title
catalog.package.5678.custom.0.value=I am the Superman
catalog.package.5678.custom.1.name=Summary_Short
catalog.package.5678.custom.1.value=blah!! blah!! Clah.. 2014
catalog.package.5678.custom.2.name=StartDate
catalog.package.5678.custom.2.value=2014-11-24 22:00:00
catalog.package.5678.custom.3.name=EndDate
catalog.package.5678.custom.3.value=2015-01-08 04:00:00
catalog.package.5678.custom.5.name=ReleaseDate
catalog.package.5678.custom.5.value=
catalog.package.5678.custom.6.name=Advisories
catalog.package.5678.custom.6.value=V
catalog.package.5678.custom.7.name=Actors
catalog.package.5678.custom.7.value=Sen,Navaneeth
catalog.package.5678.custom.8.name=Actors
catalog.package.5678.custom.9.value=Bautista,Dave
catalog.package.5678.custom.10.name=Genre
catalog.package.5678.custom.10.value=Movies/Comedy
catalog.package.5678.custom.11.name=Genre
catalog.package.5678.custom.11.value=Movies/All Movies
catalog.package.5678.asset_number=3
catalog.package.5678.asset.0.name=I am the Superman
catalog.package.5678.asset.0.asset_id=file://y.jpg
catalog.package.5678.asset.0.type=poster
catalog.package.5678.asset.0.version_minor=1
catalog.package.5678.asset.0.version_major=1
catalog.package.5678.asset.0.description=blah!! blah!! Clah.. 2014
catalog.package.5678.asset.1.name=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.5678.asset.1.asset_id=qwerty5678
catalog.package.5678.asset.1.type=scrambled_video
catalog.package.5678.asset.1.version_minor=2
catalog.package.5678.asset.1.version_major=1
catalog.package.5678.asset.1.description=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.5678.asset.1.custom_number=7
catalog.package.5678.asset.1.custom.0.name=Audio_Type
catalog.package.5678.asset.1.custom.0.value=Dolby Digital
catalog.package.5678.asset.1.custom.1.name=HDContent
catalog.package.5678.asset.1.custom.1.value=Y
catalog.package.5678.asset.1.custom.2.name=Dubbed_Languages
catalog.package.5678.asset.1.custom.2.value=
catalog.package.5678.asset.1.ca.product_number=1
catalog.package.5678.asset.1.ca.product.0.id=4102
catalog.package.5678.asset.1.ca.product.0.rental_period=48

С помощью некоторого поиска в Google, я написал код, который может анализировать строку за строкой этого файла и получать мне пары ключ и значение.

Пожалуйста, найдите код ниже для этого:

<?php

$catalogFile = "catalog.properties";

$b = parse_properties($catalogFile);

file_put_contents('filename.txt', print_r($b, true));

function parse_properties($txtProperties)
{

$result = array();

$handle = fopen($txtProperties, "r");
if ($handle)
{
while (($line = fgets($handle)) !== false)
{
// process the line read.

$key = "";
$value = "";

$isWaitingOtherLine = false;
if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0 && strpos($line, "//") === 0))
{
continue;
}

if (!$isWaitingOtherLine)
{
$key = trim(substr($line, 0, strpos($line, '=')));
$value = trim(substr($line, strpos($line, '=') + 1, strlen($line)));
}
else
{
$value .= $line;
}

/* Check if ends with single '\' */
if (strrpos($value, "\\") === strlen($value) - strlen("\\"))
{
$value = substr($value, 0, strlen($value) - 1) . "\n";
$isWaitingOtherLine = true;
}
else
{
$isWaitingOtherLine = false;
}

$result[$key] = $value;
}
}
else
{
// error opening the file.
}
fclose($handle);

return $result;
}

?>

Теперь мне нужно создать объекты типа Asset при разборе файла свойств:

class Asset
{
public $title;
public $posterUrl;
public $videoUrl;
public $assetId;
public $description;
public $sDate;
public $eDate;
public $director;
public $actor;
public $genre;
public $rating;
public $channel;
public $epSeason;
public $epNumber;
public $studio;
public $fSize;
public $audioType;
public $hd;
public $sf;
public $subtitle;
public $country;
}

Мне нужно заполнить каждое значение в этом объекте класса при анализе этого.

Теперь проблемы в том,

  • как определить начало и конец каждого объекта данных
  • В случаях, как показано ниже:
catalog.package.1234.custom.0.name=Title
catalog.package.1234.custom.0.value=I am the Galaxians

Мне нужно проверить, если значение catalog.package.1234.custom.0.name «Заголовок», заполните поле «Заголовок» в объекте catalog.package.1234.custom.0.value

И так далее..

Каждый объект начинается с catalog.package.1234.asset_name и вместо «1234» это может быть любой уникальный номер.

Пожалуйста помоги мне с этим?


Как и просили, это то, что я получаю с приведенным выше PHP-кодом для чтения файла построчно

[catalog.package.1234.asset_name] => I am the Galaxians
[catalog.package.1234.asset_id] => 1234
[catalog.package.1234.version_minor] => 75
[catalog.package.1234.version_major] => 1
[catalog.package.1234.custom_number] => 27
[catalog.package.1234.custom.0.name] => Title
[catalog.package.1234.custom.0.value] => I am the Galaxians
[catalog.package.1234.custom.1.name] => Summary_Short
[catalog.package.1234.custom.1.value] => blah!! blah!! blah.. 2014
[catalog.package.1234.custom.2.name] => StartDate
[catalog.package.1234.custom.2.value] => 2014-11-23 22:00:00
[catalog.package.1234.custom.3.name] => EndDate
[catalog.package.1234.custom.3.value] => 2015-01-07 04:00:00
[catalog.package.1234.custom.5.name] => ReleaseDate
[catalog.package.1234.custom.5.value] =>
[catalog.package.1234.custom.6.name] => Advisories
[catalog.package.1234.custom.6.value] => V
[catalog.package.1234.custom.7.name] => Actors
[catalog.package.1234.custom.7.value] => Pratt,Chris
[catalog.package.1234.custom.8.name] => Actors
[catalog.package.1234.custom.9.value] => Bautista,Dave
[catalog.package.1234.custom.10.name] => Genre
[catalog.package.1234.custom.10.value] => Movies/Action
[catalog.package.1234.custom.11.name] => Genre
[catalog.package.1234.custom.11.value] => Movies/All Movies
[catalog.package.1234.asset_number] => 3
[catalog.package.1234.asset.0.name] => I am the Galaxians
[catalog.package.1234.asset.0.asset_id] => file://x.jpg
[catalog.package.1234.asset.0.type] => poster
[catalog.package.1234.asset.0.version_minor] => 1
[catalog.package.1234.asset.0.version_major] => 1
[catalog.package.1234.asset.0.description] => blah!! blah!! blah.. 2014
[catalog.package.1234.asset.1.name] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.1234.asset.1.asset_id] => qwerty1234
[catalog.package.1234.asset.1.type] => scrambled_video
[catalog.package.1234.asset.1.version_minor] => 2
[catalog.package.1234.asset.1.version_major] => 1
[catalog.package.1234.asset.1.description] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.1234.asset.1.custom_number] => 7
[catalog.package.1234.asset.1.custom.0.name] => Audio_Type
[catalog.package.1234.asset.1.custom.0.value] => Dolby Digital
[catalog.package.1234.asset.1.custom.1.name] => HDContent
[catalog.package.1234.asset.1.custom.1.value] => Y
[catalog.package.1234.asset.1.custom.2.name] => Dubbed_Languages
[catalog.package.1234.asset.1.custom.2.value] =>
[catalog.package.1234.asset.1.ca.product_number] => 1
[catalog.package.1234.asset.1.ca.product.0.id] => 4102
[catalog.package.1234.asset.1.ca.product.0.rental_period] => 48

...

n items of the same format as shown above and below

[catalog.package.****.asset_name] => xyz
.
.
.
[catalog.package.****.asset.1.ca.product.0.rental_period] => 48

...[catalog.package.5678.asset_name] => I am the Superman
[catalog.package.5678.asset_id] => 5678
[catalog.package.5678.version_minor] => 75
[catalog.package.5678.version_major] => 1
[catalog.package.5678.custom_number] => 27
[catalog.package.5678.custom.0.name] => Title
[catalog.package.5678.custom.0.value] => I am the Superman
[catalog.package.5678.custom.1.name] => Summary_Short
[catalog.package.5678.custom.1.value] => blah!! blah!! Clah.. 2014
[catalog.package.5678.custom.2.name] => StartDate
[catalog.package.5678.custom.2.value] => 2014-11-24 22:00:00
[catalog.package.5678.custom.3.name] => EndDate
[catalog.package.5678.custom.3.value] => 2015-01-08 04:00:00
[catalog.package.5678.custom.5.name] => ReleaseDate
[catalog.package.5678.custom.5.value] =>
[catalog.package.5678.custom.6.name] => Advisories
[catalog.package.5678.custom.6.value] => V
[catalog.package.5678.custom.7.name] => Actors
[catalog.package.5678.custom.7.value] => Sen,Navaneeth
[catalog.package.5678.custom.8.name] => Actors
[catalog.package.5678.custom.9.value] => Bautista,Dave
[catalog.package.5678.custom.10.name] => Genre
[catalog.package.5678.custom.10.value] => Movies/Comedy
[catalog.package.5678.custom.11.name] => Genre
[catalog.package.5678.custom.11.value] => Movies/All Movies
[catalog.package.5678.asset_number] => 3
[catalog.package.5678.asset.0.name] => I am the Superman
[catalog.package.5678.asset.0.asset_id] => file://y.jpg
[catalog.package.5678.asset.0.type] => poster
[catalog.package.5678.asset.0.version_minor] => 1
[catalog.package.5678.asset.0.version_major] => 1
[catalog.package.5678.asset.0.description] => blah!! blah!! Clah.. 2014
[catalog.package.5678.asset.1.name] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.5678.asset.1.asset_id] => qwerty5678
[catalog.package.5678.asset.1.type] => scrambled_video
[catalog.package.5678.asset.1.version_minor] => 2
[catalog.package.5678.asset.1.version_major] => 1
[catalog.package.5678.asset.1.description] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.5678.asset.1.custom_number] => 7
[catalog.package.5678.asset.1.custom.0.name] => Audio_Type
[catalog.package.5678.asset.1.custom.0.value] => Dolby Digital
[catalog.package.5678.asset.1.custom.1.name] => HDContent
[catalog.package.5678.asset.1.custom.1.value] => Y
[catalog.package.5678.asset.1.custom.2.name] => Dubbed_Languages
[catalog.package.5678.asset.1.custom.2.value] =>
[catalog.package.5678.asset.1.ca.product_number] => 1
[catalog.package.5678.asset.1.ca.product.0.id] => 4102
[catalog.package.5678.asset.1.ca.product.0.rental_period] => 48

0

Решение

Я наконец-то придумал собственное решение для этого.

<?php

require('Asset.php');

$vodAssetArray = array();

$catalogMajorVersion = "-1";
$catalogMinorVersion = "-1";
$catalogCreated = "-1";

$catalogFile = "catalog.properties";

$b = parse_properties($catalogFile);

$c = array();

foreach ($b as $key => $values)
{
echo $key . "=" . $values . "<br />";
if ($key === "catalog.package_asset_id_list")
{
$c = explode(",", $values);
}
}foreach ($c as $key_c => $values_c)
{
$vodAssetObject = new PushVodAsset();

$actorsList = array();
$directorList = array();
$genreList = array();
$advisoriesList = array();

foreach ($b as $key_b => $values_b)
{
if (strpos($key_b, $values_c) !== false)
{

if (endsWith($key_b, ".name"))
{
//                    echo $values_b . "<br />";

//                    echo $key_b . "<br />";

if ($values_b === "Title")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
$vodAssetObject->title = $b[$key_val];
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "Summary_Short")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
$vodAssetObject->description = $b[$key_val];
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "StartDate")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
$vodAssetObject->sDate = $b[$key_val];
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "EndDate")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
$vodAssetObject->eDate = $b[$key_val];
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "Director")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
array_push($directorList, $b[$key_val]);
$vodAssetObject->director = $directorList;
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "Actors")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
array_push($actorsList, $b[$key_val]);
$vodAssetObject->actor = $actorsList;
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "Genre")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
array_push($genreList, $b[$key_val]);
$vodAssetObject->genre = $genreList;
//                        echo $b[$key_val] . "<br />";
}

else if ($values_b === "Advisories")
{
$key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
array_push($advisoriesList, $b[$key_val]);
$vodAssetObject->advisories = $advisoriesList;
//                        echo $b[$key_val] . "<br />";
}
}

else if (endsWith($key_b, ".asset_id"))
{
$vodAssetObject->assetId = $values_b;
}

else if (endsWith($key_b, ".type"))
{
if ($values_b === "poster")
{
$key_val = substr_replace($key_b, ".asset_id", strpos($key_b, ".type"));
$vodAssetObject->posterUrl = $b[$key_val];
//                        echo $b[$key_val] . "<br />";
}
}
}
}

//Add this item to an ArrayList
array_push($vodAssetArray, $vodAssetObject);
}

//Put this into Tmp folder for access

function parse_properties($txtProperties)
{

$result = array();

$handle = fopen($txtProperties, "r");
if ($handle)
{
while (($line = fgets($handle)) !== false)
{
// process the line read.

$key = "";
$value = "";

$isWaitingOtherLine = false;
if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0 && strpos($line, "//") === 0))
{
continue;
}

if (!$isWaitingOtherLine)
{
$key = trim(substr($line, 0, strpos($line, '=')));
$value = trim(substr($line, strpos($line, '=') + 1, strlen($line)));
}
else
{
$value .= $line;
}

/* Check if ends with single '\' */
if (strrpos($value, "\\") === strlen($value) - strlen("\\"))
{
$value = substr($value, 0, strlen($value) - 1) . "\n";
$isWaitingOtherLine = true;
}
else
{
$isWaitingOtherLine = false;
}

$result[$key] = $value;
}
}
else
{
// error opening the file.
}
fclose($handle);

return $result;
}

function endsWith($string, $subString)
{
$strLen = strlen($string);
$subStringLen = strlen($subString);
if ($subStringLen > $strLen)
{
return false;
}
return substr_compare($string, $subString, $strLen - $subStringLen, $subStringLen) === 0;
}

?>
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]