00001 <?php
00008 abstract class SXMLHelper extends SObject {
00018 public static function parse($xml, $intoDOM = false) {
00019 $source = utf8_encode($xml);
00020 $source = preg_replace('/&(?!amp|(g|l)t)([^;]+);/', '<entity>$2</entity>', $source);
00021 $source = str_replace('&', '&', $source);
00022 if($intoDOM) {
00023 $doc = new domDocument();
00024 if(!$doc->loadXML($source))
00025 return false;
00026 else
00027 return $doc;
00028 }
00029 else {
00030 $old_error_handler = set_error_handler(array('SXMLHelper', 'XMLParseErrorHandler2'));
00031 $XMLObj = simplexml_load_string($source);
00032 restore_error_handler();
00033 if($XMLObj === false) {
00034 self::setStaticError('XML Parse Error. Please validate your source XML.');
00035 return false;
00036 }
00037 return $XMLObj;
00038 }
00039 }
00040
00048 protected static function XMLParseErrorHandler2($errno, $errstr, $errfile, $errline) {
00049 switch ($errno) {
00050 case 2:
00051 $errMsg = preg_replace('/\s/', ' ', $errstr);
00052 self::setStaticError($errMsg);
00053 break;
00054 default:
00055 break;
00056 }
00057 }
00058
00076 public static function validateXML($xml, $useSchema = true) {
00077 if($xml == "")
00078 return true;
00079
00080 $old_internal = libxml_use_internal_errors(true);
00081 libxml_clear_errors();
00082 $xml = utf8_encode($xml);
00083 $xml = preg_replace('/&(?!amp|(g|l)t)([^;]+);/', '<entity>$2</entity>', $xml);
00084 $dom = new DOMDocument('1.0', 'utf-8');
00085 $dom->loadXML($xml);
00086 if($useSchema) {
00087 $schema = file_get_contents('/var/www/html/XML/StdXML/3.0/schema.xsd');
00088 $dom->schemaValidateSource($schema);
00089 }
00090
00091 $errors = libxml_get_errors();
00092 libxml_clear_errors();
00093 libxml_use_internal_errors($old_internal);
00094
00095 if(count($errors) == 0)
00096 return true;
00097
00098 $ret = array();
00099
00100 foreach($errors as $err) {
00101 switch($err->level) {
00102 case LIBXML_ERR_WARNING:
00103 $what = "Warning";
00104 break;
00105 case LIBXML_ERR_ERROR:
00106 $what = "Error";
00107 break;
00108 case LIBXML_ERR_FATAL:
00109 $what = "Fatal error";
00110 break;
00111 }
00112 array_push($ret, array('type' => $what, 'code' => $err->code, 'line' => $err->line, 'message' => $err->message));
00113 }
00114
00115 return $ret;
00116 }
00117
00124 public static function formatXML($xml, $highlight = false, $lineNumbers = false) {
00125 return XmlFormatter::format($xml, $highlight, $lineNumbers);
00126 }
00127
00128 public static function formatXMLSource($xml) {
00129 return XmlFormatter::formatXMLSource($xml);
00130 }
00131
00132 public static function formatErrors($errors, $asList = false) {
00133 $out = array();
00134 foreach($errors as $err)
00135 $out[] = "<b>$err[type]</b> ($err[code]) on line <b>$err[line]</b>: $err[message]";
00136 if($asList)
00137 return '<ul><li>' . implode('</li><li>', $out) . '</li></ul>';
00138 else
00139 return $out;
00140 }
00141
00149 public static function contentToXML($input) {
00150
00151
00152
00153 $input = str_replace('&', '&', $input);
00154 $input = str_replace('<', '<', $input);
00155 $input = str_replace('>', '>', $input);
00156 $input = str_replace('"', '"', $input);
00157 $input = str_replace('\'', ''', $input);
00158 return $input;
00159 }
00160
00168 public static function contentFromXML($input) {
00169
00170
00171 $input = str_replace('<', '<', $input);
00172 $input = str_replace('>', '>', $input);
00173 $input = str_replace('"', '"', $input);
00174 $input = str_replace(''', '\'', $input);
00175 $input = str_replace('&', '&', $input);
00176 return $input;
00177 }
00178
00179
00180 }
00181 ?>