public function parse($xml, $context)
 {
     if (empty($xml)) {
         throw new SpecParserException("Empty XML document");
     }
     // make sure we can generate a detailed error report
     libxml_use_internal_errors(true);
     if (($doc = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) == false) {
         $errors = @libxml_get_errors();
         $xmlErrors = '';
         foreach ($errors as $error) {
             $xmlErrors .= $this->displayXmlError($error);
         }
         @libxml_clear_errors();
         throw new SpecParserException("<b>Invalid XML Document</b><br/>\n" . $xmlErrors);
     }
     if (count($doc->ModulePrefs) != 1) {
         throw new SpecParserException("Missing or duplicated <ModulePrefs>");
     }
     $gadget = new Gadget($context->getGadgetId(), $context);
     // record Checksum to trace xml version
     $gadget->setChecksum($xml);
     // process ModulePref attributes
     $this->processModulePrefs($gadget, $doc->ModulePrefs);
     if (isset($doc->ModulePrefs->OAuth)) {
         // process OAuthPref attributes
         $this->processOAuthSpec($gadget, $doc->ModulePrefs->OAuth, $context);
     }
     // process UserPrefs, if any
     foreach ($doc->UserPref as $pref) {
         $this->processUserPref($gadget, $pref);
     }
     foreach ($doc->Content as $content) {
         $this->processContent($gadget, $content);
     }
     foreach ($doc->ModulePrefs->Preload as $feature) {
         $gadget->preloads[] = new Preload($feature);
     }
     foreach ($doc->ModulePrefs->Require as $feature) {
         $this->processFeature($gadget, $feature, true);
     }
     foreach ($doc->ModulePrefs->Optional as $feature) {
         $this->processFeature($gadget, $feature, false);
     }
     foreach ($doc->ModulePrefs->Icon as $icon) {
         $this->processIcon($gadget, $icon);
     }
     return $gadget;
 }
 public function parse($xml, $context)
 {
     if (empty($xml)) {
         throw new SpecParserException("Empty XML document");
     }
     // make sure we can generate a detailed error report
     libxml_use_internal_errors(true);
     if (($doc = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) == false) {
         $errors = @libxml_get_errors();
         $xmlErrors = '';
         foreach ($errors as $error) {
             $xmlErrors .= $this->displayXmlError($error);
         }
         @libxml_clear_errors();
         throw new SpecParserException("<b>Invalid XML Document</b><br/>\n" . $xmlErrors);
     }
     if (count($doc->ModulePrefs) != 1) {
         throw new SpecParserException("Missing or duplicated <ModulePrefs>");
     }
     $gadget = new Gadget($context->getGadgetId(), $context);
     // record Checksum to trace xml version
     $gadget->setChecksum($xml);
     // process ModulePref attributes
     $this->processModulePrefs($gadget, $doc->ModulePrefs);
     if (isset($doc->ModulePrefs->OAuth)) {
         // process OAuthPref attributes
         $this->processOAuthSpec($gadget, $doc->ModulePrefs->OAuth, $context);
     }
     // process UserPrefs, if any
     foreach ($doc->UserPref as $pref) {
         $this->processUserPref($gadget, $pref);
     }
     // Assume gadget v1
     $explicit_profile = false;
     foreach ($doc->Content as $content) {
         $attributes = $content->attributes();
         if (empty($attributes['type'])) {
             throw new SpecParserException("No content type specified!");
         }
         $view = isset($attributes['view']) ? trim($attributes['view']) : '';
         // Note if we find a profile explicity defined
         if (strpos($view, "profile") !== false) {
             $explicit_profile = true;
         }
     }
     foreach ($doc->Content as $content) {
         $attributes = $content->attributes();
         if (empty($attributes['type'])) {
             throw new SpecParserException("No content type specified!");
         }
         $view = isset($attributes['view']) ? trim($attributes['view']) : '';
         // If view isnt defined and we didnt find a profile explicity defined
         if ($view == '') {
             if (!$explicit_profile) {
                 $this->processContent($gadget, $content, array(0 => DEFAULT_VIEW));
             }
             // If view isnt defined and a profile was found, this will catch it
         } else {
             $views = explode(',', $view);
             $this->processContent($gadget, $content, $views);
         }
     }
     foreach ($doc->ModulePrefs->Preload as $feature) {
         $gadget->preloads[] = new Preload($feature);
     }
     foreach ($doc->ModulePrefs->Require as $feature) {
         $this->processFeature($gadget, $feature, true);
     }
     foreach ($doc->ModulePrefs->Optional as $feature) {
         $this->processFeature($gadget, $feature, false);
     }
     foreach ($doc->ModulePrefs->Icon as $icon) {
         $this->processIcon($gadget, $icon);
     }
     return $gadget;
 }