protected Map<String, String> readUsersFile(InputStream usersInputStream) throws IOException {

    if (usersInputStream == null) {
      return Collections.emptyMap();
    }

    Map<String, String> usersMap = new HashMap<String, String>();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new InputStreamReader(usersInputStream));

    String line = unsyncBufferedReader.readLine();

    while (line != null) {
      String[] array = StringUtil.split(line);

      if ((array.length == 2) && Validator.isNotNull(array[0]) && Validator.isNotNull(array[1])) {

        usersMap.put(array[0], array[1]);
      } else {
        if (_log.isInfoEnabled()) {
          _log.info("Ignoring line " + line + " because it does not contain exactly 2 columns");
        }
      }

      line = unsyncBufferedReader.readLine();
    }

    return usersMap;
  }
Beispiel #2
0
  @Override
  protected String reword(String data) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(data));

    StringBundler sb = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.startsWith(ALTER_COLUMN_NAME) || line.startsWith(ALTER_COLUMN_TYPE)) {

        line = "-- " + line;

        if (_log.isWarnEnabled()) {
          _log.warn("This statement is not supported by Derby: " + line);
        }
      } else if (line.indexOf(DROP_INDEX) != -1) {
        String[] tokens = StringUtil.split(line, " ");

        line = StringUtil.replace("drop index @index@;", "@index@", tokens[2]);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    return sb.toString();
  }
Beispiel #3
0
  public Object convert(String key) throws WebCacheException {
    Whois whois = null;

    try {
      Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);

      UnsyncBufferedReader unsyncBufferedReader =
          new UnsyncBufferedReader(new InputStreamReader(socket.getInputStream()));

      PrintStream out = new PrintStream(socket.getOutputStream());

      out.println(_domain);

      StringBundler sb = new StringBundler();
      String line = null;

      while ((line = unsyncBufferedReader.readLine()) != null) {
        if (line.startsWith("Results ")) {
          break;
        }

        sb.append(line).append("\n");
      }

      unsyncBufferedReader.close();
      socket.close();

      whois = new Whois(_domain, StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
    } catch (Exception e) {
      throw new WebCacheException(_domain + " " + e.toString());
    }

    return whois;
  }
Beispiel #4
0
  protected String reword(String data) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(data));

    StringBundler sb = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.startsWith(ALTER_COLUMN_NAME)) {
        String[] template = buildColumnNameTokens(line);

        line =
            StringUtil.replace(
                "exec sp_rename '@table@.@old-column@', '@new-column@', " + "'column';",
                REWORD_TEMPLATE,
                template);
      } else if (line.startsWith(ALTER_COLUMN_TYPE)) {
        String[] template = buildColumnTypeTokens(line);

        line =
            StringUtil.replace(
                "alter table @table@ alter column @old-column@ @type@;", REWORD_TEMPLATE, template);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    return sb.toString();
  }
Beispiel #5
0
  @Override
  public void buildSQLFile(String sqlDir, String fileName) throws IOException {

    String oracle = buildTemplate(sqlDir, fileName);

    oracle = _preBuildSQL(oracle);

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(oracle));

    StringBundler imageSB = new StringBundler();
    StringBundler journalArticleSB = new StringBundler();
    StringBundler journalStructureSB = new StringBundler();
    StringBundler journalTemplateSB = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.startsWith("insert into Image")) {
        _convertToOracleCSV(line, imageSB);
      } else if (line.startsWith("insert into JournalArticle (")) {
        _convertToOracleCSV(line, journalArticleSB);
      } else if (line.startsWith("insert into JournalStructure (")) {
        _convertToOracleCSV(line, journalStructureSB);
      } else if (line.startsWith("insert into JournalTemplate (")) {
        _convertToOracleCSV(line, journalTemplateSB);
      }
    }

    unsyncBufferedReader.close();

    if (imageSB.length() > 0) {
      FileUtil.write(
          sqlDir + "/" + fileName + "/" + fileName + "-oracle-image.csv", imageSB.toString());
    }

    if (journalArticleSB.length() > 0) {
      FileUtil.write(
          sqlDir + "/" + fileName + "/" + fileName + "-oracle-journalarticle.csv",
          journalArticleSB.toString());
    }

    if (journalStructureSB.length() > 0) {
      FileUtil.write(
          sqlDir + "/" + fileName + "/" + fileName + "-oracle-journalstructure.csv",
          journalStructureSB.toString());
    }

    if (journalTemplateSB.length() > 0) {
      FileUtil.write(
          sqlDir + "/" + fileName + "/" + fileName + "-oracle-journaltemplate.csv",
          journalTemplateSB.toString());
    }

    oracle = _postBuildSQL(oracle);

    FileUtil.write(sqlDir + "/" + fileName + "/" + fileName + "-oracle.sql", oracle);
  }
  @Override
  protected String reword(String data) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(data));

    StringBundler sb = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.contains(DROP_COLUMN)) {
        line = StringUtil.replace(line, " drop column ", " drop ");
      }

      if (line.startsWith(ALTER_COLUMN_NAME)) {
        String[] template = buildColumnNameTokens(line);

        line =
            StringUtil.replace(
                "exec sp_rename '@table@.@old-column@', '@new-column@', " + "'column';",
                REWORD_TEMPLATE,
                template);
      } else if (line.startsWith(ALTER_COLUMN_TYPE)) {
        String[] template = buildColumnTypeTokens(line);

        line =
            StringUtil.replace(
                "alter table @table@ modify @old-column@ @type@;", REWORD_TEMPLATE, template);
      } else if (line.startsWith(ALTER_TABLE_NAME)) {
        String[] template = buildTableNameTokens(line);

        line =
            StringUtil.replace(
                "exec sp_rename @old-table@, @new-table@;", RENAME_TABLE_TEMPLATE, template);
      } else if (line.contains(DROP_INDEX)) {
        String[] tokens = StringUtil.split(line, ' ');

        String tableName = tokens[4];

        if (tableName.endsWith(StringPool.SEMICOLON)) {
          tableName = tableName.substring(0, tableName.length() - 1);
        }

        line = StringUtil.replace("drop index @table@.@index@;", "@table@", tableName);
        line = StringUtil.replace(line, "@index@", tokens[2]);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    return sb.toString();
  }
Beispiel #7
0
  @Override
  protected String reword(String data) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(data));

    boolean createTable = false;

    StringBundler sb = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (StringUtil.startsWith(line, "create table")) {
        createTable = true;
      } else if (line.startsWith(ALTER_COLUMN_NAME)) {
        String[] template = buildColumnNameTokens(line);

        line =
            StringUtil.replace(
                "alter table @table@ change column @old-column@ " + "@new-column@ @type@;",
                REWORD_TEMPLATE,
                template);
      } else if (line.startsWith(ALTER_COLUMN_TYPE)) {
        String[] template = buildColumnTypeTokens(line);

        line =
            StringUtil.replace(
                "alter table @table@ modify @old-column@ @type@;", REWORD_TEMPLATE, template);
      }

      int pos = line.indexOf(";");

      if (createTable && (pos != -1)) {
        createTable = false;

        line =
            line.substring(0, pos)
                + " engine "
                + PropsValues.DATABASE_MYSQL_ENGINE
                + line.substring(pos);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    return sb.toString();
  }
  @Test
  public void testIsAndroid() throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(
            new InputStreamReader(getClass().getResourceAsStream("dependencies/user_agents.csv")));

    boolean android = false;
    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      line = line.trim();

      if (line.isEmpty()) {
        continue;
      }

      if (line.contains("Android")) {
        android = true;

        continue;
      }

      if (android && (line.charAt(0) == CharPool.POUND)) {
        break;
      }

      if (android) {
        MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

        mockHttpServletRequest.addHeader(HttpHeaders.USER_AGENT, line);

        Assert.assertTrue(_browserSnifferImpl.isAndroid(mockHttpServletRequest));
      }
    }

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.addHeader(
        HttpHeaders.USER_AGENT,
        "Safari 6, 6.0, 536.26, mozilla/5.0 (ipad; cpu os 6_0 like mac os"
            + " x) applewebkit/536.26 (khtml, like gecko) version/6.0"
            + " mobile/10a5355d safari/8536.25");

    Assert.assertFalse(_browserSnifferImpl.isAndroid(mockHttpServletRequest));
  }
  private void _updateLanguageProperties(String key, String value) throws IOException {

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new FileReader(_languagePropertiesFile));

    StringBundler sb = new StringBundler();

    boolean begin = false;
    boolean firstLine = true;
    String linePrefix = key + "=";

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.equals(StringPool.BLANK)) {
        begin = !begin;
      }

      if (firstLine) {
        firstLine = false;
      } else {
        sb.append(StringPool.NEW_LINE);
      }

      if (line.startsWith(linePrefix)) {
        sb.append(linePrefix + value);
      } else {
        sb.append(line);
      }
    }

    unsyncBufferedReader.close();

    Writer writer =
        new OutputStreamWriter(
            new FileOutputStream(_languagePropertiesFile, false), StringPool.UTF8);

    writer.write(sb.toString());

    writer.close();

    System.out.println("Updating " + _languagePropertiesFile + " key " + key);
  }
  protected void compressSQL(Reader reader) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(reader);

    String s = null;

    while ((s = unsyncBufferedReader.readLine()) != null) {
      s = s.trim();

      if (s.length() > 0) {
        if (s.startsWith("insert into ")) {
          compressInsertSQL(s.substring(12));
        } else if (s.length() > 0) {
          _otherSQLs.add(s);
        }
      }
    }

    unsyncBufferedReader.close();
  }
Beispiel #11
0
  @Override
  protected String reword(String data) throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(data));

    StringBundler sb = new StringBundler();

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      if (line.startsWith(ALTER_COLUMN_NAME)) {
        String[] template = buildColumnNameTokens(line);

        line =
            StringUtil.replace(
                "alter table @table@ add column @new-column@ @type@;\n", REWORD_TEMPLATE, template);

        line =
            line
                + StringUtil.replace(
                    "update @table@ set @new-column@ = @old-column@;\n", REWORD_TEMPLATE, template);

        line =
            line
                + StringUtil.replace(
                    "alter table @table@ drop column @old-column@", REWORD_TEMPLATE, template);
      } else if (line.startsWith(ALTER_COLUMN_TYPE)) {
        line = "-- " + line;
      } else if (line.indexOf(DROP_INDEX) != -1) {
        String[] tokens = StringUtil.split(line, ' ');

        line = StringUtil.replace("drop index @index@;", "@index@", tokens[2]);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    return sb.toString();
  }
  protected String formatPortalPortalProperties() throws Exception {
    if (!portalSource) {
      return ContentUtil.get("portal.properties");
    }

    String fileName = "portal-impl/src/portal.properties";

    File file = new File(BASEDIR + fileName);

    String content = fileUtil.read(file);

    StringBundler sb = new StringBundler();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      line = trimLine(line, true);

      if (line.startsWith(StringPool.TAB)) {
        line = line.replaceFirst(StringPool.TAB, StringPool.FOUR_SPACES);
      }

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    String newContent = sb.toString();

    if (newContent.endsWith("\n")) {
      newContent = newContent.substring(0, newContent.length() - 1);
    }

    compareAndAutoFixContent(file, fileName, content, newContent);

    return newContent;
  }
  @Test
  public void testParseVersion() throws IOException {
    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(
            new InputStreamReader(getClass().getResourceAsStream("dependencies/user_agents.csv")));

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      line = line.trim();

      if (line.isEmpty() || (line.charAt(0) == CharPool.POUND)) {
        continue;
      }

      String[] parts = StringUtil.split(line, CharPool.COMMA);

      if (parts.length != 4) {
        continue;
      }

      String userAgent = parts[3].trim();

      Assert.assertEquals(
          parts[0].trim() + " version",
          parts[1].trim(),
          BrowserSnifferImpl.parseVersion(
              userAgent, BrowserSnifferImpl.versionLeadings, BrowserSnifferImpl.versionSeparators));

      Assert.assertEquals(
          parts[0].trim() + " revision",
          parts[2].trim(),
          BrowserSnifferImpl.parseVersion(
              userAgent,
              BrowserSnifferImpl.revisionLeadings,
              BrowserSnifferImpl.revisionSeparators));
    }

    unsyncBufferedReader.close();
  }
  public String format(String imports) throws IOException {
    if (imports.contains("/*") || imports.contains("*/") || imports.contains("\n//")) {

      return imports + "\n";
    }

    Set<ImportPackage> importPackages = new TreeSet<>();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(imports));

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      ImportPackage importPackage = createImportPackage(line);

      if (importPackage != null) {
        importPackages.add(importPackage);
      }
    }

    StringBundler sb = new StringBundler(3 * importPackages.size());

    ImportPackage previousImportPackage = null;

    for (ImportPackage importPackage : importPackages) {
      if ((previousImportPackage != null) && !importPackage.isGroupedWith(previousImportPackage)) {

        sb.append("\n");
      }

      sb.append(importPackage.getLine());
      sb.append("\n");

      previousImportPackage = importPackage;
    }

    return sb.toString();
  }
  protected void formatPortalProperties(
      String fileName, String content, String portalPortalPropertiesContent) throws IOException {

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));

    int lineCount = 0;

    String line = null;

    int previousPos = -1;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      lineCount++;

      int pos = line.indexOf(StringPool.EQUAL);

      if (pos == -1) {
        continue;
      }

      String property = line.substring(0, pos + 1);

      property = property.trim();

      pos = portalPortalPropertiesContent.indexOf(StringPool.FOUR_SPACES + property);

      if (pos == -1) {
        continue;
      }

      if (pos < previousPos) {
        processErrorMessage(fileName, "sort " + fileName + " " + lineCount);
      }

      previousPos = pos;
    }
  }
Beispiel #16
0
  protected String transform(String sql) {
    sql = PortalUtil.transformCustomSQL(sql);

    StringBundler sb = new StringBundler();

    try {
      UnsyncBufferedReader unsyncBufferedReader =
          new UnsyncBufferedReader(new UnsyncStringReader(sql));

      String line = null;

      while ((line = unsyncBufferedReader.readLine()) != null) {
        sb.append(line.trim());
        sb.append(StringPool.SPACE);
      }

      unsyncBufferedReader.close();
    } catch (IOException ioe) {
      return sql;
    }

    return sb.toString();
  }
  protected String trimContent(String content, boolean allowLeadingSpaces) throws IOException {

    StringBundler sb = new StringBundler();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      sb.append(trimLine(line, allowLeadingSpaces));
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    content = sb.toString();

    if (content.endsWith("\n")) {
      content = content.substring(0, content.length() - 1);
    }

    return content;
  }
  public String getNormalizedContent(String fileName) throws Exception {
    String content = readFile(fileName);

    if (fileName.endsWith(".path")) {
      int x = content.indexOf("<tbody>");
      int y = content.indexOf("</tbody>");

      if ((x == -1) || (y == -1)) {
        throwValidationException(1002, fileName, "tbody");
      }

      String pathTbody = content.substring(x, y + 8);

      Map<String, Object> context = new HashMap<String, Object>();

      context.put("pathName", getName(fileName));
      context.put("pathTbody", pathTbody);

      String newContent = processTemplate("path_xml.ftl", context);

      if (!content.equals(newContent)) {
        content = newContent;

        writeFile(getBaseDir(), fileName, newContent, false);
      }
    }

    StringBundler sb = new StringBundler();

    int lineNumber = 1;

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      Pattern pattern = Pattern.compile("<[a-z\\-]+");

      Matcher matcher = pattern.matcher(line);

      if (matcher.find()) {
        for (String reservedTag : _reservedTags) {
          if (line.contains("<" + reservedTag)) {
            line =
                StringUtil.replace(
                    line, matcher.group(), matcher.group() + " line-number=\"" + lineNumber + "\"");

            break;
          }
        }
      }

      sb.append(line);

      lineNumber++;
    }

    content = sb.toString();

    if (content != null) {
      content = content.trim();
      content = StringUtil.replace(content, "\n", "");
      content = StringUtil.replace(content, "\r\n", "");
      content = StringUtil.replace(content, "\t", " ");
      content = content.replaceAll(" +", " ");
    }

    return content;
  }
  @Override
  public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

    long orderId = ParamUtil.getLong(request, PaypalConstants.PARAM_INVOICE);
    String status = ParamUtil.getString(request, PaypalConstants.PAYMENT_STATUS);

    LOG.info(String.format(LOG_NOTIFICATION, orderId, status));

    ShoppingOrder order = ShoppingOrderLocalServiceUtil.fetchShoppingOrder(orderId);
    List<ShoppingOrderItem> items = ShoppingOrderItemLocalServiceUtil.findByOrderId(orderId);

    if (items.isEmpty()) {
      LOG.error(String.format(LOG_UNKNOWN_ORDER, orderId));
      return null;
    }

    List<String> itemsIds = new ArrayList<String>();
    for (ShoppingOrderItem item : items) {
      itemsIds.add(Long.toString(item.getItemId()));
    }

    String query = PaypalConstants.PARAM_CMD + "=" + PaypalConstants.CMD_VALIDATE;

    Enumeration<String> enu = request.getParameterNames();

    while (enu.hasMoreElements()) {
      String name = enu.nextElement();
      String value = request.getParameter(name);
      if (LOG.isDebugEnabled()) LOG.debug(String.format(LOG_DEBUG_PARAM, name, value));
      query = query + "&" + name + "=" + HttpUtil.encodeURL(value);
    }

    if (LOG.isDebugEnabled()) LOG.debug(String.format(LOG_DEBUG_QUERY, query));

    URL url = new URL(PaypalConstants.PAYPAL_ENDPOINT);

    URLConnection urlc = url.openConnection();

    urlc.setDoOutput(true);
    urlc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    PrintWriter pw = UnsyncPrintWriterPool.borrow(urlc.getOutputStream());

    pw.println(query);

    pw.close();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new InputStreamReader(urlc.getInputStream()));

    String payPalStatus = unsyncBufferedReader.readLine();

    unsyncBufferedReader.close();

    LOG.info(String.format(LOG_STATUS_RESPONSE, payPalStatus));

    if (payPalStatus.equals(PaypalConstants.TRANSACTION_VERIFIED)
        && status.equals(PaypalConstants.PAYMENT_COMPLETE)) {

      LOG.info(LOG_TRX_VERIFIED);

      order.setOrderStatus(OrderStatusEnum.PAID.toString());
      ShoppingOrderLocalServiceUtil.updateOrder(order);

      EmailNotificationUtil.sendEmailNotification(orderId);

    } else {
      LOG.error(LOG_TRX_ERROR);
    }

    return "/portal/paypal.jsp";
  }
  protected String formatJSP(String fileName, String absolutePath, String content)
      throws IOException {

    StringBundler sb = new StringBundler();

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));

    int lineCount = 0;

    String line = null;

    String previousLine = StringPool.BLANK;

    String currentAttributeAndValue = null;
    String previousAttribute = null;
    String previousAttributeAndValue = null;

    boolean readAttributes = false;

    String currentException = null;
    String previousException = null;

    boolean hasUnsortedExceptions = false;

    boolean javaSource = false;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      lineCount++;

      if (!fileName.contains("jsonw") || !fileName.endsWith("action.jsp")) {

        line = trimLine(line, false);
      }

      if (line.contains("<aui:button ") && line.contains("type=\"button\"")) {

        processErrorMessage(fileName, "aui:button " + fileName + " " + lineCount);
      }

      if (line.contains("debugger.")) {
        processErrorMessage(fileName, "debugger " + fileName + " " + lineCount);
      }

      String trimmedLine = StringUtil.trimLeading(line);
      String trimmedPreviousLine = StringUtil.trimLeading(previousLine);

      checkStringBundler(trimmedLine, fileName, lineCount);

      checkEmptyCollection(trimmedLine, fileName, lineCount);

      if (trimmedLine.equals("<%") || trimmedLine.equals("<%!")) {
        javaSource = true;
      } else if (trimmedLine.equals("%>")) {
        javaSource = false;
      }

      if (javaSource || trimmedLine.contains("<%= ")) {
        checkInefficientStringMethods(line, fileName, absolutePath, lineCount);
      }

      if (javaSource
          && portalSource
          && !isExcluded(_unusedVariablesExclusions, fileName, lineCount)
          && !_jspContents.isEmpty()
          && hasUnusedVariable(fileName, trimmedLine)) {

        processErrorMessage(fileName, "Unused variable: " + fileName + " " + lineCount);
      }

      if (!trimmedLine.equals("%>")
          && line.contains("%>")
          && !line.contains("--%>")
          && !line.contains(" %>")) {

        line = StringUtil.replace(line, "%>", " %>");
      }

      if (line.contains("<%=") && !line.contains("<%= ")) {
        line = StringUtil.replace(line, "<%=", "<%= ");
      }

      if (trimmedPreviousLine.equals("%>")
          && Validator.isNotNull(line)
          && !trimmedLine.equals("-->")) {

        sb.append("\n");
      } else if (Validator.isNotNull(previousLine)
          && !trimmedPreviousLine.equals("<!--")
          && trimmedLine.equals("<%")) {

        sb.append("\n");
      } else if (trimmedPreviousLine.equals("<%") && Validator.isNull(line)) {

        continue;
      } else if (trimmedPreviousLine.equals("<%") && trimmedLine.startsWith("//")) {

        sb.append("\n");
      } else if (Validator.isNull(previousLine) && trimmedLine.equals("%>") && (sb.index() > 2)) {

        String lineBeforePreviousLine = sb.stringAt(sb.index() - 3);

        if (!lineBeforePreviousLine.startsWith("//")) {
          sb.setIndex(sb.index() - 1);
        }
      }

      if ((trimmedLine.startsWith("if (")
              || trimmedLine.startsWith("else if (")
              || trimmedLine.startsWith("while ("))
          && trimmedLine.endsWith(") {")) {

        checkIfClauseParentheses(trimmedLine, fileName, lineCount);
      }

      if (readAttributes) {
        if (!trimmedLine.startsWith(StringPool.FORWARD_SLASH)
            && !trimmedLine.startsWith(StringPool.GREATER_THAN)) {

          int pos = trimmedLine.indexOf(StringPool.EQUAL);

          if (pos != -1) {
            String attribute = trimmedLine.substring(0, pos);

            if (!trimmedLine.endsWith(StringPool.APOSTROPHE)
                && !trimmedLine.endsWith(StringPool.GREATER_THAN)
                && !trimmedLine.endsWith(StringPool.QUOTE)) {

              processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount);

              readAttributes = false;
            } else if (trimmedLine.endsWith(StringPool.APOSTROPHE)
                && !trimmedLine.contains(StringPool.QUOTE)) {

              line = StringUtil.replace(line, StringPool.APOSTROPHE, StringPool.QUOTE);

              readAttributes = false;
            } else if (Validator.isNotNull(previousAttribute)) {
              if (!isAttributName(attribute) && !attribute.startsWith(StringPool.LESS_THAN)) {

                processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount);

                readAttributes = false;
              } else if (Validator.isNull(previousAttributeAndValue)
                  && (previousAttribute.compareTo(attribute) > 0)) {

                previousAttributeAndValue = previousLine;
                currentAttributeAndValue = line;
              }
            }

            if (!readAttributes) {
              previousAttribute = null;
              previousAttributeAndValue = null;
            } else {
              previousAttribute = attribute;
            }
          }
        } else {
          previousAttribute = null;

          readAttributes = false;
        }
      }

      if (!hasUnsortedExceptions) {
        int i = line.indexOf("<liferay-ui:error exception=\"<%=");

        if (i != -1) {
          currentException = line.substring(i + 33);

          if (Validator.isNotNull(previousException)
              && (previousException.compareTo(currentException) > 0)) {

            hasUnsortedExceptions = true;
          }
        }

        if (!hasUnsortedExceptions) {
          previousException = currentException;
          currentException = null;
        }
      }

      if (trimmedLine.startsWith(StringPool.LESS_THAN)
          && !trimmedLine.startsWith("<%")
          && !trimmedLine.startsWith("<!")) {

        if (!trimmedLine.contains(StringPool.GREATER_THAN)
            && !trimmedLine.contains(StringPool.SPACE)) {

          readAttributes = true;
        } else {
          line = sortAttributes(fileName, line, lineCount, true);
        }
      }

      if (!trimmedLine.contains(StringPool.DOUBLE_SLASH)
          && !trimmedLine.startsWith(StringPool.STAR)) {

        while (trimmedLine.contains(StringPool.TAB)) {
          line = StringUtil.replaceLast(line, StringPool.TAB, StringPool.SPACE);

          trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.TAB, StringPool.SPACE);
        }

        while (trimmedLine.contains(StringPool.DOUBLE_SPACE)
            && !trimmedLine.contains(StringPool.QUOTE + StringPool.DOUBLE_SPACE)
            && !fileName.endsWith(".vm")) {

          line = StringUtil.replaceLast(line, StringPool.DOUBLE_SPACE, StringPool.SPACE);

          trimmedLine =
              StringUtil.replaceLast(trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE);
        }
      }

      if (!fileName.endsWith("/touch.jsp")) {
        int x = line.indexOf("<%@ include file");

        if (x != -1) {
          x = line.indexOf(StringPool.QUOTE, x);

          int y = line.indexOf(StringPool.QUOTE, x + 1);

          if (y != -1) {
            String includeFileName = line.substring(x + 1, y);

            Matcher matcher = _jspIncludeFilePattern.matcher(includeFileName);

            if (!matcher.find()) {
              processErrorMessage(fileName, "include: " + fileName + " " + lineCount);
            }
          }
        }
      }

      line = replacePrimitiveWrapperInstantiation(fileName, line, lineCount);

      previousLine = line;

      sb.append(line);
      sb.append("\n");
    }

    unsyncBufferedReader.close();

    content = sb.toString();

    if (content.endsWith("\n")) {
      content = content.substring(0, content.length() - 1);
    }

    content = formatTaglibQuotes(fileName, content, StringPool.QUOTE);
    content = formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE);

    if (Validator.isNotNull(previousAttributeAndValue)) {
      content =
          StringUtil.replaceFirst(
              content,
              previousAttributeAndValue + "\n" + currentAttributeAndValue,
              currentAttributeAndValue + "\n" + previousAttributeAndValue);
    }

    if (hasUnsortedExceptions) {
      if ((StringUtil.count(content, currentException) > 1)
          || (StringUtil.count(content, previousException) > 1)) {

        processErrorMessage(fileName, "unsorted exceptions: " + fileName);
      } else {
        content = StringUtil.replaceFirst(content, previousException, currentException);

        content = StringUtil.replaceLast(content, currentException, previousException);
      }
    }

    return content;
  }
  protected String stripJSPImports(String fileName, String content) throws IOException {

    fileName = fileName.replace(CharPool.BACK_SLASH, CharPool.FORWARD_SLASH);

    if (!fileName.contains("docroot") || fileName.endsWith("init-ext.jsp")) {

      return content;
    }

    Matcher matcher = _jspImportPattern.matcher(content);

    if (!matcher.find()) {
      return content;
    }

    String imports = matcher.group();

    imports =
        StringUtil.replace(
            imports,
            new String[] {"%><%@\r\n", "%><%@\n"},
            new String[] {"%>\r\n<%@ ", "%>\n<%@ "});

    if (!fileName.endsWith("html/common/init.jsp") && !fileName.endsWith("html/portal/init.jsp")) {

      List<String> importLines = new ArrayList<String>();

      UnsyncBufferedReader unsyncBufferedReader =
          new UnsyncBufferedReader(new UnsyncStringReader(imports));

      String line = null;

      while ((line = unsyncBufferedReader.readLine()) != null) {
        if (line.contains("import=")) {
          importLines.add(line);
        }
      }

      List<String> unneededImports = getJSPDuplicateImports(fileName, content, importLines);

      addJSPUnusedImports(fileName, importLines, unneededImports);

      for (String unneededImport : unneededImports) {
        imports = StringUtil.replace(imports, unneededImport, StringPool.BLANK);
      }
    }

    ImportsFormatter importsFormatter = new JSPImportsFormatter();

    imports = importsFormatter.format(imports);

    String beforeImports = content.substring(0, matcher.start());

    if (Validator.isNull(imports)) {
      beforeImports = StringUtil.replaceLast(beforeImports, "\n", StringPool.BLANK);
    }

    String afterImports = content.substring(matcher.end());

    if (Validator.isNull(afterImports)) {
      imports = StringUtil.replaceLast(imports, "\n", StringPool.BLANK);

      content = beforeImports + imports;

      return content;
    }

    content = beforeImports + imports + "\n" + afterImports;

    return content;
  }
Beispiel #22
0
  private void _createProperties(String content, String languageId, String parentLanguageId)
      throws IOException {

    File propertiesFile = new File(_langDir + "/" + _langFile + "_" + languageId + ".properties");

    Properties properties = new Properties();

    if (propertiesFile.exists()) {
      properties = PropertiesUtil.load(new FileInputStream(propertiesFile), StringPool.UTF8);
    }

    Properties parentProperties = null;

    if (parentLanguageId != null) {
      File parentPropertiesFile =
          new File(_langDir + "/" + _langFile + "_" + parentLanguageId + ".properties");

      if (parentPropertiesFile.exists()) {
        parentProperties = new Properties();

        parentProperties =
            PropertiesUtil.load(new FileInputStream(parentPropertiesFile), StringPool.UTF8);
      }
    }

    String translationId = "en_" + languageId;

    if (translationId.equals("en_pt_BR")) {
      translationId = "en_pt";
    } else if (translationId.equals("en_pt_PT")) {
      translationId = "en_pt";
    } else if (translationId.equals("en_zh_CN")) {
      translationId = "en_zh";
    } else if (translationId.equals("en_zh_TW")) {
      translationId = "en_zt";
    } else if (translationId.equals("en_hi_IN")) {
      translationId = "en_hi";
    }

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));
    UnsyncBufferedWriter unsyncBufferedWriter =
        new UnsyncBufferedWriter(
            new OutputStreamWriter(new FileOutputStream(propertiesFile), StringPool.UTF8));

    int state = 0;

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      line = line.trim();

      int pos = line.indexOf("=");

      if (pos != -1) {
        String key = line.substring(0, pos);
        String value = line.substring(pos + 1, line.length());

        if (((state == 1) && !key.startsWith("lang."))
            || ((state == 2) && !key.startsWith("javax.portlet."))
            || ((state == 3) && !key.startsWith("category."))
            || ((state == 4) && !key.startsWith("model.resource."))
            || ((state == 5) && !key.startsWith("action."))
            || ((state == 7) && !key.startsWith("currency."))
            || ((state != 7) && key.startsWith("currency."))) {

          throw new RuntimeException(
              "File " + languageId + " with state " + state + " has key " + key);
        }

        String translatedText = properties.getProperty(key);

        if ((translatedText == null) && (parentProperties != null)) {
          translatedText = parentProperties.getProperty(key);
        }

        if ((translatedText == null) && (_renameKeys != null)) {
          String renameKey = _renameKeys.getProperty(key);

          if (renameKey != null) {
            translatedText = properties.getProperty(key);

            if ((translatedText == null) && (parentProperties != null)) {

              translatedText = parentProperties.getProperty(key);
            }
          }
        }

        if (translatedText != null) {
          if (translatedText.contains("Babel Fish") || translatedText.contains("Yahoo! - 999")) {

            translatedText = "";
          } else if (translatedText.endsWith(AUTOMATIC_COPY)) {
            translatedText = value + AUTOMATIC_COPY;
          }
        }

        if ((translatedText == null) || translatedText.equals("")) {
          if (line.contains("{") || line.contains("<")) {
            translatedText = value + AUTOMATIC_COPY;
          } else if (line.contains("[")) {
            pos = line.indexOf("[");

            String baseKey = line.substring(0, pos);

            translatedText = properties.getProperty(baseKey) + AUTOMATIC_COPY;
          } else if (key.equals("lang.dir")) {
            translatedText = "ltr";
          } else if (key.equals("lang.line.begin")) {
            translatedText = "left";
          } else if (key.equals("lang.line.end")) {
            translatedText = "right";
          } else if (translationId.equals("en_el")
              && (key.equals("enabled") || key.equals("on") || key.equals("on-date"))) {

            translatedText = "";
          } else if (translationId.equals("en_es") && key.equals("am")) {

            translatedText = "";
          } else if (translationId.equals("en_it") && key.equals("am")) {

            translatedText = "";
          } else if (translationId.equals("en_ja")
              && (key.equals("any")
                  || key.equals("anytime")
                  || key.equals("down")
                  || key.equals("on")
                  || key.equals("on-date")
                  || key.equals("the"))) {

            translatedText = "";
          } else if (translationId.equals("en_ko") && key.equals("the")) {

            translatedText = "";
          } else {
            translatedText = _translate(translationId, key, value, 0);

            if (Validator.isNull(translatedText)) {
              translatedText = value + AUTOMATIC_COPY;
            } else {
              translatedText = translatedText + AUTOMATIC_TRANSLATION;
            }
          }
        }

        if (Validator.isNotNull(translatedText)) {
          if (translatedText.contains("Babel Fish") || translatedText.contains("Yahoo! - 999")) {

            throw new IOException(
                "IP was blocked because of over usage. Please " + "use another IP.");
          }

          translatedText = _fixTranslation(translatedText);

          unsyncBufferedWriter.write(key + "=" + translatedText);

          unsyncBufferedWriter.newLine();
          unsyncBufferedWriter.flush();
        }
      } else {
        if (line.startsWith("## Language settings")) {
          if (state == 1) {
            throw new RuntimeException(languageId);
          }

          state = 1;
        } else if (line.startsWith("## Portlet descriptions and titles")) {

          if (state == 2) {
            throw new RuntimeException(languageId);
          }

          state = 2;
        } else if (line.startsWith("## Category titles")) {
          if (state == 3) {
            throw new RuntimeException(languageId);
          }

          state = 3;
        } else if (line.startsWith("## Model resources")) {
          if (state == 4) {
            throw new RuntimeException(languageId);
          }

          state = 4;
        } else if (line.startsWith("## Action names")) {
          if (state == 5) {
            throw new RuntimeException(languageId);
          }

          state = 5;
        } else if (line.startsWith("## Messages")) {
          if (state == 6) {
            throw new RuntimeException(languageId);
          }

          state = 6;
        } else if (line.startsWith("## Currency")) {
          if (state == 7) {
            throw new RuntimeException(languageId);
          }

          state = 7;
        }

        unsyncBufferedWriter.write(line);

        unsyncBufferedWriter.newLine();
        unsyncBufferedWriter.flush();
      }
    }

    unsyncBufferedReader.close();
    unsyncBufferedWriter.close();
  }
Beispiel #23
0
  private String _orderProperties(File propertiesFile) throws IOException {
    if (!propertiesFile.exists()) {
      return null;
    }

    String content = FileUtil.read(propertiesFile);

    UnsyncBufferedReader unsyncBufferedReader =
        new UnsyncBufferedReader(new UnsyncStringReader(content));
    UnsyncBufferedWriter unsyncBufferedWriter =
        new UnsyncBufferedWriter(new FileWriter(propertiesFile));

    Set<String> messages = new TreeSet<String>();

    boolean begin = false;

    String line = null;

    while ((line = unsyncBufferedReader.readLine()) != null) {
      int pos = line.indexOf("=");

      if (pos != -1) {
        String key = line.substring(0, pos);

        String value = _fixTranslation(line.substring(pos + 1, line.length()));

        value = _fixEnglishTranslation(key, value);

        if (_portalLanguageProperties != null) {
          String portalValue = String.valueOf(_portalLanguageProperties.get(key));

          if (value.equals(portalValue)) {
            System.out.println("Duplicate key " + key);
          }
        }

        messages.add(key + "=" + value);
      } else {
        if (begin == true && line.equals("")) {
          _sortAndWrite(unsyncBufferedWriter, messages);
        }

        if (line.equals("")) {
          begin = !begin;
        }

        unsyncBufferedWriter.write(line);
        unsyncBufferedWriter.newLine();
      }

      unsyncBufferedWriter.flush();
    }

    if (messages.size() > 0) {
      _sortAndWrite(unsyncBufferedWriter, messages);
    }

    unsyncBufferedReader.close();
    unsyncBufferedWriter.close();

    return FileUtil.read(propertiesFile);
  }
  protected List<ObjectValuePair<String, IndexMetadata>> getIndexesSQL(
      ClassLoader classLoader, String tableName) throws IOException {

    if (!PortalClassLoaderUtil.isPortalClassLoader(classLoader)) {
      List<ObjectValuePair<String, IndexMetadata>> objectValuePairs = new ArrayList<>();

      try (InputStream is = classLoader.getResourceAsStream("META-INF/sql/indexes.sql");
          Reader reader = new InputStreamReader(is);
          UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(reader)) {

        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
          line = line.trim();

          if (line.isEmpty()) {
            continue;
          }

          IndexMetadata indexMetadata = IndexMetadataFactoryUtil.createIndexMetadata(line);

          if (tableName.equals(indexMetadata.getTableName())) {
            objectValuePairs.add(new ObjectValuePair<>(line, indexMetadata));
          }
        }
      }

      return objectValuePairs;
    }

    if (!_portalIndexesSQL.isEmpty()) {
      return _portalIndexesSQL.get(tableName);
    }

    try (InputStream is =
            classLoader.getResourceAsStream(
                "com/liferay/portal/tools/sql/dependencies/indexes.sql");
        Reader reader = new InputStreamReader(is);
        UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(reader)) {

      String line = null;

      while ((line = unsyncBufferedReader.readLine()) != null) {
        line = line.trim();

        if (line.isEmpty()) {
          continue;
        }

        IndexMetadata indexMetadata = IndexMetadataFactoryUtil.createIndexMetadata(line);

        List<ObjectValuePair<String, IndexMetadata>> objectValuePairs =
            _portalIndexesSQL.get(indexMetadata.getTableName());

        if (objectValuePairs == null) {
          objectValuePairs = new ArrayList<>();

          _portalIndexesSQL.put(indexMetadata.getTableName(), objectValuePairs);
        }

        objectValuePairs.add(new ObjectValuePair<>(line, indexMetadata));
      }
    }

    return _portalIndexesSQL.get(tableName);
  }
Beispiel #25
0
  private String[] _getCookieNames(String serviceUrl) {
    String[] cookieNames = _cookieNamesMap.get(serviceUrl);

    if (cookieNames != null) {
      return cookieNames;
    }

    List<String> cookieNamesList = new ArrayList<String>();

    try {
      String cookieName = null;

      String url = serviceUrl.concat(_GET_COOKIE_NAME);

      URL urlObj = new URL(url);

      HttpURLConnection httpURLConnection = (HttpURLConnection) urlObj.openConnection();

      InputStream inputStream = (InputStream) httpURLConnection.getContent();

      UnsyncBufferedReader unsyncBufferedReader =
          new UnsyncBufferedReader(new InputStreamReader(inputStream));

      int responseCode = httpURLConnection.getResponseCode();

      if (responseCode != HttpURLConnection.HTTP_OK) {
        if (_log.isDebugEnabled()) {
          _log.debug(url + " has response code " + responseCode);
        }
      } else {
        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
          if (line.startsWith("string=")) {
            line = line.replaceFirst("string=", "");

            cookieName = line;
          }
        }
      }

      url = serviceUrl.concat(_GET_COOKIE_NAMES);

      urlObj = new URL(url);

      httpURLConnection = (HttpURLConnection) urlObj.openConnection();

      inputStream = (InputStream) httpURLConnection.getContent();

      unsyncBufferedReader = new UnsyncBufferedReader(new InputStreamReader(inputStream));

      if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {

        if (_log.isDebugEnabled()) {
          _log.debug(url + " has response code " + responseCode);
        }
      } else {
        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
          if (line.startsWith("string=")) {
            line = line.replaceFirst("string=", "");

            if (cookieName.equals(line)) {
              cookieNamesList.add(0, cookieName);
            } else {
              cookieNamesList.add(line);
            }
          }
        }
      }
    } catch (IOException ioe) {
      if (_log.isWarnEnabled()) {
        _log.warn(ioe, ioe);
      }
    }

    cookieNames = cookieNamesList.toArray(new String[cookieNamesList.size()]);

    if (cookieNames.length > 0) {
      _cookieNamesMap.put(serviceUrl, cookieNames);
    }

    return cookieNames;
  }
Beispiel #26
0
  private Map<String, String> _getAttributes(HttpServletRequest request, String serviceUrl) {

    Map<String, String> nameValues = new HashMap<String, String>();

    String url = serviceUrl.concat(_GET_ATTRIBUTES);

    try {
      URL urlObj = new URL(url);

      HttpURLConnection httpURLConnection = (HttpURLConnection) urlObj.openConnection();

      httpURLConnection.setDoOutput(true);
      httpURLConnection.setRequestMethod("POST");
      httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

      String[] cookieNames = _getCookieNames(serviceUrl);

      _setCookieProperty(request, httpURLConnection, cookieNames);

      OutputStreamWriter osw = new OutputStreamWriter(httpURLConnection.getOutputStream());

      osw.write("dummy");

      osw.flush();

      int responseCode = httpURLConnection.getResponseCode();

      if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = (InputStream) httpURLConnection.getContent();

        UnsyncBufferedReader unsyncBufferedReader =
            new UnsyncBufferedReader(new InputStreamReader(inputStream));

        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
          if (line.startsWith("userdetails.attribute.name=")) {
            String name = line.replaceFirst("userdetails.attribute.name=", "");

            line = unsyncBufferedReader.readLine();

            if (line.startsWith("userdetails.attribute.value=")) {
              String value = line.replaceFirst("userdetails.attribute.value=", "");

              nameValues.put(name, value);
            }
          }
        }
      } else if (_log.isDebugEnabled()) {
        _log.debug("Attributes response code " + responseCode);
      }
    } catch (MalformedURLException mfue) {
      _log.error(mfue.getMessage());

      if (_log.isDebugEnabled()) {
        _log.debug(mfue, mfue);
      }
    } catch (IOException ioe) {
      _log.error(ioe.getMessage());

      if (_log.isDebugEnabled()) {
        _log.debug(ioe, ioe);
      }
    }

    return nameValues;
  }