private static void formatPlexTalkAttributesOrder(File file, String encoding) throws IOException {

    try {
      BufferedReader br =
          new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));

      File tempOutputFile = new File(file.getAbsolutePath() + "__tmp");
      BufferedWriter bw =
          new BufferedWriter(
              new OutputStreamWriter(new FileOutputStream(tempOutputFile), encoding));

      String line = br.readLine();
      StringBuffer elemStringBuffer = new StringBuffer();

      boolean elementComplete = false;
      boolean buildingElement = false;
      while (line != null) {
        String trimmedLine = line.trim();
        // 1. RETRIEVE A meta OR audio ELEMENT
        if ((trimmedLine.startsWith("<meta") || trimmedLine.startsWith("<audio"))
            && trimmedLine.endsWith("/>")) {
          // a line with a complete meta or audio element
          elemStringBuffer.append(line);
          elementComplete = true;
        } else if ((trimmedLine.startsWith("<meta") || trimmedLine.startsWith("<audio"))
            && !trimmedLine.endsWith("/>")) {
          // a line that starts meta or audio element
          elemStringBuffer.append(line + " ");
          buildingElement = true;
          line = br.readLine();
          continue;

        } else if (buildingElement) {
          elemStringBuffer.append(trimmedLine);
          if (trimmedLine.endsWith("/>")) {
            // a line that ends meta or audio element
            elementComplete = true;
            buildingElement = false;
          } else {
            // a line with a mid part of meta or audio element
            elemStringBuffer.append(" ");
            line = br.readLine();
            continue;
          }
        }
        // 2. MOVE THE audio/@src OR meta/@name ATTRIBUTE TO THE FIRST POSITION
        if (elementComplete) {
          int srcAttStart = elemStringBuffer.indexOf("src=");
          int nameAttrStart = elemStringBuffer.indexOf("name=");
          int attrStart = -1;
          if (srcAttStart != -1) {
            attrStart = srcAttStart;
          } else if (nameAttrStart != -1) {
            attrStart = nameAttrStart;
          }
          if (attrStart != -1) {

            String attrElemTrail = elemStringBuffer.substring(attrStart);
            int attrLength = attrElemTrail.indexOf(" ");
            if (attrLength == -1) {
              attrLength = attrElemTrail.indexOf("/>");
            }
            String attr = elemStringBuffer.substring(attrStart, attrStart + attrLength);

            elemStringBuffer.delete(attrStart, attrStart + attrLength);

            int newAttrstart = 0;
            int metaIndex = elemStringBuffer.indexOf("<meta");
            int audioIndex = elemStringBuffer.indexOf("<audio");
            if (metaIndex != -1) {
              newAttrstart = metaIndex + "<meta".length() + 1;
            } else if (audioIndex != -1) {
              newAttrstart = audioIndex + "<audio".length() + 1;
            }
            elemStringBuffer.insert(newAttrstart, attr + " ");
          }
          String element = elemStringBuffer.toString();
          bw.write(element + "\r\n");

          elemStringBuffer.setLength(0);
          elementComplete = false;
        } else if (!buildingElement) {
          bw.write(line + "\r\n");
        }

        line = br.readLine();
      } // move to the next line

      br.close();
      bw.flush();
      bw.close();
      FileUtils.copy(tempOutputFile, file);
      tempOutputFile.delete();
    } catch (IOException ioe) {
      throw ioe;
    }
  }
  private static void formatNavPointsLineBreaks(File navigationFile, String encoding)
      throws IOException {

    BufferedReader br =
        new BufferedReader(new InputStreamReader(new FileInputStream(navigationFile), encoding));

    File tmpOutputFile = new File(navigationFile.getAbsolutePath() + "_tmp");
    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpOutputFile), encoding));

    String line = br.readLine();

    StringBuffer sBuffer = new StringBuffer();
    if (FilesetRegex.getInstance()
        .matches(FilesetRegex.getInstance().FILE_NCC, navigationFile.getName())) {
      // handle ncc
      boolean isInsideNavPoint = false;
      while (line != null) {
        String l = line.toLowerCase();

        if ((l.indexOf("<h") != -1 && (l.indexOf("<html") == -1) && l.indexOf("<head") == -1)
            || l.indexOf("<span") != -1) {
          isInsideNavPoint = true;
        } else if (l.indexOf("</h") != -1 || l.indexOf("</span") != -1) {
          isInsideNavPoint = false;
        }

        if (isInsideNavPoint) {
          sBuffer.append(line.trim());
          if (!l.trim().endsWith(">")) {
            sBuffer.append(" ");
          }
        } else {
          sBuffer.append(line.trim());
          sBuffer.append("\r\n");
          bw.write(sBuffer.toString());
          sBuffer.setLength(0);
        }
        line = br.readLine();
      } // move to the next line in the ncc

    } else {
      // handle non ncc documents
      while (line != null) {
        sBuffer.append(line);
        sBuffer.append("\r\n");

        line = br.readLine();
        bw.write(sBuffer.toString());
        sBuffer.setLength(0);
      } // move to the next line in the full text
    }

    br.close();
    // BufferedWriter bw = new BufferedWriter(new FileWriter(navigationFile));
    // bw.write(sBuffer.toString());
    bw.flush();
    bw.close();
    FileUtils.copy(tmpOutputFile, navigationFile);
    tmpOutputFile.delete();
  }