Exemplo n.º 1
0
  public void load() throws IOException, SQLException {
    conn = Mysql.getConn("semsearch");
    List<String> lines = FileUtils.readLines(new File(this.filePath));
    AANPaper paper = new AANPaper();
    for (int i = 0; i < lines.size(); i++) {
      String line = lines.get(i).trim();
      if (line.equals("")) {
        process(paper);
      }
      if (line.startsWith("id")) {
        paper = new AANPaper();
        String temp = StringUtils.substringAfter(line, "{").trim();
        temp = StringUtils.substringBeforeLast(temp, "}");
        paper.setId(temp);
      } else if (line.startsWith("author")) {
        String temp = StringUtils.substringAfter(line, "{").trim();
        temp = StringUtils.substringBeforeLast(temp, "}");
        paper.setAuthor(temp);
      } else if (line.startsWith("title")) {
        String temp = StringUtils.substringAfter(line, "{").trim();
        temp = StringUtils.substringBeforeLast(temp, "}");
        paper.setTitle(temp);
      } else if (line.startsWith("venue")) {
        String temp = StringUtils.substringAfter(line, "{").trim();
        temp = StringUtils.substringBeforeLast(temp, "}");
        paper.setVenue(temp);
      } else if (line.startsWith("year")) {
        String temp = StringUtils.substringAfter(line, "{").trim();
        temp = StringUtils.substringBeforeLast(temp, "}");
        paper.setYear(temp);
      }

      System.out.println(line);
    }
  }
  /**
   * @param info
   * @return
   */
  public static Set<String> getLocations(WuliuInfo info) {
    Set<String> locations = new LinkedHashSet<String>();

    for (TrackInfo trackInfo : info.getTrackInfoList()) {
      String context = trackInfo.getContext();
      if (StringUtils.isNotBlank(context)) {
        context = StringUtils.substringBeforeLast(context, " ");
        context = StringUtils.substringBeforeLast(context, "分拨中心");
        locations.add(context);
      }
    }
    return locations;
  }
Exemplo n.º 3
0
  /**
   * easyui 运行中流程列表数据
   *
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "runningProcessDataGrid")
  public void runningProcessDataGrid(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    /*
     * List<HistoricProcessInstance> historicProcessInstances =
     * historyService .createHistoricProcessInstanceQuery()
     * .unfinished().list();
     */
    ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
    List<ProcessInstance> list = processInstanceQuery.list();

    StringBuffer rows = new StringBuffer();
    for (ProcessInstance hi : list) {
      rows.append(
          "{'id':"
              + hi.getId()
              + ",'processDefinitionId':'"
              + hi.getProcessDefinitionId()
              + "','processInstanceId':'"
              + hi.getProcessInstanceId()
              + "','activityId':'"
              + hi.getActivityId()
              + "'},");
    }

    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject =
        JSONObject.fromObject("{'total':" + list.size() + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
Exemplo n.º 4
0
 private void setTitle() {
   if (StringUtils.isNotBlank(version)) {
     title = StringUtils.substringBeforeLast(version, " ");
   } else {
     title = "DDF";
   }
 }
 /**
  * Computes the best file to save the response to the given URL.
  *
  * @param url the requested URL
  * @param extension the preferred extension
  * @return the file to create
  * @throws IOException if a problem occurs creating the file
  */
 private File createFile(final URL url, final String extension) throws IOException {
   String name = url.getPath().replaceFirst("/$", "").replaceAll(".*/", "");
   name = StringUtils.substringBefore(name, "?"); // remove query
   name = StringUtils.substringBefore(name, ";"); // remove additional info
   name = StringUtils.substring(name, 0, 30); // avoid exceptions due to too long file names
   if (!name.endsWith(extension)) {
     name += extension;
   }
   int counter = 0;
   while (true) {
     final String fileName;
     if (counter != 0) {
       fileName =
           StringUtils.substringBeforeLast(name, ".")
               + "_"
               + counter
               + "."
               + StringUtils.substringAfterLast(name, ".");
     } else {
       fileName = name;
     }
     final File f = new File(reportFolder_, fileName);
     if (f.createNewFile()) {
       return f;
     }
     counter++;
   }
 }
Exemplo n.º 6
0
  /**
   * Returns the user that has an OpenID whose real ID is equal to the specified OpenID.
   *
   * @throws UserNotFoundException when the user could not be found
   */
  public User getUserByOpenId(String openId) throws IOException {
    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);
      File workingDir = RepositoryUtil.getWorkingDir(repo.r());
      FileFilter filter =
          new FileFilter() {
            @Override
            public boolean accept(File file) {
              return file.isFile() && file.getName().endsWith(USER_SUFFIX);
            }
          };
      for (File file : workingDir.listFiles(filter)) {
        String loginName = StringUtils.substringBeforeLast(file.getName(), USER_SUFFIX);
        String json = FileUtils.readFileToString(file, Charsets.UTF_8);
        User user = getUser(loginName, json);
        for (OpenId id : user.getOpenIds()) {
          if (id.getRealId().equals(openId)) {
            return user;
          }
        }
      }

      throw new OpenIdNotFoundException(openId);
    } finally {
      Util.closeQuietly(repo);
    }
  }
Exemplo n.º 7
0
  /**
   * easyui AJAX请求数据 已办任务
   *
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "finishedTaskDataGrid")
  public void finishedTask(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    // String userId = "leaderuser";
    String userId = ResourceUtil.getSessionUserName().getId();
    List<HistoricTaskInstance> historicTasks =
        historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).finished().list();

    StringBuffer rows = new StringBuffer();
    for (HistoricTaskInstance t : historicTasks) {
      rows.append(
          "{'name':'"
              + t.getName()
              + "','description':'"
              + t.getDescription()
              + "','taskId':'"
              + t.getId()
              + "','processDefinitionId':'"
              + t.getProcessDefinitionId()
              + "','processInstanceId':'"
              + t.getProcessInstanceId()
              + "'},");
    }
    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject =
        JSONObject.fromObject("{'total':" + historicTasks.size() + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
Exemplo n.º 8
0
  /**
   * easyui AJAX请求数据 待领任务
   *
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "waitingClaimTaskDataGrid")
  public void waitingClaimTaskDataGrid(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    // String userId = "hruser";
    String userId = ResourceUtil.getSessionUserName().getId();
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks =
        taskService
            .createTaskQuery()
            .taskCandidateUser(userId)
            .active()
            .list(); // .taskCandidateGroup("hr").active().list();

    StringBuffer rows = new StringBuffer();
    for (Task t : tasks) {
      rows.append(
          "{'name':'"
              + t.getName()
              + "','taskId':'"
              + t.getId()
              + "','processDefinitionId':'"
              + t.getProcessDefinitionId()
              + "'},");
    }
    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject =
        JSONObject.fromObject("{'total':" + tasks.size() + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
Exemplo n.º 9
0
 /**
  * Extracts the geneID out of the splitted file line.
  *
  * @param splittedLine
  * @return
  */
 private Gene getGeneId(String[] splittedLine) {
   String[] geneSplittedStr = StringUtils.split(splittedLine[3], FileUtils.SEPARATOR_DOT);
   return new Gene(
       Integer.valueOf(geneSplittedStr[1]),
       Integer.valueOf(geneSplittedStr[2]),
       StringUtils.substringBeforeLast(splittedLine[3], FileUtils.SEPARATOR_DOT));
 }
Exemplo n.º 10
0
 // 1234567.gpx -> 1234567-wpts.gpx
 static String getWaypointsFileNameForGpxFileName(String name) {
   if (StringUtils.endsWithIgnoreCase(name, GPX_FILE_EXTENSION)
       && (StringUtils.length(name) > GPX_FILE_EXTENSION.length())) {
     return StringUtils.substringBeforeLast(name, ".") + WAYPOINTS_FILE_SUFFIX_AND_EXTENSION;
   } else {
     return null;
   }
 }
Exemplo n.º 11
0
 // 1234567.zip -> 1234567.gpx
 static String getGpxFileNameForZipFileName(String name) {
   if (StringUtils.endsWithIgnoreCase(name, ZIP_FILE_EXTENSION)
       && (StringUtils.length(name) > ZIP_FILE_EXTENSION.length())) {
     return StringUtils.substringBeforeLast(name, ".") + GPX_FILE_EXTENSION;
   } else {
     return null;
   }
 }
Exemplo n.º 12
0
 @Override
 public String getProductName() {
   return getBrandingPlugins()
       .stream()
       .map(plugin -> StringUtils.substringBeforeLast(plugin.getProductName(), " "))
       .filter(Objects::nonNull)
       .findFirst()
       .orElse("DDF");
 }
Exemplo n.º 13
0
 private int count(String hql, Map<String, Object> paramMap) {
   StringBuffer sb =
       new StringBuffer("")
           .append("select count(*) from ")
           .append(
               StringUtils.substringBeforeLast(StringUtils.substringAfter(hql, "from"), "order "));
   Query query = getSession().createQuery(sb.toString());
   setQuery(query, paramMap);
   return ((Long) query.uniqueResult()).intValue();
 }
 /**
  * *
  * <li><b>Substring/Left/Right/Mid</b> - null-safe substring extractions
  * <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b> - substring extraction relative to
  *     other strings
  */
 @Test
 public void testSubstringLeftStringUtils() {
   System.out.println(strOne + ":" + strOne.length());
   System.out.println(StringUtils.substring(strOne, 3));
   System.out.println(StringUtils.substring(strOne, 3, 7));
   System.out.println(StringUtils.substringAfter(strOne, " "));
   System.out.println(StringUtils.substringAfterLast(strOne, " "));
   System.out.println(StringUtils.substringBefore(strOne, " "));
   System.out.println(StringUtils.substringBeforeLast(strOne, " "));
   System.out.println(StringUtils.substringBetween(strOne, "the", "not"));
 }
Exemplo n.º 15
0
 // 1234567.gpx -> 1234567-wpts.gpx
 static File getWaypointsFileForGpx(File file) {
   final String name = file.getName();
   if (StringUtils.endsWithIgnoreCase(name, GPX_FILE_EXTENSION)
       && (StringUtils.length(name) > GPX_FILE_EXTENSION.length())) {
     final String wptsName =
         StringUtils.substringBeforeLast(name, ".") + WAYPOINTS_FILE_SUFFIX_AND_EXTENSION;
     return new File(file.getParentFile(), wptsName);
   } else {
     return null;
   }
 }
Exemplo n.º 16
0
  /**
   * 處理學生退選線上選課紀錄之方法
   *
   * @param mapping org.apache.struts.action.ActionMapping object
   * @param form org.apache.struts.action.ActionForm object
   * @param request javax.servlet.http.HttpServletRequest object
   * @param response javax.servlet.http.HttpServletResponse object
   * @return org.apache.struts.action.ActionForward object
   * @exception java.lang.Exception
   */
  public ActionForward deleteCourse(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    DynaActionForm aForm = (DynaActionForm) form;
    ActionMessages messages = validateInputForUpdate(aForm, Toolket.getBundle(request));
    if (!messages.isEmpty()) {
      saveErrors(request, messages);
      return mapping.findForward(IConstants.ACTION_MAIN_NAME);
    } else {

      List<SeldDataInfo> aList = getSeldDataListByIndex(request);
      if (!aList.isEmpty()) {

        CourseManager cm = (CourseManager) getBean(COURSE_MANAGER_BEAN_NAME);
        // 取得選課欲退選之Oid列表
        StringBuffer seldBuf = new StringBuffer();
        for (SeldDataInfo info : aList) {
          seldBuf.append(info.getSeldOid()).append(",");
        }
        String inSyntax = StringUtils.substringBeforeLast(seldBuf.toString(), ",");
        log.info("Seld Oid SQL IN Syntax : " + inSyntax);
        String studentNo = aForm.getString("stdNo").toUpperCase();
        MemberManager mm = (MemberManager) getBean(MEMBER_MANAGER_BEAN_NAME);
        String classNo = mm.findStudentByNo(studentNo).getDepartClass();
        // 只有第一階段不會檢查選課人數下限
        cm.txRemoveSelectedSeld(studentNo, classNo, 1, inSyntax, false);
        String idno = getUserCredential(request.getSession(false)).getMember().getIdno();
        for (SeldDataInfo info : aList) {
          cm.txSaveAdcdHistory(info.getDtimeOid(), studentNo.toUpperCase(), idno, "D");
        }
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("Message.DeleteSuccessful"));
        saveMessages(request, messages);
        // aForm.initialize(mapping);
        Toolket.resetCheckboxCookie(response, SELD_LIST_NAME);
        setContentPage(request.getSession(false), "course/OnlineAddRemoveCourse.jsp");
      } else {
        messages = new ActionMessages();
        messages.add(
            ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage("Course.onlineAddRemoveCourse.unselected"));
        saveErrors(request, messages);
        return mapping.findForward(IConstants.ACTION_MAIN_NAME);
      }
    }

    return list(mapping, form, request, response);
  }
Exemplo n.º 17
0
  /**
   * easyui AJAX请求数据
   *
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "datagrid")
  public void datagrid(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
    List<ProcessDefinition> list = query.list();

    StringBuffer rows = new StringBuffer();
    int i = 0;
    for (ProcessDefinition pi : list) {
      Deployment deployment =
          repositoryService
              .createDeploymentQuery()
              .deploymentId(pi.getDeploymentId())
              .singleResult();
      i++;
      rows.append(
          "{'id':"
              + i
              + ",'processDefinitionId':'"
              + pi.getId()
              + "','deploymentDate':'"
              + new SimpleDateFormat("yyyy-MM-dd").format(deployment.getDeploymentTime())
              + "','resourceName':'"
              + pi.getResourceName()
              + "','deploymentId':'"
              + pi.getDeploymentId()
              + "','key':'"
              + pi.getKey()
              + "','name':'"
              + pi.getName()
              + "','version':'"
              + pi.getVersion()
              + "','isSuspended':'"
              + pi.isSuspended()
              + "'},");
    }
    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject =
        JSONObject.fromObject("{'total':" + query.count() + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
Exemplo n.º 18
0
  /**
   * Transforms the given image (i.e. shrinks the image and changes its quality) before it is
   * downloaded.
   *
   * @param image the image to be downloaded
   * @param width the desired image width; this value is taken into account only if it is greater
   *     than zero and less than the current image width
   * @param height the desired image height; this value is taken into account only if it is greater
   *     than zero and less than the current image height
   * @param quality the desired compression quality
   * @param context the XWiki context
   * @return the transformed image
   * @throws Exception if transforming the image fails
   */
  private XWikiAttachment downloadImage(
      XWikiAttachment image, int width, int height, float quality, XWikiContext context)
      throws Exception {
    initCache(context);

    boolean keepAspectRatio = Boolean.valueOf(context.getRequest().getParameter("keepAspectRatio"));

    XWikiAttachment thumbnail =
        (this.imageCache == null)
            ? shrinkImage(image, width, height, keepAspectRatio, quality, context)
            : downloadImageFromCache(image, width, height, keepAspectRatio, quality, context);

    // If the image has been transformed, update the file name extension to match the image format.
    String fileName = thumbnail.getFilename();
    String extension =
        StringUtils.lowerCase(StringUtils.substringAfterLast(fileName, String.valueOf('.')));
    if (thumbnail != image && !Arrays.asList("jpeg", "jpg", "png").contains(extension)) {
      // The scaled image is PNG, so correct the extension in order to output the correct MIME type.
      thumbnail.setFilename(StringUtils.substringBeforeLast(fileName, ".") + ".png");
    }
    return thumbnail;
  }
Exemplo n.º 19
0
  /**
   * easyui 流程历史数据获取
   *
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "taskHistoryList")
  public void taskHistoryList(
      @RequestParam("processInstanceId") String processInstanceId,
      HttpServletRequest request,
      HttpServletResponse response,
      DataGrid dataGrid) {

    List<HistoricTaskInstance> historicTasks =
        historyService
            .createHistoricTaskInstanceQuery()
            .processInstanceId(processInstanceId)
            .list();

    StringBuffer rows = new StringBuffer();
    for (HistoricTaskInstance hi : historicTasks) {
      rows.append(
          "{'name':'"
              + hi.getName()
              + "','processInstanceId':'"
              + hi.getProcessInstanceId()
              + "','startTime':'"
              + hi.getStartTime()
              + "','endTime':'"
              + hi.getEndTime()
              + "','assignee':'"
              + hi.getAssignee()
              + "','deleteReason':'"
              + hi.getDeleteReason()
              + "'},");
      // System.out.println(hi.getName()+"@"+hi.getAssignee()+"@"+hi.getStartTime()+"@"+hi.getEndTime());
    }

    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject =
        JSONObject.fromObject("{'total':" + historicTasks.size() + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
  /*
   * 原则:
   * 	当前page下要求解析出来的字段都要保证在当前page生命周期内解析完成。
   * 	哪些规则是当前page下的,由page.getRequest().getFiledRuleId()决定
   */
  @Override
  public void process(Page page) throws PageProcessException {

    Request originalReq = page.getRequest(); // 源rquest,非null
    Integer fieldRuleId = originalReq.getFieldRuleId();
    if (fieldRuleId != null) {
      if (fieldRuleId == 20) System.out.println("test");
    }

    Request nextRequest = originalReq.getNextRequest(); // 抽出下一步request,允许null
    Request templast = nextRequest; // 创建请求链时需要的临时节点

    // 找到当前page下要求解析的字段
    final List<SpiderFieldRule> dependenceFieldRules = new ArrayList<SpiderFieldRule>();

    for (SpiderFieldRule fieldRule : fieldRules) {
      if (fieldRule.getParentId() == (fieldRuleId == null ? 0 : fieldRuleId)) {
        dependenceFieldRules.add(fieldRule);
      }
    }
    // 开始解析当前page下要求解析的字段
    for (SpiderFieldRule fieldRule : dependenceFieldRules) {
      List<String> results;
      StringBuilder sb;
      switch (fieldRule.getType()) {
        case 0:
          results = page.getHtml().regex(fieldRule.getRule()).all();
          if (results.size() == 0) {
            if (fieldRule.getAllowEmpty() == 1) {
              throw new PageProcessException(
                  String.format(
                      "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#regex@#}",
                      fieldRule.getFieldName(), fieldRule.getRule(), fieldRule.getParentId()));
            }
          }
          // 判断是否会产生新的下载请求,如果产生新的下载请求,则当前规则只解析顶级层,如果有子规则,要=到新的下载完成之后才能解析
          if (fieldRule.getAdditionDownload() == 1) {
            // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              if (templast == null) {
                nextRequest = additionReq;
                templast = nextRequest;
              } else {
                templast.setNextRequest(additionReq);
                templast = additionReq;
              }
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else if (fieldRule.getAdditionRequest() == 1) {
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              transmitResultItem(page, additionReq);
              page.addTargetRequest(additionReq);
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else {
            if (fieldRule.getNeedPersistence() == 0) {
              sb = new StringBuilder();
              for (String result : results) {
                sb.append(result + ",");
              }
              page.putField(
                  fieldRule.getFieldName(),
                  StringUtils.substringBeforeLast(sb.toString().trim(), ","));
            }
            ruleComplierLoop(fieldRule.getId(), page, nextRequest, templast);
          }
          break;
        case 1:
          results = page.getHtml().xpath(fieldRule.getRule()).all();
          if (results.size() == 0) {
            if (fieldRule.getAllowEmpty() == 1) {
              throw new PageProcessException(
                  String.format(
                      "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#xpath@#}",
                      fieldRule.getFieldName(), fieldRule.getRule(), fieldRule.getParentId()));
            }
          }
          // 判断是否会产生新的下载请求
          if (fieldRule.getAdditionDownload() == 1) {
            // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              if (templast == null) {
                nextRequest = additionReq;
                templast = nextRequest;
              } else {
                templast.setNextRequest(additionReq);
                templast = additionReq;
              }
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else if (fieldRule.getAdditionRequest() == 1) {
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              transmitResultItem(page, additionReq);
              page.addTargetRequest(additionReq);
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else {
            if (fieldRule.getNeedPersistence() == 0) {
              sb = new StringBuilder();
              for (String result : results) {
                sb.append(result + ",");
              }
              page.putField(
                  fieldRule.getFieldName(),
                  StringUtils.substringBeforeLast(sb.toString().trim(), ","));
            }
            ruleComplierLoop(fieldRule.getId(), page, nextRequest, templast);
          }
          break;
        case 2:
          results = page.getHtml().css(fieldRule.getRule()).all();
          if (results.size() == 0) {
            if (fieldRule.getAllowEmpty() == 1) {
              throw new PageProcessException(
                  String.format(
                      "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#css@#}",
                      fieldRule.getFieldName(), fieldRule.getRule(), fieldRule.getParentId()));
            }
          }
          // 判断是否会产生新的下载请求
          if (fieldRule.getAdditionDownload() == 1) {
            // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              if (templast == null) {
                nextRequest = additionReq;
                templast = nextRequest;
              } else {
                templast.setNextRequest(additionReq);
                templast = additionReq;
              }
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else if (fieldRule.getAdditionRequest() == 1) {
            for (String result : results) {
              if (!result.startsWith("http")) {
                result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
              }
              Request additionReq = new Request(result);
              additionReq.setFieldRuleId(fieldRule.getId());
              transmitResultItem(page, additionReq);
              page.addTargetRequest(additionReq);
              if (fieldRule.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                sb.append(result + ",");
                page.putField(
                    fieldRule.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
            }
          } else {
            if (fieldRule.getNeedPersistence() == 0) {
              sb = new StringBuilder();
              for (String result : results) {
                sb.append(result + ",");
              }
              page.putField(
                  fieldRule.getFieldName(),
                  StringUtils.substringBeforeLast(sb.toString().trim(), ","));
            }
            ruleComplierLoop(fieldRule.getId(), page, nextRequest, templast);
          }
          break;
        default:
          if (page.getRequest().getExtra(fieldRule.getFieldName()) == null) {
            throw new PageProcessException(
                String.format(
                    "{fieldname:@#%s@#,fieldrule:@#%s@#,type:@#orig@#}",
                    fieldRule.getFieldName(), fieldRule.getRule()));
          }
          page.putField(
              fieldRule.getFieldName(), page.getRequest().getExtra(fieldRule.getFieldName()));
          break;
      }
    }
    // 最后判断一下当前page有没有产生新的下载请求或任务请求,如果有,将page.getResultItems()中的解析结果通过request传递到下一个page中去
    if (nextRequest != null) {
      transmitResultItem(page, nextRequest);
      page.setSkip(true);
      originalReq.setNextRequest(nextRequest);
    }
  }
  /**
   * 当某个解析规则不会产生新的下载请求时(这种情况下当前page生命周期已经结束),当前page必须解析完该规则下的所有字段,存在子规则层层嵌套的情况
   *
   * @param parentRuleId
   * @param page
   * @param nextRequest
   * @param templast
   * @throws PageProcessException
   */
  private void ruleComplierLoop(int parentRuleId, Page page, Request nextRequest, Request templast)
      throws PageProcessException {

    List<SpiderFieldRule> childs = new ArrayList<SpiderFieldRule>();
    for (SpiderFieldRule fieldRule : fieldRules) {
      if (fieldRule.getParentId() == parentRuleId) {
        childs.add(fieldRule);
      }
    }
    if (childs.size() == 0) {
      return;
    } else {
      for (SpiderFieldRule child : childs) {
        List<String> results;
        StringBuilder sb;
        switch (child.getType()) {
          case 0:
            results = page.getHtml().regex(child.getRule()).all();
            if (results.size() == 0) {
              if (child.getAllowEmpty() == 1) {
                throw new PageProcessException(
                    String.format(
                        "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#regex@#}",
                        child.getFieldName(), child.getRule(), child.getParentId()));
              }
            }
            // 判断是否会产生新的下载请求
            if (child.getAdditionDownload() == 1) {
              // page.setIncludeAddition(true);
              // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                if (templast == null) {
                  nextRequest = additionReq;
                  templast = nextRequest;
                } else {
                  templast.setNextRequest(additionReq);
                  templast = additionReq;
                }
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else if (child.getAdditionRequest() == 1) {
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                page.addTargetRequest(additionReq);
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else {
              if (child.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                for (String result : results) {
                  sb.append(result + ",");
                }
                page.putField(
                    child.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
              ruleComplierLoop(child.getId(), page, nextRequest, templast);
            }
            break;
          case 1:
            results = page.getHtml().xpath(child.getRule()).all();
            if (results.size() == 0) {
              if (child.getAllowEmpty() == 1) {
                throw new PageProcessException(
                    String.format(
                        "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#xpath@#}",
                        child.getFieldName(), child.getRule(), child.getParentId()));
              }
            }
            // 判断是否会产生新的下载请求
            if (child.getAdditionDownload() == 1) {
              // page.setIncludeAddition(true);
              // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                if (templast == null) {
                  nextRequest = additionReq;
                  templast = nextRequest;
                } else {
                  templast.setNextRequest(additionReq);
                  templast = additionReq;
                }
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else if (child.getAdditionRequest() == 1) {
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                page.addTargetRequest(additionReq);
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else {
              if (child.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                for (String result : results) {
                  sb.append(result + ",");
                }
                page.putField(
                    child.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
              ruleComplierLoop(child.getId(), page, nextRequest, templast);
            }
            break;
          case 2:
            results = page.getHtml().css(child.getRule()).all();
            if (results.size() == 0) {
              if (child.getAllowEmpty() == 1) {
                throw new PageProcessException(
                    String.format(
                        "{fieldname:@#%s@#,fieldrule:@#%s@#,parentId:@#%d@#,type:@#css@#}",
                        child.getFieldName(), child.getRule(), child.getParentId()));
              }
            }
            // 判断是否会产生新的下载请求
            if (child.getAdditionDownload() == 1) {
              // page.setIncludeAddition(true);
              // 将fieldRule.getId()保存下来,新请求的页面将筛选parentid为该id的规则进行解析
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                if (templast == null) {
                  nextRequest = additionReq;
                  templast = nextRequest;
                } else {
                  templast.setNextRequest(additionReq);
                  templast = additionReq;
                }
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else if (child.getAdditionRequest() == 1) {
              for (String result : results) {
                if (!result.startsWith("http")) {
                  result = "http://" + site.getDomain() + result.trim().replace("|", "%7C");
                }
                Request additionReq = new Request(result);
                additionReq.setFieldRuleId(child.getId());
                page.addTargetRequest(additionReq);
                if (child.getNeedPersistence() == 0) {
                  sb = new StringBuilder();
                  sb.append(result + ",");
                  page.putField(
                      child.getFieldName(),
                      StringUtils.substringBeforeLast(sb.toString().trim(), ","));
                }
              }
            } else {
              if (child.getNeedPersistence() == 0) {
                sb = new StringBuilder();
                for (String result : results) {
                  sb.append(result + ",");
                }
                page.putField(
                    child.getFieldName(),
                    StringUtils.substringBeforeLast(sb.toString().trim(), ","));
              }
              ruleComplierLoop(child.getId(), page, nextRequest, templast);
            }
            break;
          default:
            if (page.getRequest().getExtra(child.getFieldName()) == null) {
              throw new PageProcessException(
                  String.format(
                      "{fieldname:@#%s@#,fieldrule:@#%s@#,type:@#orig@#}",
                      child.getFieldName(), child.getRule()));
            }
            page.putField(child.getFieldName(), page.getRequest().getExtra(child.getFieldName()));
            break;
        }
      }
    }
  }
Exemplo n.º 22
0
  @Override
  protected void doGet(final HttpServletRequest request, final HttpServletResponse response) {

    final Authenticator auth = config.getAuthenticator();
    final SecurityContext securityContext;
    final App app;

    try {
      String path = request.getPathInfo();

      // check for registration (has its own tx because of write access
      if (checkRegistration(auth, request, response, path)) {

        return;
      }

      // isolate request authentication in a transaction
      try (final Tx tx = StructrApp.getInstance().tx()) {
        securityContext = auth.initializeAndExamineRequest(request, response);
        tx.success();
      }

      app = StructrApp.getInstance(securityContext);

      try (final Tx tx = app.tx()) {

        // Ensure access mode is frontend
        securityContext.setAccessMode(AccessMode.Frontend);

        request.setCharacterEncoding("UTF-8");

        // Important: Set character encoding before calling response.getWriter() !!, see Servlet
        // Spec 5.4
        response.setCharacterEncoding("UTF-8");

        boolean dontCache = false;

        logger.log(Level.FINE, "Path info {0}", path);

        // don't continue on redirects
        if (response.getStatus() == 302) {
          return;
        }

        Principal user = securityContext.getUser(false);
        if (user != null) {

          // Don't cache if a user is logged in
          dontCache = true;
        }

        final RenderContext renderContext =
            RenderContext.getInstance(request, response, getEffectiveLocale(request));

        renderContext.setResourceProvider(config.getResourceProvider());

        EditMode edit = renderContext.getEditMode(user);

        DOMNode rootElement = null;
        AbstractNode dataNode = null;

        String[] uriParts = PathHelper.getParts(path);
        if ((uriParts == null) || (uriParts.length == 0)) {

          // find a visible page
          rootElement = findIndexPage(securityContext);

          logger.log(Level.FINE, "No path supplied, trying to find index page");

        } else {

          if (rootElement == null) {

            rootElement = findPage(securityContext, request, path);

          } else {
            dontCache = true;
          }
        }

        if (rootElement == null) { // No page found

          // Look for a file
          File file = findFile(securityContext, request, path);
          if (file != null) {

            streamFile(securityContext, file, request, response, edit);
            return;
          }

          // store remaining path parts in request
          Matcher matcher = threadLocalUUIDMatcher.get();
          boolean requestUriContainsUuids = false;

          for (int i = 0; i < uriParts.length; i++) {

            request.setAttribute(uriParts[i], i);
            matcher.reset(uriParts[i]);

            // set to "true" if part matches UUID pattern
            requestUriContainsUuids |= matcher.matches();
          }

          if (!requestUriContainsUuids) {

            // Try to find a data node by name
            dataNode = findFirstNodeByName(securityContext, request, path);

          } else {

            dataNode = findNodeByUuid(securityContext, PathHelper.getName(path));
          }

          if (dataNode != null) {

            // Last path part matches a data node
            // Remove last path part and try again searching for a page
            // clear possible entry points
            request.removeAttribute(POSSIBLE_ENTRY_POINTS);

            rootElement =
                findPage(
                    securityContext,
                    request,
                    StringUtils.substringBeforeLast(path, PathHelper.PATH_SEP));

            renderContext.setDetailsDataObject(dataNode);

            // Start rendering on data node
            if (rootElement == null && dataNode instanceof DOMNode) {

              rootElement = ((DOMNode) dataNode);
            }
          }
        }

        // Still nothing found, do error handling
        if (rootElement == null) {

          // Check if security context has set an 401 status
          if (response.getStatus() == HttpServletResponse.SC_UNAUTHORIZED) {

            try {

              UiAuthenticator.writeUnauthorized(response);

            } catch (IllegalStateException ise) {
            }

          } else {

            rootElement = notFound(response, securityContext);
          }
        }

        if (rootElement == null) {
          return;
        }

        if (EditMode.WIDGET.equals(edit) || dontCache) {

          setNoCacheHeaders(response);
        }

        if (!securityContext.isVisible(rootElement)) {

          rootElement = notFound(response, securityContext);
          if (rootElement == null) {
            return;
          }
        }

        if (securityContext.isVisible(rootElement)) {

          if (!EditMode.WIDGET.equals(edit)
              && !dontCache
              && notModifiedSince(request, response, rootElement, dontCache)) {

            ServletOutputStream out = response.getOutputStream();
            out.flush();
            // response.flushBuffer();
            out.close();

          } else {

            // prepare response
            response.setCharacterEncoding("UTF-8");

            String contentType = rootElement.getProperty(Page.contentType);

            if (contentType != null && contentType.equals("text/html")) {

              contentType = contentType.concat(";charset=UTF-8");
              response.setContentType(contentType);

            } else {

              // Default
              response.setContentType("text/html;charset=UTF-8");
            }

            response.setHeader("Strict-Transport-Security", "max-age=60");
            response.setHeader("X-Content-Type-Options", "nosniff");
            response.setHeader("X-Frame-Options", "SAMEORIGIN");
            response.setHeader("X-XSS-Protection", "1; mode=block");

            // async or not?
            boolean isAsync =
                HttpService.parseBoolean(
                    Services.getBaseConfiguration().getProperty(HttpService.ASYNC), true);
            if (isAsync) {

              final AsyncContext async = request.startAsync();
              final ServletOutputStream out = async.getResponse().getOutputStream();
              final AtomicBoolean finished = new AtomicBoolean(false);
              final DOMNode rootNode = rootElement;

              threadPool.submit(
                  new Runnable() {

                    @Override
                    public void run() {

                      try (final Tx tx = app.tx()) {

                        // final long start = System.currentTimeMillis();

                        // render
                        rootNode.render(securityContext, renderContext, 0);
                        finished.set(true);

                        // final long end = System.currentTimeMillis();
                        // System.out.println("Done in " + (end-start) + " ms");

                        tx.success();

                      } catch (Throwable t) {
                        t.printStackTrace();
                        final String errorMsg = t.getMessage();
                        try {
                          // response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                          response.sendError(
                              HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMsg);
                          finished.set(true);
                        } catch (IOException ex) {
                          ex.printStackTrace();
                        }
                      }
                    }
                  });

              // start output write listener
              out.setWriteListener(
                  new WriteListener() {

                    @Override
                    public void onWritePossible() throws IOException {

                      try {

                        final Queue<String> queue = renderContext.getBuffer().getQueue();
                        while (out.isReady()) {

                          String buffer = null;

                          synchronized (queue) {
                            buffer = queue.poll();
                          }

                          if (buffer != null) {

                            out.print(buffer);

                          } else {

                            if (finished.get()) {

                              async.complete();
                              response.setStatus(HttpServletResponse.SC_OK);

                              // prevent this block from being called again
                              break;
                            }

                            Thread.sleep(1);
                          }
                        }

                      } catch (Throwable t) {
                        t.printStackTrace();
                      }
                    }

                    @Override
                    public void onError(Throwable t) {
                      t.printStackTrace();
                    }
                  });

            } else {

              final StringRenderBuffer buffer = new StringRenderBuffer();
              renderContext.setBuffer(buffer);

              // render
              rootElement.render(securityContext, renderContext, 0);

              response.getOutputStream().write(buffer.getBuffer().toString().getBytes("utf-8"));
              response.getOutputStream().flush();
              response.getOutputStream().close();
            }
          }

        } else {

          notFound(response, securityContext);
        }

        tx.success();

      } catch (FrameworkException fex) {
        fex.printStackTrace();
        logger.log(Level.SEVERE, "Exception while processing request", fex);
      }

    } catch (IOException | FrameworkException t) {

      t.printStackTrace();
      logger.log(Level.SEVERE, "Exception while processing request", t);
      UiAuthenticator.writeInternalServerError(response);
    }
  }
Exemplo n.º 23
0
  /**
   * @Title: myWorkTaskData @Description: TODO
   *
   * @param request
   * @param response
   * @param dataGrid void
   * @throws
   * @exception
   * @author fly
   * @date 2015年6月23日 上午10:20:42
   */
  @RequestMapping(params = "myWorkTaskData")
  public void myWorkTaskData(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    String userId = ResourceUtil.getSessionUserName().getId();
    // involvedUser 当前用户相关的
    HistoricProcessInstanceQuery query =
        historyService.createHistoricProcessInstanceQuery().involvedUser(userId);

    List<HistoricProcessInstance> historicTasks =
        query
            .orderByProcessInstanceStartTime()
            .desc()
            .listPage(dataGrid.getStart(), dataGrid.getEnd());
    long total = query.count();
    System.out.println(dataGrid.getStart() + " end: " + dataGrid.getEnd());
    StringBuffer rows = new StringBuffer();
    for (HistoricProcessInstance t : historicTasks) {
      ProcessDefinition processDefinition =
          repositoryService.getProcessDefinition(t.getProcessDefinitionId());

      rows.append(
          "{'id':'"
              + t.getId()
              + "','key':'"
              + processDefinition.getName()
              + "-"
              + DateUtils.date_sdf.format(t.getStartTime())
              + "','taskId':'"
              + t.getId()
              + "'");
      // 流程详细查看
      WorkFlowSetEntity workFlowSet =
          systemService.findUniqueByProperty(
              WorkFlowSetEntity.class, "deploymentId", processDefinition.getDeploymentId());
      if (workFlowSet != null) {
        rows.append(",'action':'" + workFlowSet.getDetailUrl() + "'");
      }
      // 流程用户处理
      if (t.getStartUserId() != null) {
        TSUser user = systemService.get(TSUser.class, t.getStartUserId());
        rows.append(",'username':'******'");
      }

      // 流程开始结束时间处理
      if (t.getStartTime() == null) {
        rows.append(",'beginDate':'无'");
      } else {
        rows.append(",'beginDate':'" + DateUtils.datetimeFormat.format(t.getStartTime()) + "'");
      }
      if (t.getEndTime() == null) {
        rows.append(",'endDate':'无','stateType':'办理中'");

      } else {
        rows.append(
            ",'endDate':'"
                + DateUtils.datetimeFormat.format(t.getEndTime())
                + "','stateType':'已完成'");
      }
      rows.append("},");
    }
    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject = JSONObject.fromObject("{'total':" + total + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }
Exemplo n.º 24
0
  @Override
  public boolean process(
      final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) {
    // Get all classes that has the annotation
    Set<? extends Element> classElements =
        roundEnvironment.getElementsAnnotatedWith(BoundBox.class);
    // For each class that has the annotation
    for (final Element classElement : classElements) {

      // Get the annotation information
      TypeElement boundClass = null;
      String maxSuperClass = null;
      String[] prefixes = null;
      String boundBoxPackageName = null;

      List<? extends AnnotationValue> extraBoundFields = null;
      List<? extends AnnotationMirror> listAnnotationMirrors = classElement.getAnnotationMirrors();
      if (listAnnotationMirrors == null) {
        messager.printMessage(Kind.WARNING, "listAnnotationMirrors is null", classElement);
        return true;
      }

      StringBuilder message = new StringBuilder();
      for (AnnotationMirror annotationMirror : listAnnotationMirrors) {
        log.info("mirror " + annotationMirror.getAnnotationType());
        Map<? extends ExecutableElement, ? extends AnnotationValue> map =
            annotationMirror.getElementValues();
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
            map.entrySet()) {
          message.append(entry.getKey().getSimpleName().toString());
          message.append("\n");
          message.append(entry.getValue().toString());
          if (BOUNDBOX_ANNOTATION_PARAMETER_BOUND_CLASS.equals(
              entry.getKey().getSimpleName().toString())) {
            boundClass = getAnnotationValueAsTypeElement(entry.getValue());
          }
          if (BOUNDBOX_ANNOTATION_PARAMETER_MAX_SUPER_CLASS.equals(
              entry.getKey().getSimpleName().toString())) {
            maxSuperClass = getAnnotationValueAsTypeElement(entry.getValue()).asType().toString();
          }
          if (BOUNDBOX_ANNOTATION_PARAMETER_EXTRA_BOUND_FIELDS.equals(
              entry.getKey().getSimpleName().toString())) {
            extraBoundFields = getAnnotationValueAsAnnotationValueList(entry.getValue());
          }
          if (BOUNDBOX_ANNOTATION_PARAMETER_PREFIXES.equals(
              entry.getKey().getSimpleName().toString())) {
            List<? extends AnnotationValue> listPrefixes =
                getAnnotationValueAsAnnotationValueList(entry.getValue());
            prefixes = new String[listPrefixes.size()];
            for (int indexAnnotation = 0;
                indexAnnotation < listPrefixes.size();
                indexAnnotation++) {
              prefixes[indexAnnotation] =
                  getAnnotationValueAsString(listPrefixes.get(indexAnnotation));
            }
          }
          if (BOUNDBOX_ANNOTATION_PARAMETER_PACKAGE.equals(
              entry.getKey().getSimpleName().toString())) {
            boundBoxPackageName = getAnnotationValueAsString(entry.getValue());
          }
        }
      }

      if (boundClass == null) {
        messager.printMessage(Kind.WARNING, "BoundClass is null : " + message, classElement);
        return true;
      }

      if (maxSuperClass != null) {
        boundClassVisitor.setMaxSuperClassName(maxSuperClass);
      }

      if (prefixes != null && prefixes.length != 2 && prefixes.length != 1) {
        error(
            classElement,
            "You must provide 1 or 2 prefixes. The first one for class names, the second one for methods.");
        return true;
      }
      if (prefixes != null && prefixes.length == 1) {
        String[] newPrefixes = new String[] {prefixes[0], prefixes[0].toLowerCase(Locale.US)};
        prefixes = newPrefixes;
      }
      boundboxWriter.setPrefixes(prefixes);

      if (boundBoxPackageName == null) {
        String boundClassFQN = boundClass.getQualifiedName().toString();
        if (boundClassFQN.contains(PACKAGE_SEPARATOR)) {
          boundBoxPackageName = StringUtils.substringBeforeLast(boundClassFQN, PACKAGE_SEPARATOR);
        } else {
          boundBoxPackageName = StringUtils.EMPTY;
        }
      }
      boundClassVisitor.setBoundBoxPackageName(boundBoxPackageName);
      boundboxWriter.setBoundBoxPackageName(boundBoxPackageName);

      ClassInfo classInfo = boundClassVisitor.scan(boundClass);

      injectExtraBoundFields(extraBoundFields, classInfo);

      listClassInfo.add(classInfo);

      // perform some computations on meta model
      inheritanceComputer.computeInheritanceAndHidingFields(classInfo.getListFieldInfos());
      inheritanceComputer.computeInheritanceAndOverridingMethods(
          classInfo.getListMethodInfos(), boundClass, elements);
      inheritanceComputer.computeInheritanceAndHidingInnerClasses(
          classInfo.getListInnerClassInfo());
      inheritanceComputer.computeInheritanceInInnerClasses(classInfo, elements);

      // write meta model to java class file
      Writer sourceWriter = null;
      try {
        String boundBoxClassName =
            boundboxWriter.getNamingGenerator().createBoundBoxName(classInfo);
        String boundBoxClassFQN =
            boundBoxPackageName.isEmpty()
                ? boundBoxClassName
                : boundBoxPackageName + PACKAGE_SEPARATOR + boundBoxClassName;
        JavaFileObject sourceFile = filer.createSourceFile(boundBoxClassFQN, (Element[]) null);
        sourceWriter = sourceFile.openWriter();

        boundboxWriter.writeBoundBox(classInfo, sourceWriter);
      } catch (IOException e) {
        e.printStackTrace();
        error(classElement, e.getMessage());
      } finally {
        if (sourceWriter != null) {
          IOUtils.closeQuietly(sourceWriter);
        }
      }
    }

    return true;
  }
Exemplo n.º 25
0
  public static Optional<ItemStack> getItemStack(final String name, final int quantity) {

    if (name == null || name.isEmpty()) return Optional.absent();

    // Check our preferred list first. If we have a hit, use it.
    ItemStack result = PreferredItemStacks.instance.get(name);

    if (result != null) {

      result = result.copy();
      result.stackSize = quantity;

    } else {

      // Parse out the possible subtype from the end of the string
      String workingName = name;
      int subType = -1;

      if (StringUtils.countMatches(name, ":") == 2) {
        workingName = StringUtils.substringBeforeLast(name, ":");
        final String num = StringUtils.substringAfterLast(name, ":");

        if (num != null && !num.isEmpty()) {

          if ("*".compareTo(num) == 0) subType = OreDictionaryHelper.WILDCARD_VALUE;
          else {
            try {
              subType = Integer.parseInt(num);
            } catch (Exception e) {
              // It appears malformed - assume the incoming name
              // is
              // the real name and continue.
              ;
            }
          }
        }
      }

      // Check the OreDictionary first for any alias matches. Otherwise
      // go to the game registry to find a match.
      final List<ItemStack> ores = OreDictionaryHelper.getOres(workingName);
      if (!ores.isEmpty()) {
        result = ores.get(0).copy();
        result.stackSize = quantity;
      } else {
        final Item i = GameData.getItemRegistry().getObject(workingName);
        if (i != null) {
          result = new ItemStack(i, quantity);
        }
      }

      // If we did have a hit on a base item, set the sub-type
      // as needed.
      if (result != null && subType != -1) {
        if (subType == OreDictionaryHelper.WILDCARD_VALUE && !result.getHasSubtypes()) {
          ModLog.warn("[%s] GENERIC requested but Item does not support sub-types", name);
        } else {
          result.setItemDamage(subType);
        }
      }
    }

    return Optional.fromNullable(result);
  }
Exemplo n.º 26
0
  /**
   * @param info
   * @return
   */
  public static Set<String> getLocations(
      LogisticInfo info, Map<String, Map<String, Set<String>>> dizhi) {
    Set<String> locations = new LinkedHashSet<String>();
    if (info == null) {
      return locations;
    }

    if (info.getTrackInfoList() != null)
      for (TrackInfo trackInfo : info.getTrackInfoList()) {
        String context = trackInfo.getContext();
        if (StringUtils.isNotBlank(context)) {
          context = StringUtils.substringBeforeLast(context, "正发往");
          context = StringUtils.substringBeforeLast(context, "分拨中心");
          String local = "";
          for (Map.Entry<String, Map<String, Set<String>>> entry : dizhi.entrySet()) {
            if (context.contains(entry.getKey()) || context.contains(remove(entry.getKey()))) {
              local = local + entry.getKey();
              for (Map.Entry<String, Set<String>> subEntry : entry.getValue().entrySet()) {
                if (StringUtils.equals(subEntry.getKey(), "市辖区")
                    || StringUtils.equals(subEntry.getKey(), "县")
                    || StringUtils.equals(subEntry.getKey(), "自治区直辖县级行政区划")) {
                  for (String str : subEntry.getValue()) {
                    if (context.contains(str) || context.contains(remove(str))) {
                      local = local + str;
                      break;
                    }
                  }

                } else if (context.contains(subEntry.getKey())
                    || context.contains(remove(subEntry.getKey()))) {
                  local = local + subEntry.getKey();
                  for (String str : subEntry.getValue()) {
                    if (context.contains(str) || context.contains(remove(str))) {
                      local = local + str;
                      break;
                    }
                  }
                }
              }
              break;
            } else {
              for (Map.Entry<String, Set<String>> subEntry : entry.getValue().entrySet()) {
                if (StringUtils.equals(subEntry.getKey(), "市辖区")
                    || StringUtils.equals(subEntry.getKey(), "县")
                    || StringUtils.equals(subEntry.getKey(), "自治区直辖县级行政区划")) {
                  for (String str : subEntry.getValue()) {
                    if (context.contains(str) || context.contains(remove(str))) {
                      local = local + str;
                      break;
                    }
                  }

                } else if (context.contains(subEntry.getKey())
                    || context.contains(remove(subEntry.getKey()))) {
                  local = local + subEntry.getKey();
                  for (String str : subEntry.getValue()) {
                    if (context.contains(str) || context.contains(remove(str))) {
                      local = local + str;
                      break;
                    }
                  }
                }
              }
            }
          }
          locations.add(local);
        }
      }
    return locations;
  }