Package com.ngt.jopenmetaverse.shared.structureddata

Examples of com.ngt.jopenmetaverse.shared.structureddata.OSDException


  /// <returns></returns>
 
  private static OSD ParseLLSDXmlRoot(Node reader) throws OSDException, URISyntaxException
  {
    if (reader.getNodeType() != Node.ELEMENT_NODE || !reader.getNodeName().equals("llsd"))
      throw new OSDException("Expected an element llsd");
   
    NodeList nodeList = reader.getChildNodes();
    OSD ret = new OSD();
   
    for(int i = 0; i < nodeList.getLength(); i++)
View Full Code Here


 
 
  private static OSD ParseLLSDXmlElement(Node reader) throws OSDException, URISyntaxException
  {
    if (reader.getNodeType() != Node.ELEMENT_NODE)
      throw new OSDException("Expected an element");

    String type = reader.getNodeName();
    OSD ret;

    ////System.out.println("Parsing Element..." + type);
   
    if(type.equals("undef"))
    {
      if (!reader.hasChildNodes())
      {
        return new OSD();
      }
      ret = new OSD();
    }
    else if(type.equals("boolean"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromBoolean(false);
      }

      String s = reader.getFirstChild().getNodeValue().trim();

      if (s.equals("true") || s.equals("1"))
        ret = OSD.FromBoolean(true);
      else
        ret = OSD.FromBoolean(false);
    }
    else if(type.equals("integer"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromInteger(0);
      }

      String s = reader.getFirstChild().getNodeValue().trim();
      int[] result = new int[1];
      if(Utils.tryParseInt(s, result))
      {
        ret = OSD.FromInteger(result[0]);
      }
      else
        ret = OSD.FromInteger(0);
    }
    else if(type.equals("real"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromReal(0d);
      }

      double[] value = new double[]{0d};
      String s = reader.getFirstChild().getNodeValue().trim().toLowerCase();

      if (s.equals("nan"))
        value[0] = Double.NaN;
      else
        Utils.tryParseDouble(s, value);

      ret = OSD.FromReal(value[0]);
    }
    else if(type.equals("uuid"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromUUID(UUID.Zero);
      }

      UUID[] value = new UUID[]{UUID.Zero};
      String s = reader.getFirstChild().getNodeValue().trim();
      UUID.TryParse(s, value);
      ret = OSD.FromUUID(value[0]);
    }
    else if(type.equals("date"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromDate(Utils.Epoch);
      }
      Date[] value = new Date[]{Utils.Epoch};
      String s = reader.getFirstChild().getNodeValue().trim();
      Utils.tryParseDate(s, value);
      ret = OSD.FromDate(value[0]);
    }
    else if(type.equals("string"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromString("");
      }

      String s = reader.getFirstChild().getNodeValue().trim();
      ret = OSD.FromString(s);

    }
    else if(type.equals("binary"))
    {
      if (!reader.hasChildNodes())
      {
        return OSD.FromBinary(Utils.EmptyBytes);
      }

      NamedNodeMap namedNodeMap = reader.getAttributes();
      Node attr = namedNodeMap.getNamedItem("encoding");
      if (attr != null && !attr.getNodeValue().equals("base64"))
        throw new OSDException("Unsupported binary encoding: " + attr.getNodeValue());
     
      try
      {
        String s = reader.getFirstChild().getNodeValue().trim();
        byte[] bytes = Utils.decodeBase64String(s);

        ret = OSD.FromBinary(bytes);
      }
      catch (Exception ex)
      {
        throw new OSDException("Binary decoding exception: " + ex.getMessage());
      }

    }
    else if(type.equals("uri"))
    {
View Full Code Here

  }

  private static OSDMap ParseLLSDXmlMap(Node reader) throws OSDException, URISyntaxException
  {
    if (reader.getNodeType() != Node.ELEMENT_NODE|| !reader.getNodeName().equals("map"))
      throw new OSDException("Expected <map>");

    ////System.out.println("Parsing Map...");
   
    OSDMap map = new OSDMap();

    if (!reader.hasChildNodes())
    {
      return map;
    }

    NodeList nodeList = reader.getChildNodes();
   
    List<Node> nl = new ArrayList<Node>();
   
    //Remove the Non Element Nodes
    for(int i = 0; i < nodeList.getLength(); i+=1)
    {
      if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE)
      {
        nl.add(nodeList.item(i));
      }
    }
   
    for(int i = 0; i < nl.size(); i+=2)
    {
      //System.out.println(i);
        Node keyNode = nl.get(i);
        Node valueNode = nl.get(i+1);
     
        if (keyNode == null || !keyNode.getNodeName().equals("key"))
          throw new OSDException("Expected <key>");
     
        if (valueNode == null)
          throw new OSDException("Expected value for a key");       
        //System.out.println(keyNode.getFirstChild().getNodeValue());
        String key = keyNode.getFirstChild().getNodeValue().trim();
        map.put(key, ParseLLSDXmlElement(valueNode));
    }
   
View Full Code Here

  }

  private static OSDArray ParseLLSDXmlArray(Node reader) throws OSDException, URISyntaxException
  {
    if (reader.getNodeType() != Node.ELEMENT_NODE|| !reader.getNodeName().equals("array"))
      throw new OSDException("Expected <array>");

    //System.out.println("Parsing Array...");
   
    OSDArray array = new OSDArray();
View Full Code Here

                    break;
                case Map:
                    SerializeLLSDBinaryMap(stream, (OSDMap)osd);
                    break;
                default:
                    throw new OSDException("Binary serialization: Not existing element discovered.");

            }
        }
View Full Code Here

            SkipWhiteSpace(stream);
            OSD osd;

            int marker = stream.read();
            if (marker < 0)
                throw new OSDException("Binary LLSD parsing: Unexpected end of stream.");

            switch ((byte)marker)
            {
                case undefBinaryValue:
                    osd = new OSD();
                    break;
                case trueBinaryValue:
                    osd = OSD.FromBoolean(true);
                    break;
                case falseBinaryValue:
                    osd = OSD.FromBoolean(false);
                    break;
                case integerBinaryMarker:
                    int integer = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                    //System.out.println("Found Integer Binary Marker");
                    osd = OSD.FromInteger(integer);
                    break;
                case realBinaryMarker:
                    //System.out.println("Found Real Binary Marker");
                    double dbl = NetworkToHostDouble(ConsumeBytes(stream, doubleLength));
                    //System.out.println("Double Value: " + dbl);                   
                    osd = OSD.FromReal(dbl);
                    break;
                case uuidBinaryMarker:
                    //System.out.println("Found UUID Binary Marker");
                    osd = OSD.FromUUID(new UUID(ConsumeBytes(stream, 16), 0));
                    break;
                case binaryBinaryMarker:
                    int binaryLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                    osd = OSD.FromBinary(ConsumeBytes(stream, binaryLength));
                    break;
                case StringBinaryMarker:
                    int StringLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                    //System.out.print("Length of String:" + StringLength);
                    String ss = new String(ConsumeBytes(stream, StringLength), "UTF-8");
                    //System.out.print("Found Length:" + StringLength + "\n");
                    osd = OSD.FromString(ss);
                    break;
                case uriBinaryMarker:
                    int uriLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                    String sUri = new String(ConsumeBytes(stream, uriLength), "UTF-8");
                    URI uri[] = new URI[1];
                    if(!Utils.tryParseUri(sUri, uri))
                    {
                        throw new OSDException("Binary LLSD parsing: Invalid Uri format detected.");
                    }
                    osd = OSD.FromUri(uri[0]);
                    break;
                case dateBinaryMarker:
                  //TODO Check why Little Endian Conversion is required in this case
                    double timestamp = Utils.bytesToDoubleLit(ConsumeBytes(stream, doubleLength), 0);
                    System.out.println("Timestamp: " + timestamp);
                      Date date = Utils.unixTimeToDate((long)(timestamp));
                    osd = OSD.FromDate(date);
                    break;
                case arrayBeginBinaryMarker:
                    osd = ParseLLSDBinaryArray(stream);
                    break;
                case mapBeginBinaryMarker:
                    osd = ParseLLSDBinaryMap(stream);
                    break;
                default:
                    throw new OSDException("Binary LLSD parsing: Unknown type marker: " + marker);

            }
            return osd;
        }
View Full Code Here

                osdArray.add(ParseLLSDBinaryElement(stream));
                crrElement++;
            }

            if (!FindByte(stream, arrayEndBinaryMarker))
                throw new OSDException("Binary LLSD parsing: Missing end marker in array.");

            return (OSD)osdArray;
        }
View Full Code Here

            int crrElement = 0;
            OSDMap osdMap = new OSDMap();
            while (crrElement < numElements)
            {
                if (!FindByte(stream, keyBinaryMarker))
                    throw new OSDException("Binary LLSD parsing: Missing key marker in map.");
                int keyLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                String key = new String(ConsumeBytes(stream, keyLength), "UTF-8");
                osdMap.put(key, ParseLLSDBinaryElement(stream));
                crrElement++;
            }

            if (!FindByte(stream, mapEndBinaryMarker))
                throw new OSDException("Binary LLSD parsing: Missing end marker in map.");

            return (OSD)osdMap;
        }
View Full Code Here

        {
            byte[] bytes = new byte[consumeBytes];
            if(consumeBytes > 0)
            {
              if (stream.read(bytes, 0, consumeBytes) < consumeBytes)
                throw new OSDException("Binary LLSD parsing: Unexpected end of stream.");
            }
//          //System.out.print("\t" + Utils.bytesToHexString(bytes, "consumed bytes\t"));
            return bytes;
        }
View Full Code Here

  private static OSD ParseLLSDJsonRoot(JsonElement obj) throws OSDException, URISyntaxException
  {   
    OSD ret = new OSD();

    if(!obj.isJsonArray())
      throw new OSDException("Json Top Element must be array");

    JsonArray rootArray = obj.getAsJsonArray();
    Iterator<JsonElement> iterator = rootArray.iterator();
    while(iterator.hasNext())
    {
View Full Code Here

TOP

Related Classes of com.ngt.jopenmetaverse.shared.structureddata.OSDException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.