⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader.java

📁 liu7788414
💻 JAVA
字号:
package cameracontrol;

/**
 * <p>Title: Reader.java</p>
 * <p>Description: Class for read and convert xml file</p>
 * <p>Copyright: Gameloft 2004</p>
 * <p>Company: Gameloft Shanghai</p>
 * @author Yi Wen Hu
 * @version 1.0
 */

import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.util.Vector;
import java.io.*;

public class Reader {
  public Element m_root;
  public CamComb[] m_camCombs;

  // xml nodes name
  public static final String XML_CAMERACOMBINATION = "CameraCombination";
  public static final String XML_CAMERACOMB_NAME = "Name";
  public static final String XML_STAGE = "Stage";
  public static final String XML_START = "Start";
  public static final String XML_END = "End";

  // stage's attributes name
  public static final String XML_SG_CONTROLTYPE = "controlType";
  public static final String XML_SG_TOTALTIME = "totalTime";
  public static final String XML_SG_POSITIONREFER = "posRef";
  public static final String XML_SG_POSITIONSTYLE = "posStyle";
  public static final String XML_SG_LOOKATREFER = "lookatRef";
  public static final String XML_SG_LOOKATSTYLE = "lookatStyle";

  // start or end's attributes name
  public static final String XML_FRM_POSITION = "pos";
  public static final String XML_FRM_LOOKAT = "lookat";
  public static final String XML_FRM_UP = "up";

  // control type
  public static final String[] XML_CONTROL_TYPE = {
      "TIME", "DISTANCE", "STATIC", "CONSTANT", "MENTAL"};

  // reference object
  public static final String[] XML_REFERENCE_OBJECT = {
      "BALL", "PLAYER", "SCENE", "CAMERA"};

  // coordinates define
  public static final String[] XML_COORD_DEFINE = {
      "XYZ", "AAD"};

  public static final String XML_DISTANCE_ENDDIST = "endDist";
  public static final String XML_DISTANCE_STARTDIST = "startDist";

  public Reader() {
  }

  public boolean open(String filename) {
    try {
      FileInputStream fin = new FileInputStream(filename);
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(fin);
      m_root = document.getDocumentElement();

      fin.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return true;
  }

  public void save(String filename) {
    try {
      FileOutputStream fos = new FileOutputStream(filename);
      int count = m_camCombs.length;
      fos.write(reverseInt(count));

      for (int i = 0; i < count; i++)
        m_camCombs[i].save(fos);

      fos.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void read() {
    if (m_root == null)return;

    int count = m_root.getElementsByTagName(XML_CAMERACOMBINATION).getLength();
    m_camCombs = new CamComb[count];

    for (int i = 0; i < count; i++) {
      m_camCombs[i] = new CamComb( (Element) m_root.getElementsByTagName(
          XML_CAMERACOMBINATION).item(i));
    }
  }

  public void writeHeadFile(String filename) {
    try {
      FileOutputStream fos = new FileOutputStream(filename);
      int count = m_camCombs.length;

      for(int i=0;i<count;i++)
      {
        String st;
        st=m_camCombs[i].m_name;
        String line="#define IDC_"+st.trim().toUpperCase()+" "+i+"\n";
        writeStringToHeader(line,fos);
      }

      fos.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void test() {
    System.out.println();
    System.out.println("READER.TEST()");
    int count = m_camCombs.length;
    for (int i = 0; i < count; i++)
      m_camCombs[i].test();
  }

  public static byte[] reverseInt(int t) {
    byte[] r = new byte[4];

    r[0] = (byte) (t & 0x000000ff);
    r[1] = (byte) ( (t & 0x0000ff00) >> 8);
    r[2] = (byte) ( (t & 0x00ff0000) >> 16);
    r[3] = (byte) ( (t & 0xff000000) >> 24);

    return r;
  }

  public static int getIndexOfString(String st, String[] starray)
  // if not found, return -1;
  // found, return index in array
  {
    int count = starray.length;
    for (int i = 0; i < count; i++) {
      if (st.compareToIgnoreCase(starray[i]) == 0)
        return i;
    }
    return -1;
  }

  public static void writeStringToHeader(String st, FileOutputStream fos) {

    byte[] data;
    try {
      data = convertString(st);
      fos.write(data);
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }

  private static byte[] convertString(String st) {
    byte[] result;
    int count;
    count = st.length();
//    result =new byte[count+1];
    result = new byte[count];

    for (int i = 0; i < count; i++)
      result[i] = (byte) st.charAt(i);

//    result[count]='\0';

    return result;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -