public List<ICTomorrowItemMetadata> getMetadataFile(int jobId) throws ICTomorrowApiException {
    try {
      Element resultElement = getResultElement(client.get(getMetadataFileUrl(jobId)));

      String jobStatus = resultElement.getFirstChildElement("job_status").getValue().trim();

      if ("COMPLETE".equals(jobStatus)) {
        Element downloadElement = resultElement.getFirstChildElement("Download");

        List<ICTomorrowItemMetadata> items = Lists.newArrayList();
        Elements itemElements =
            downloadElement.getFirstChildElement("Items").getChildElements("Item");
        for (int i = 0; i < itemElements.size(); i++) {
          items.add(getItem(itemElements.get(i)));
        }

        return items;
      } else if ("FAILED".equals(jobStatus)) {
        throw new ICTomorrowApiException("Metadata file creation failed");
      }

      return null;
    } catch (HttpException e) {
      throw new ICTomorrowApiException("Exception while recording consumer activity", e);
    }
  }
  public ICTomorrowIncrementMeterResponse incrementMeter(
      int consumerId, int offerId, Integer incrementValue, String freeText)
      throws ICTomorrowApiException {
    try {
      FormEncodedPayload data =
          new FormEncodedPayload()
              .withField("update_type", Integer.toString(METER_OPERATION_INCREMENT));
      addOptionalParameter(data, "increment_value", incrementValue);
      addOptionalParameter(data, "Free_text", freeText);
      HttpResponse res = client.put(meterUrl(consumerId, offerId), data);

      Element resultElement = getResultElement(res);

      Element meterElement = resultElement.getFirstChildElement("meter");

      Boolean incrementGranted =
          Boolean.valueOf(meterElement.getFirstChildElement("granted").getValue().trim());
      Boolean notificationLimitReached =
          Boolean.valueOf(
              meterElement.getFirstChildElement("notificationLimitReached").getValue().trim());
      Boolean maximumLimitReached =
          Boolean.valueOf(
              meterElement.getFirstChildElement("maximumLimitReached").getValue().trim());
      // ICTomorrowMeter meter = parseMeter(resultElement);

      return new ICTomorrowIncrementMeterResponse(
          incrementGranted, notificationLimitReached, maximumLimitReached);
    } catch (HttpException e) {
      throw new ICTomorrowApiException("Exception while incrementing meter", e);
    }
  }
  /**
   * Read urn.
   *
   * @param file the file
   * @return The URN specified in the METS file or null if the METS file doesn't specify an URN
   * @throws IOException Signals that an I/O exception has occurred.
   * @throws ParseException the parse exception
   * @author Thomas Kleinke
   */
  public String readURN(File file) throws IOException, ParseException {

    FileInputStream fileInputStream = new FileInputStream(file);
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);

    XMLReader xmlReader = null;
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
      xmlReader = spf.newSAXParser().getXMLReader();
    } catch (Exception e) {
      fileInputStream.close();
      bomInputStream.close();
      throw new IOException("Error creating SAX parser", e);
    }
    xmlReader.setErrorHandler(err);
    NodeFactory nodeFactory = new PremisXmlReaderNodeFactory();
    Builder parser = new Builder(xmlReader, false, nodeFactory);
    logger.trace("Successfully built builder and XML reader");

    try {
      String urn = null;

      Document doc = parser.build(bomInputStream);
      Element root = doc.getRootElement();

      Element dmdSecEl = root.getFirstChildElement("dmdSec", METS_NS);
      if (dmdSecEl == null) return null;

      Element mdWrapEl = dmdSecEl.getFirstChildElement("mdWrap", METS_NS);
      if (mdWrapEl == null) return null;

      Element xmlDataEl = mdWrapEl.getFirstChildElement("xmlData", METS_NS);
      if (xmlDataEl == null) return null;

      Element modsEl = xmlDataEl.getFirstChildElement("mods", MODS_NS);
      if (modsEl == null) return null;

      Elements identifierEls = modsEl.getChildElements("identifier", MODS_NS);
      for (int i = 0; i < identifierEls.size(); i++) {
        Element element = identifierEls.get(i);
        Attribute attribute = element.getAttribute("type");
        if (attribute.getValue().toLowerCase().equals("urn")) urn = element.getValue();
      }

      if (urn != null && urn.equals("")) urn = null;

      return urn;
    } catch (ValidityException ve) {
      throw new IOException(ve);
    } catch (ParsingException pe) {
      throw new IOException(pe);
    } catch (IOException ie) {
      throw new IOException(ie);
    } finally {
      fileInputStream.close();
      bomInputStream.close();
    }
  }
Ejemplo n.º 4
0
  private static boolean isLive(String aServer) {
    try {
      URL url = new URL(aServer + "health");
      HttpURLConnection uc = (HttpURLConnection) url.openConnection();

      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(BUNDLE.get("TC_STATUS_CHECK"), url);
      }

      if (uc.getResponseCode() == 200) {
        Document xml = new Builder().build(uc.getInputStream());
        Element response = (Element) xml.getRootElement();
        Element health = response.getFirstChildElement("health");
        String status = health.getValue();

        if (status.equals("dying") || status.equals("sick")) {
          if (LOGGER.isWarnEnabled()) {
            LOGGER.warn(BUNDLE.get("TC_SERVER_STATUS"), status);
          }

          return true;
        } else if (status.equals("ok")) {
          return true;
        } else {
          LOGGER.error(BUNDLE.get("TC_UNEXPECTED_STATUS"), status);
        }
      }
    } catch (UnknownHostException details) {
      LOGGER.error(BUNDLE.get("TC_UNKNOWN_HOST"), details.getMessage());
    } catch (Exception details) {
      LOGGER.error(details.getMessage());
    }

    return false;
  }
Ejemplo n.º 5
0
  private static void cacheImage(String aServer, String aID) {
    try {
      String id = URLEncoder.encode(aID, "UTF-8");
      String baseURL = aServer + "view/image/" + id;
      URL url = new URL(baseURL + "/info.xml");
      HttpURLConnection uc = (HttpURLConnection) url.openConnection();
      int status = uc.getResponseCode();

      if (status == 200) {
        Document xml = new Builder().build(uc.getInputStream());
        Element info = (Element) xml.getRootElement();
        Element elem = info.getFirstChildElement("identifier", IIIF_NS);
        Element hElem = info.getFirstChildElement("height", IIIF_NS);
        Element wElem = info.getFirstChildElement("width", IIIF_NS);
        String idValue = elem.getValue();

        try {
          int height = Integer.parseInt(hElem.getValue());
          int width = Integer.parseInt(wElem.getValue());
          Iterator<String> tileIterator;
          List<String> tiles;

          if (idValue.equals(aID) && height > 0 && width > 0) {
            if (idValue.startsWith("/") && LOGGER.isWarnEnabled()) {
              LOGGER.warn(BUNDLE.get("TC_SLASH_ID"), aID);
            }

            tiles = CacheUtils.getCachingQueries(height, width);
            tileIterator = tiles.iterator();

            while (tileIterator.hasNext()) {
              cacheTile(baseURL + tileIterator.next());
            }
          } else if (LOGGER.isErrorEnabled()) {
            LOGGER.error(BUNDLE.get("TC_ID_404"), aID);
          }
        } catch (NumberFormatException nfe) {
          LOGGER.error(BUNDLE.get("TC_INVALID_DIMS"), aID);
        }
      } else {
        LOGGER.error(BUNDLE.get("TC_SERVER_STATUS_CODE"), status);
      }
    } catch (Exception details) {
      LOGGER.error(details.getMessage());
    }
  }
  public ICTomorrowMeter getMeter(int consumerId, int offerId) throws ICTomorrowApiException {
    try {
      HttpResponse res = client.get(meterUrl(consumerId, offerId));

      Element resultElement = getResultElement(res);

      return parseMeter(resultElement.getFirstChildElement("meter"));
    } catch (HttpException e) {
      throw new ICTomorrowApiException("Exception while getting meter", e);
    }
  }
  private ICTomorrowItemMetadata getItem(Element itemElement) {
    Integer contentHandle = Integer.valueOf(itemElement.getAttributeValue("ContentHandle"));
    String contentKey = itemElement.getFirstChildElement("Key").getValue().trim();

    ICTomorrowItemMetadata item = new ICTomorrowItemMetadata(contentHandle, contentKey);

    item.setChannelTitle(getCharacteristic(itemElement, "Channel-Title"));
    String link = getCharacteristic(itemElement, "Link");
    if (link == null) {
      link = getCharacteristic(itemElement, "URL Location");
    }
    item.setLink(link);
    item.setSource(getCharacteristic(itemElement, "Source"));
    item.setWebmaster(getCharacteristic(itemElement, "WebMaster"));
    item.setTitle(getOptionalValue(itemElement, "Title"));
    item.setContentProvider(getOptionalValue(itemElement, "ContentProvider"));
    item.setLicenceTemplateName(getOptionalValue(itemElement, "LicenseTemplateName"));

    String keywords = getCharacteristic(itemElement, "Keywords");
    if (keywords != null) {
      item.setKeywords(
          ImmutableSet.copyOf(Splitter.on(",").trimResults().omitEmptyStrings().split(keywords)));
    }

    String dateString = getCharacteristic(itemElement, "Pub-date");
    if (dateString != null) {
      DateTimeFormatter dateParser =
          new DateTimeFormatterBuilder()
              .appendDayOfWeekShortText()
              .appendLiteral(", ")
              .appendDayOfMonth(2)
              .appendLiteral(" ")
              .appendMonthOfYearShortText()
              .appendLiteral(" ")
              .appendYear(4, 4)
              .appendLiteral(" ")
              .appendHourOfDay(2)
              .appendLiteral(":")
              .appendMinuteOfHour(2)
              .appendLiteral(":")
              .appendSecondOfMinute(2)
              .appendLiteral(" GMT")
              .toFormatter();
      // DateTimeFormat.forPattern("E, dd MM yyyy HH:mm:ss z");

      DateTime date = dateParser.parseDateTime(dateString);

      item.setPublishedDate(date);
    }

    return item;
  }
Ejemplo n.º 8
0
  public void beforeParsing(Document document) {
    nu.xom.Element html = document.getRootElement();
    nu.xom.Element head = html.getFirstChildElement("head");
    Check.notNull(head, "<head> section is missing from document");
    script = new nu.xom.Element("script");
    script.addAttribute(new Attribute("type", "text/javascript"));

    // Fix for Issue #26: Strict XHTML DTD requires an explicit end tag for <script> element
    // Thanks to Matthias Schwegler for reporting and supplying a fix for this.
    script.appendChild("");

    head.appendChild(script);
  }
  public int checkAccessToken(String code, String callbackUrl) throws ICTomorrowApiException {
    HttpResponse res;
    try {
      FormEncodedPayload data =
          new FormEncodedPayload()
              .withField("client_id", applicationKey)
              .withField("redirect_uri", callbackUrl)
              .withField("client_secret", clientSecret)
              .withField("code", code);
      res = client.post(TOKEN_URL, data);

      Element resultElement = getResultElement(res);
      return Integer.valueOf(resultElement.getFirstChildElement("access_token").getValue().trim());
    } catch (HttpException e) {
      throw new ICTomorrowAuthException("Exception while fetching consumerId", e);
    }
  }
  public int recordConsumerActivity(
      int consumerId,
      int offerId,
      int trialId,
      String activityType,
      DateTime transactionDate,
      String contentKey,
      String contentHandle,
      Integer incrementValue,
      Integer integerValue,
      String freeText)
      throws ICTomorrowApiException {
    try {
      String transactionDateString = transactionDate.toString("yyyy-MM-dd'T'HH:mm:ssZZ");

      FormEncodedPayload data =
          new FormEncodedPayload()
              .withField("consumer_id", Integer.toString(consumerId))
              .withField("offer_id", Integer.toString(offerId))
              .withField("application_key", applicationKey)
              .withField("trial_id", Integer.toString(trialId))
              .withField("activity_type", activityType)
              .withField("transaction_date", transactionDateString);

      addOptionalParameter(data, "content_handle", contentHandle);
      addOptionalParameter(data, "content_key", contentKey);
      addOptionalParameter(data, "increment_value", incrementValue);
      addOptionalParameter(data, "integer_value", integerValue);
      addOptionalParameter(data, "free_text", freeText);
      HttpResponse res = client.post(RECORD_CONSUMER_ACTIVITY_URL, data);
      Element resultElement = getResultElement(res);
      return Integer.valueOf(
          resultElement
              .getFirstChildElement("transaction")
              .getFirstChildElement("TransactionID")
              .getValue()
              .trim());
    } catch (HttpException e) {
      throw new ICTomorrowApiException("Exception while recording consumer activity", e);
    }
  }
Ejemplo n.º 11
0
  public static void doStatisticsForXMLFile(String filePath) {
    try {
      Builder builder = new Builder(false);
      Document doc = builder.build(filePath);
      Element corpus = doc.getRootElement();
      Elements lexeltElements = corpus.getChildElements("lexelt");

      Map<String, Integer> posMap = new HashMap<String, Integer>();

      for (int k = 0; k < lexeltElements.size(); k++) {
        Element lexeltElement = lexeltElements.get(k);
        Elements instanceElements = lexeltElement.getChildElements("instance");
        for (int i = 0; i < instanceElements.size(); i++) {
          Element instanceElement = instanceElements.get(i);
          Element postaggingElement = instanceElement.getFirstChildElement("postagging");
          Elements wordElements = postaggingElement.getChildElements("word");
          for (int j = 0; j < wordElements.size(); j++) {
            Element wordElement = wordElements.get(j);
            Elements subwordElements = wordElement.getChildElements("subword");
            if (subwordElements.size() == 0) {
              String pos = wordElement.getAttributeValue("pos");
              doIncrement(posMap, pos);
            } else {
              for (int m = 0; m < subwordElements.size(); m++) {
                Element subwordElement = subwordElements.get(m);
                String pos = subwordElement.getAttributeValue("pos");
                doIncrement(posMap, pos);
              }
            }
          }
        }
      }
      doStatistics(filePath, posMap);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public TestCaseTemplateParameter(String xmlData) throws Exception {

    Document doc = new Builder().build(xmlData, null);
    Element root = doc.getRootElement();

    Elements importElements = root.getChildElements("import");
    for (int i = 0; i < importElements.size(); i++) {
      Element importElement = importElements.get(i);
      this.addImport(importElement.getValue());
    }

    Elements classToMockElements = root.getChildElements("classToMock");
    for (int i = 0; i < classToMockElements.size(); i++) {
      Element classToMockElement = classToMockElements.get(i);
      Element classNameElement = classToMockElement.getFirstChildElement("className");
      Element instanceNameElement = classToMockElement.getFirstChildElement("instanceName");
      this.addClassToMockInstanceName(classNameElement.getValue(), instanceNameElement.getValue());
      Elements invokeElements = classToMockElement.getChildElements("invoke");
      ArrayList<String> invokeList = new ArrayList<String>();
      for (int j = 0; j < invokeElements.size(); j++) {
        Element invokeElement = invokeElements.get(j);
        invokeList.add(invokeElement.getValue());
      }
      this.addJMockInvokeSequence(classNameElement.getValue(), invokeList.toArray(new String[0]));
    }

    this.setPackageName(root.getFirstChildElement("packageName").getValue());
    this.setClassUnderTest(root.getFirstChildElement("classUnderTest").getValue());

    Elements constructorArguments = root.getChildElements("constructorArgument");
    for (int i = 0; i < constructorArguments.size(); i++) {
      Element constructorArgumentElement = constructorArguments.get(i);
      String type = constructorArgumentElement.getFirstChildElement("type").getValue();
      String value = constructorArgumentElement.getFirstChildElement("value").getValue();
      this.addConstructorArgument(type, value);
    }

    this.setMethodUnderTest(root.getFirstChildElement("methodUnderTest").getValue());

    Elements methodParameters = root.getChildElements("methodParameter");
    for (int i = 0; i < methodParameters.size(); i++) {
      Element methodParameterElement = methodParameters.get(i);
      String type = methodParameterElement.getFirstChildElement("type").getValue();
      String name = methodParameterElement.getFirstChildElement("name").getValue();
      this.addMethodParameter(type, name);
    }

    this.setStaticMethod(
        Boolean.parseBoolean(root.getFirstChildElement("isStaticMethod").getValue()));

    if (root.getFirstChildElement("singletonMethod") != null) {
      this.setSingletonMethod(root.getFirstChildElement("singletonMethod").getValue());
    }

    if (root.getFirstChildElement("checkStateMethod") != null) {
      this.setCheckStateMethod(root.getFirstChildElement("checkStateMethod").getValue());
    }

    this.setReturnType(root.getFirstChildElement("returnType").getValue());

    if (root.getFirstChildElement("delta") != null) {
      this.setDelta(Double.parseDouble(root.getFirstChildElement("delta").getValue()));
    }
  }
Ejemplo n.º 13
0
 private String itemId(Element epElem) {
   return epElem.getFirstChildElement("ItemId", LAKEVIEW.getUri()).getValue();
 }
Ejemplo n.º 14
0
 private static List<CoreMap> toTimexCoreMaps(Element docElem, CoreMap originalDocument) {
   // --Collect Token Offsets
   HashMap<Integer, Integer> beginMap = new HashMap<Integer, Integer>();
   HashMap<Integer, Integer> endMap = new HashMap<Integer, Integer>();
   boolean haveTokenOffsets = true;
   for (CoreMap sent : originalDocument.get(CoreAnnotations.SentencesAnnotation.class)) {
     for (CoreLabel token : sent.get(CoreAnnotations.TokensAnnotation.class)) {
       Integer tokBegin = token.get(CoreAnnotations.TokenBeginAnnotation.class);
       Integer tokEnd = token.get(CoreAnnotations.TokenEndAnnotation.class);
       if (tokBegin == null || tokEnd == null) {
         haveTokenOffsets = false;
       }
       int charBegin = token.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
       int charEnd = token.get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
       beginMap.put(charBegin, tokBegin);
       endMap.put(charEnd, tokEnd);
     }
   }
   // --Set Timexes
   List<CoreMap> timexMaps = new ArrayList<CoreMap>();
   int offset = 0;
   Element textElem = docElem.getFirstChildElement("text");
   for (int i = 0; i < textElem.getChildCount(); i++) {
     Node content = textElem.getChild(i);
     if (content instanceof Text) {
       Text text = (Text) content;
       offset += text.getValue().length();
     } else if (content instanceof Element) {
       Element child = (Element) content;
       if (child.getLocalName().equals("TIMEX3")) {
         Timex timex = new Timex(child);
         if (child.getChildCount() != 1) {
           throw new RuntimeException("TIMEX3 should only contain text " + child);
         }
         String timexText = child.getValue();
         CoreMap timexMap = new ArrayCoreMap();
         // (timex)
         timexMap.set(TimexAnnotation.class, timex);
         // (text)
         timexMap.set(CoreAnnotations.TextAnnotation.class, timexText);
         // (characters)
         int charBegin = offset;
         timexMap.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, charBegin);
         offset += timexText.length();
         int charEnd = offset;
         timexMap.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, charEnd);
         // (tokens)
         if (haveTokenOffsets) {
           Integer tokBegin = beginMap.get(charBegin);
           int searchStep = 1; // if no exact match, search around the character offset
           while (tokBegin == null) {
             tokBegin = beginMap.get(charBegin - searchStep);
             if (tokBegin == null) {
               tokBegin = beginMap.get(charBegin + searchStep);
             }
             searchStep += 1;
           }
           searchStep = 1;
           Integer tokEnd = endMap.get(charEnd);
           while (tokEnd == null) {
             tokEnd = endMap.get(charEnd - searchStep);
             if (tokEnd == null) {
               tokEnd = endMap.get(charEnd + searchStep);
             }
             searchStep += 1;
           }
           timexMap.set(CoreAnnotations.TokenBeginAnnotation.class, tokBegin);
           timexMap.set(CoreAnnotations.TokenEndAnnotation.class, tokEnd);
         }
         // (add)
         timexMaps.add(timexMap);
       } else {
         throw new RuntimeException("unexpected element " + child);
       }
     } else {
       throw new RuntimeException("unexpected content " + content);
     }
   }
   return timexMaps;
 }
 private String getOptionalValue(Element element, String attributeName) {
   if (element.getFirstChildElement(attributeName) != null) {
     return element.getFirstChildElement(attributeName).getValue().trim();
   }
   return null;
 }
 private Integer getOptionalIntegerValue(Element element, String attributeName) {
   if (element.getFirstChildElement(attributeName) != null) {
     return Integer.valueOf(element.getFirstChildElement(attributeName).getValue().trim());
   }
   return null;
 }