Ejemplo n.º 1
0
  protected void parseTu(StartElement element) throws Exception {
    currentTu.clear();

    currentTu.changeid = getAttributeValue(element, "changeid");
    currentTu.changedate = parseISO8601date(getAttributeValue(element, "changedate"));
    currentTu.creationid = getAttributeValue(element, "creationid");
    currentTu.creationdate = parseISO8601date(getAttributeValue(element, "creationdate"));

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
          StartElement eStart = (StartElement) e;
          if ("tuv".equals(eStart.getName().getLocalPart())) {
            parseTuv(eStart);
          } else if ("prop".equals(eStart.getName().getLocalPart())) {
            parseProp(eStart);
          } else if ("note".equals(eStart.getName().getLocalPart())) {
            parseNote(eStart);
          }
          break;
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = (EndElement) e;
          if ("tu".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          break;
      }
    }
  }
Ejemplo n.º 2
0
  /** External TMX - level 1. Skip text inside inline tags. */
  protected void parseSegExtLevel1() throws Exception {
    segContent.setLength(0);

    int inlineLevel = 0;

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
          inlineLevel++;
          break;
        case XMLEvent.END_ELEMENT:
          inlineLevel--;
          EndElement eEnd = (EndElement) e;
          if ("seg".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          break;
        case XMLEvent.CHARACTERS:
          if (inlineLevel == 0) {
            Characters c = (Characters) e;
            segContent.append(c.getData());
          }
          break;
      }
    }
  }
  private void handleEvent(XMLEvent event, XMLEventReader reader, ParserTarget<T> target)
      throws Exception {
    XMLEvent nextEvent = null;
    if (event.isStartElement()) {
      xmlEventAdapter.setEvent(event);
      StartElement element = xmlEventAdapter.asStartElement();
      String name = element.getName().getLocalPart();
      elementStack.push(name);
      nextEvent = startElement(name, element, reader, target);
    } else if (event.isEndElement()) {
      EndElement element = event.asEndElement();
      String name = element.getName().getLocalPart();
      nextEvent = endElement(name, element, reader, target);

      Set<String> attributes = startElementAdapter.attributes;
      Iterator<Attribute> iterator = startElementAdapter.element.getAttributes();
      while (iterator.hasNext()) {
        Attribute attribute = iterator.next();
        if (!attributes.contains(attribute.getName().getLocalPart())) {
          notHandled(attribute.getName().getLocalPart(), Type.Attribute);
        }
      }

      elementStack.pop();
    }
    if (nextEvent != null && !cancel) {
      handleEvent(nextEvent, reader, target);
    }
  }
Ejemplo n.º 4
0
  private void parseTerm(StartElement root, XMLEventReader r)
      throws IOException, XMLStreamException {

    Attribute aboutAtt = root.getAttributeByName(rdfAbout);
    if (aboutAtt == null) {
      throw new IOException("no rdf:about");
    }
    TermImpl term = uri2term.get(aboutAtt.getValue());

    if (term == null) {
      term = new TermImpl();
      term.accession = aboutAtt.getValue();
      if (term.accession.startsWith(PREFIX)) {
        term.accession = term.accession.substring(PREFIX.length());
      }
      term.name = term.accession;
      uri2term.put(aboutAtt.getValue(), term);
    }
    while (r.hasNext()) {
      XMLEvent evt = r.nextEvent();
      if (evt.isStartElement()) {
        StartElement E = evt.asStartElement();
        QName qN = E.getName();
        if (NS.equals(qN.getNamespaceURI())) {
          if (qN.getLocalPart().equals("accession")) {
            term.accession = r.getElementText();
          } else if (qN.getLocalPart().equals("name")) {
            term.name = r.getElementText();
          } else if (qN.getLocalPart().equals("is_a")) {
            Attribute rsrc = E.getAttributeByName(rdfRsrc);
            if (rsrc == null)
              throw new IOException("att missing " + rdfRsrc + " for " + aboutAtt.getValue());

            String parentUri = rsrc.getValue();
            term.parents.add(parentUri);
            TermImpl parentTerm = this.uri2term.get(parentUri);
            if (parentTerm == null) {
              parentTerm = new TermImpl();
              parentTerm.accession = parentUri;
              if (parentTerm.accession.startsWith(PREFIX)) {
                parentTerm.accession = parentTerm.accession.substring(PREFIX.length());
              }
              parentTerm.name = parentTerm.accession;
              uri2term.put(parentUri, parentTerm);
            }
            parentTerm.children.add(aboutAtt.getValue());
          }
        }

      } else if (evt.isEndElement()) {
        EndElement E = evt.asEndElement();
        QName qN = E.getName();
        if (qN.getLocalPart().equals("term") && NS.equals(qN.getNamespaceURI())) {
          break;
        }
      }
    }
  }
  /** @deprecated */
  public static List<AndroidString> getAndroidStringsList(InputStream xml) {
    List<AndroidString> result = new ArrayList<AndroidString>();

    try {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      XMLEventReader eventReader = inputFactory.createXMLEventReader(xml);

      AndroidString androidString = null;

      while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
          StartElement startElement = event.asStartElement();
          if (startElement.getName().getLocalPart().equals("string")) {
            androidString = new AndroidString();

            Iterator<Attribute> attributes = startElement.getAttributes();

            while (attributes.hasNext()) {
              Attribute attribute = attributes.next();
              if (attribute.getName().toString().equals("name")) {
                androidString.setKey(attribute.getValue());
              }
            }

            event = eventReader.nextEvent();
            String value = event.asCharacters().getData().trim();

            // todo: if the value starts with xml tags(<u>), the value will be empty
            androidString.setValue(value);
            continue;
          }
        }

        if (event.isEndElement()) {
          EndElement endElement = event.asEndElement();
          if (endElement.getName().getLocalPart().equals("string")) {
            result.add(androidString);
          }
        }
      }
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
    return result;
  }
Ejemplo n.º 6
0
 private static void setSeriesKey(
     PortableTimeSeries ts, XMLEventReader eventReader, DataFlowStructure dsd)
     throws XMLStreamException {
   String id = null;
   String val = null;
   String[] dimensions = new String[dsd.getDimensions().size()];
   while (eventReader.hasNext()) {
     XMLEvent event = eventReader.nextEvent();
     logger.finest(event.toString());
     if (event.isStartElement()) {
       StartElement startElement = event.asStartElement();
       if (startElement.getName().getLocalPart().equalsIgnoreCase(VALUE)) {
         @SuppressWarnings("unchecked")
         Iterator<Attribute> attributes = startElement.getAttributes();
         while (attributes.hasNext()) {
           Attribute attribute = attributes.next();
           if (attribute.getName().toString().equalsIgnoreCase(CONCEPT)) {
             id = attribute.getValue();
           } else if (attribute.getName().toString().equalsIgnoreCase(VALUE)) {
             val = attribute.getValue();
           }
         }
       }
     }
     if (event.isEndElement()) {
       EndElement endElement = event.asEndElement();
       if (endElement.getName().getLocalPart().equalsIgnoreCase(VALUE)) {
         if (dsd.isDimension(id)) {
           dimensions[dsd.getDimensionPosition(id) - 1] = id + "=" + val;
         }
         if (id.equalsIgnoreCase("FREQ") || id.equalsIgnoreCase("FREQUENCY")) {
           ts.setFrequency(val);
         }
       }
     }
     if (event.isEndElement()) {
       EndElement endElement = event.asEndElement();
       if (endElement.getName().getLocalPart() == (SERIES_KEY)) {
         ts.setDimensions(Arrays.asList(dimensions));
         break;
       }
     }
   }
 }
Ejemplo n.º 7
0
  /** OmegaT TMX - just read full text. */
  protected void parseSegOmegaT() throws Exception {
    segContent.setLength(0);

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = (EndElement) e;
          if ("seg".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          break;
        case XMLEvent.CHARACTERS:
          Characters c = (Characters) e;
          segContent.append(c.getData());
          break;
      }
    }
  }
 @Override
 public HandlerResponse handle(XMLEvent event, Object digestTarget) throws XMLStreamException {
   if (event.isStartElement()) {
     StartElement element = event.asStartElement();
     String uri = element.getName().getNamespaceURI();
     if (rootName.getNamespaceURI().equals(uri)) {
       String localName = element.getName().getLocalPart();
       if (rootName.getLocalPart().equals(localName)) {
         // Check for attributes
         @SuppressWarnings("unchecked")
         Iterator<Attribute> attributes = element.getAttributes();
         while (attributes.hasNext()) {
           Attribute attribute = attributes.next();
           String name = "@" + attribute.getName().getLocalPart();
           if (digestedMethods.containsKey(name)) {
             callSetter(digestedMethods.get(name), digestTarget, attribute.getValue());
           } else if (digestedFields.containsKey(name)) {
             setFieldValue(digestedFields.get(name), digestTarget, attribute.getValue());
           }
         }
       }
       if (digestedFields.containsKey(localName) || digestedMethods.containsKey(localName)) {
         resetCharacterBuffer();
       }
     }
   } else if (event.isEndElement()) {
     EndElement element = event.asEndElement();
     if (rootName.equals(element.getName())) {
       return new FinishedParsingResponse();
     } else {
       String uri = element.getName().getNamespaceURI();
       if (rootName.getNamespaceURI().equals(uri)) {
         String localName = element.getName().getLocalPart();
         if (digestedMethods.containsKey(localName)) {
           callSetter(digestedMethods.get(localName), digestTarget, getCharacterBuffer());
         } else if (digestedFields.containsKey(localName)) {
           setFieldValue(digestedFields.get(localName), digestTarget, getCharacterBuffer());
         }
       }
     }
   }
   return super.handle(event, digestTarget);
 }
Ejemplo n.º 9
0
  public static List<PortableTimeSeries> parse(
      InputStreamReader xmlBuffer, DataFlowStructure dsd, String dataflow, boolean data)
      throws XMLStreamException, UnsupportedEncodingException, SdmxException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    BufferedReader br = skipBOM(xmlBuffer);
    XMLEventReader eventReader = inputFactory.createXMLEventReader(br);

    List<PortableTimeSeries> tsList = new ArrayList<PortableTimeSeries>();
    PortableTimeSeries ts = null;

    while (eventReader.hasNext()) {
      XMLEvent event = eventReader.nextEvent();
      logger.finest(event.toString());

      if (event.isStartElement()) {
        StartElement startElement = event.asStartElement();

        if (startElement.getName().getLocalPart() == (SERIES)) {
          ts = new PortableTimeSeries();
          ts.setDataflow(dataflow);
        }

        if (startElement.getName().getLocalPart() == (SERIES_KEY)) {
          setSeriesKey(ts, eventReader, dsd);
        }

        if (startElement.getName().getLocalPart() == (ATTRIBUTES)) {
          setSeriesAttributes(ts, eventReader);
        }

        if (startElement.getName().getLocalPart() == (OBS) && data) {
          setSeriesSingleObs(ts, eventReader);
        }
      }
      if (event.isEndElement()) {
        EndElement endElement = event.asEndElement();
        if (endElement.getName().getLocalPart() == (SERIES)) {
          tsList.add(ts);
        }
      }
    }
    return tsList;
  }
Ejemplo n.º 10
0
  /**
   * Write a new element with the specified name and attribute before the end element is written out
   *
   * @return the end element
   */
  private XMLEvent getAddedEventBeforeEndElement(
      XMLEvent event,
      XMLEventWriter writer,
      String elementName,
      String attributeName,
      String attributeValue)
      throws XMLStreamException {
    Attribute newAttribute = xmlEventFactory.createAttribute(attributeName, attributeValue);
    Set attributes = new HashSet();
    attributes.add(newAttribute);

    EndElement oldEvent = event.asEndElement();
    StartElement newStartEvent =
        xmlEventFactory.createStartElement(
            new QName(elementName), attributes.iterator(), oldEvent.getNamespaces());

    writer.add(newStartEvent);
    return xmlEventFactory.createEndElement(newStartEvent.getName(), newStartEvent.getNamespaces());
  }
Ejemplo n.º 11
0
  protected void parseTuv(StartElement element) throws Exception {
    ParsedTuv tuv = new ParsedTuv();
    currentTu.tuvs.add(tuv);

    tuv.changeid = getAttributeValue(element, "changeid");
    tuv.changedate = parseISO8601date(getAttributeValue(element, "changedate"));
    tuv.creationid = getAttributeValue(element, "creationid");
    tuv.creationdate = parseISO8601date(getAttributeValue(element, "creationdate"));

    // find 'lang' or 'xml:lang' attribute
    for (Iterator<Attribute> it = element.getAttributes(); it.hasNext(); ) {
      Attribute a = it.next();
      if ("lang".equals(a.getName().getLocalPart())) {
        tuv.lang = a.getValue();
        break;
      }
    }

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
          StartElement eStart = (StartElement) e;
          if ("seg".equals(eStart.getName().getLocalPart())) {
            if (isOmegaT) {
              parseSegOmegaT();
            } else if (extTmxLevel2) {
              parseSegExtLevel2();
            } else {
              parseSegExtLevel1();
            }
            tuv.text = segContent.toString();
          }
          break;
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = (EndElement) e;
          if ("tuv".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          break;
      }
    }
  }
Ejemplo n.º 12
0
  protected void parseNote(StartElement element) throws Exception {
    noteContent.setLength(0);

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = (EndElement) e;
          if ("note".equals(eEnd.getName().getLocalPart())) {
            currentTu.note = noteContent.toString();
            return;
          }
          break;
        case XMLEvent.CHARACTERS:
          Characters c = (Characters) e;
          noteContent.append(c.getData());
          break;
      }
    }
  }
Ejemplo n.º 13
0
  protected void parseProp(StartElement element) throws Exception {
    String propType = getAttributeValue(element, "type");
    propContent.setLength(0);

    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = (EndElement) e;
          if ("prop".equals(eEnd.getName().getLocalPart())) {
            currentTu.props.add(new TMXProp(propType, propContent.toString()));
            return;
          }
          break;
        case XMLEvent.CHARACTERS:
          Characters c = (Characters) e;
          propContent.append(c.getData());
          break;
      }
    }
  }
  protected Preference readPreference(XMLEventReader xmlEventReader) throws XMLStreamException {

    String name = null;
    List<String> values = new ArrayList<String>();
    boolean readOnly = false;

    while (xmlEventReader.hasNext()) {
      XMLEvent xmlEvent = xmlEventReader.nextEvent();

      if (xmlEvent.isStartElement()) {
        StartElement startElement = xmlEvent.asStartElement();

        String elementName = startElement.getName().getLocalPart();

        if (elementName.equals("name")) {
          name = StAXReaderUtil.read(xmlEventReader);
        } else if (elementName.equals("value")) {
          String value = StAXReaderUtil.read(xmlEventReader);

          values.add(value);
        } else if (elementName.equals("read-only")) {
          String value = StAXReaderUtil.read(xmlEventReader);

          readOnly = GetterUtil.getBoolean(value);
        }
      } else if (xmlEvent.isEndElement()) {
        EndElement endElement = xmlEvent.asEndElement();

        String elementName = endElement.getName().getLocalPart();

        if (elementName.equals("preference")) {
          break;
        }
      }
    }

    return new Preference(name, values.toArray(new String[values.size()]), readOnly);
  }
Ejemplo n.º 15
0
  private static void setSeriesSingleObs(PortableTimeSeries ts, XMLEventReader eventReader)
      throws XMLStreamException {
    String time = null;
    String val = null;
    Hashtable<String, String> obs_attr = new Hashtable<String, String>();
    while (eventReader.hasNext()) {
      XMLEvent event = eventReader.nextEvent();
      logger.finest(event.toString());
      if (event.isStartElement()) {
        StartElement startElement = event.asStartElement();
        if (startElement.getName().getLocalPart() == (OBS_TIME)) {
          time = eventReader.getElementText();
        }
        if (startElement.getName().getLocalPart() == (OBS_VALUE)) {
          val = startElement.getAttributeByName(new QName(VALUE)).getValue();
        }
        if (startElement.getName().getLocalPart() == (ATTRIBUTEVALUE)) {
          String name = startElement.getAttributeByName(new QName(CONCEPT)).getValue();
          String value = startElement.getAttributeByName(new QName(VALUE)).getValue();
          obs_attr.put(name, value);
        }
      }
      if (event.isEndElement()) {
        EndElement endElement = event.asEndElement();
        if (endElement.getName().getLocalPart() == (OBS)) {
          try {
            ts.addObservation(new Double(val), time, obs_attr);
          } catch (NumberFormatException e) {
            logger.warning(
                "Invalid observation: '" + val + "' for ts " + ts.getName() + ". Setting NaN.");
            ts.addObservation(new Double("NaN"), time, obs_attr);
          }

          break;
        }
      }
    }
  }
Ejemplo n.º 16
0
 private static void setSeriesAttributes(PortableTimeSeries ts, XMLEventReader eventReader)
     throws XMLStreamException {
   String id = null;
   String val = null;
   while (eventReader.hasNext()) {
     XMLEvent event = eventReader.nextEvent();
     logger.finest(event.toString());
     if (event.isStartElement()) {
       StartElement startElement = event.asStartElement();
       if (startElement.getName().getLocalPart().equalsIgnoreCase(VALUE)) {
         @SuppressWarnings("unchecked")
         Iterator<Attribute> attributes = startElement.getAttributes();
         while (attributes.hasNext()) {
           Attribute attribute = attributes.next();
           if (attribute.getName().toString().equalsIgnoreCase(CONCEPT)) {
             id = attribute.getValue();
           } else if (attribute.getName().toString().equalsIgnoreCase(VALUE)) {
             val = attribute.getValue();
           }
         }
       }
     }
     if (event.isEndElement()) {
       EndElement endElement = event.asEndElement();
       if (endElement.getName().getLocalPart().equalsIgnoreCase(VALUE)) {
         ts.addAttribute(id + '=' + val);
       }
     }
     if (event.isEndElement()) {
       EndElement endElement = event.asEndElement();
       if (endElement.getName().getLocalPart() == (ATTRIBUTES)) {
         break;
       }
     }
   }
 }
Ejemplo n.º 17
0
 private boolean isEqual(EndElement e1, EndElement e2) {
   return isEqual(e1.getName(), e2.getName());
 }
Ejemplo n.º 18
0
  // private otherwise not safe - since constructor calling this method
  private void loadObjects() {

    staticGameObjs = new ArrayList<GameObject>();
    animatedGameObjs = new ArrayList<GameObject>();
    InputStream in = null;

    try {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      in = en.loadData(configFile);
      // this.setStream(in);
      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
      // Sprite sprite = null;
      GameObject curObj = null;
      // level = null;

      while (eventReader.hasNext()) {

        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {

          StartElement startElement = event.asStartElement();

          // If we have a item element we create a new item
          if (startElement.getName().getLocalPart().equals(OBJ_SPRITE)) {

            Iterator<Attribute> attributes = startElement.getAttributes();
            while (attributes.hasNext()) {

              Attribute attribute = attributes.next();
              if (attribute.getName().toString().equals(SPRITE_TYPE)) {

                String spriteName = attribute.getValue();

                curObj = createGameObjects(spriteName);
              }

              if (attribute.getName().toString().equals(NATURE)) {
                curObj.setNature(attribute.getValue());
              }
            }
          } else if (event.isStartElement() && curObj != null) {

            String property = event.asStartElement().getName().getLocalPart();
            event = eventReader.nextEvent();
            String data_Str = event.asCharacters().getData();

            configProperties(curObj, property, data_Str);
          }
        }

        if (event.isEndElement() && curObj != null) {

          EndElement endElement = event.asEndElement();
          if (endElement.getName().getLocalPart().equals(OBJ_SPRITE)) {

            if (curObj.getIsAnimated()) {
              this.animatedGameObjs.add(curObj);
              // Create remaining GameObjects defined in xml tag <total>
              if (curObj.getTotal() > 1) {
                for (int i = 0; i < curObj.getTotal() - 1; i++) {

                  GameObject newCurObj = createGameObjects(curObj.getName());
                  newCurObj.setX((float) Math.random() * 600);
                  newCurObj.setY((float) Math.random() * 100);
                  newCurObj.setName(curObj.getName());
                  newCurObj.setStgy(curObj.getStgy());
                  this.animatedGameObjs.add(newCurObj);
                }
              }

            } else this.staticGameObjs.add(curObj);
          }
        }
      }

      // Send GameObject arrays to current Level
      this.level.setArrGameAnim(animatedGameObjs);
      this.level.setArrGameStatic(staticGameObjs);

    } catch (FileNotFoundException ex) {

    } catch (XMLStreamException e) {
    }
  }
Ejemplo n.º 19
0
  @SuppressWarnings("unchecked")
  public List<Vehicle> readConfig(String config) {
    List<Vehicle> vehicles = new ArrayList<Vehicle>();
    try {
      // First, create a new XMLInputFactory
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      // Setup a new eventReader
      InputStream in = new FileInputStream(config);
      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
      // System.out.println("eventReader:" + eventReader);
      // read the XML document
      Vehicle vechile = null;

      while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()) {
          StartElement startElement = event.asStartElement();
          if (startElement.getName().getLocalPart() == (ROW)) {
            Iterator<Attribute> attributes = startElement.getAttributes();
            while (attributes.hasNext()) {
              Attribute attribute = attributes.next();
              if (attribute.getName().toString().equals(TYPE)) {
                if (attribute.getValue().equalsIgnoreCase("car")) {
                  vechile = new Car();
                } else {
                  vechile = new Bike();
                }
              }
            }

          } else if (event.asStartElement().getName().getLocalPart().equals(VEHICLEID)) {
            event = eventReader.nextEvent();
            vechile.setVehicleId(Double.parseDouble(event.asCharacters().getData()));

          } else if (event.asStartElement().getName().getLocalPart().equals(MAKE)) {
            event = eventReader.nextEvent();
            vechile.setMake(event.asCharacters().getData());

          } else if (event.asStartElement().getName().getLocalPart().equals(MODEL)) {
            event = eventReader.nextEvent();
            vechile.setModel(event.asCharacters().getData());

          } else if (event.asStartElement().getName().getLocalPart().equals(ENGINEINCC)) {
            event = eventReader.nextEvent();
            vechile.setEngineInCC(Integer.parseInt(event.asCharacters().getData()));

          } else if (event.asStartElement().getName().getLocalPart().equals(FUELCAPACITY)) {
            event = eventReader.nextEvent();
            vechile.setFuelCapacity(Integer.parseInt(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(MILEAGE)) {
            event = eventReader.nextEvent();
            vechile.setMileage(Integer.parseInt(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(PRICE)) {
            event = eventReader.nextEvent();
            vechile.setPrice(Float.parseFloat(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(ROADTAX)) {
            event = eventReader.nextEvent();
            vechile.setRoadTax(Float.parseFloat(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(AC)) {
            event = eventReader.nextEvent();
            ((Car) vechile).setAC(Boolean.parseBoolean(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(POWERSTEERING)) {
            event = eventReader.nextEvent();
            ((Car) vechile).setPowerSteering(Boolean.parseBoolean(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(ACCESSORYKIT)) {
            event = eventReader.nextEvent();
            ((Car) vechile).setAccessoryKit(Boolean.parseBoolean(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(SELFSTART)) {
            event = eventReader.nextEvent();
            ((Bike) vechile).setSelfStart(Boolean.parseBoolean(event.asCharacters().getData()));
          } else if (event.asStartElement().getName().getLocalPart().equals(HELMETPRICE)) {
            event = eventReader.nextEvent();
            ((Bike) vechile).setHelmetPrice(Integer.parseInt(event.asCharacters().getData()));
          }
        }
        if (event.isEndElement()) {
          EndElement endElement = event.asEndElement();
          if (endElement.getName().getLocalPart() == (ROW)) {
            vehicles.add(vechile);
          }
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
    return vehicles;
  }
Ejemplo n.º 20
0
 private boolean isReplyTo(EndElement endElement) {
   return endElement.getName().equals(new QName(ADDRESSING_URI, "ReplyTo"));
 }
Ejemplo n.º 21
0
 public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
   XMLEvent currentEvent = input.peek();
   if (currentEvent != null) {
     int eventType = currentEvent.getEventType();
     if (eventType != XMLStreamConstants.START_DOCUMENT
         && eventType != XMLStreamConstants.START_ELEMENT) {
       throw new XMLStreamException();
     }
     fLocationWrapper.setLocation(currentEvent.getLocation());
     fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
     loop:
     while (input.hasNext()) {
       currentEvent = input.nextEvent();
       eventType = currentEvent.getEventType();
       switch (eventType) {
         case XMLStreamConstants.START_ELEMENT:
           ++fDepth;
           StartElement start = currentEvent.asStartElement();
           fillQName(fElementQName, start.getName());
           fLocationWrapper.setLocation(start.getLocation());
           fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
           fillXMLAttributes(start);
           fillDeclaredPrefixes(start);
           addNamespaceDeclarations();
           fNamespaceContext.pushContext();
           fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
           break;
         case XMLStreamConstants.END_ELEMENT:
           EndElement end = currentEvent.asEndElement();
           fillQName(fElementQName, end.getName());
           fillDeclaredPrefixes(end);
           fLocationWrapper.setLocation(end.getLocation());
           fSchemaDOMParser.endElement(fElementQName, null);
           fNamespaceContext.popContext();
           --fDepth;
           if (fDepth <= 0) {
             break loop;
           }
           break;
         case XMLStreamConstants.CHARACTERS:
           sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
           break;
         case XMLStreamConstants.SPACE:
           sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
           break;
         case XMLStreamConstants.CDATA:
           fSchemaDOMParser.startCDATA(null);
           sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
           fSchemaDOMParser.endCDATA(null);
           break;
         case XMLStreamConstants.PROCESSING_INSTRUCTION:
           ProcessingInstruction pi = (ProcessingInstruction) currentEvent;
           fillProcessingInstruction(pi.getData());
           fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
           break;
         case XMLStreamConstants.DTD:
           /* There shouldn't be a DTD in the schema */
           break;
         case XMLStreamConstants.ENTITY_REFERENCE:
           /* Not needed for schemas */
           break;
         case XMLStreamConstants.COMMENT:
           /* No point in sending comments */
           break;
         case XMLStreamConstants.START_DOCUMENT:
           fDepth++;
           /* We automatically call startDocument before the loop */
           break;
         case XMLStreamConstants.END_DOCUMENT:
           /* We automatically call endDocument after the loop */
           break;
       }
     }
     fLocationWrapper.setLocation(null);
     fNamespaceContext.setNamespaceContext(null);
     fSchemaDOMParser.endDocument(null);
   }
 }
Ejemplo n.º 22
0
  /** External TMX - level 2. Replace all tags into shortcuts. */
  protected void parseSegExtLevel2() throws Exception {
    segContent.setLength(0);
    segInlineTag.setLength(0);
    inlineTagHandler.reset();

    int inlineLevel = 0;
    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
          StartElement eStart = e.asStartElement();
          if ("hi".equals(eStart.getName().getLocalPart())) {
            // tag should be skipped
            break;
          }
          inlineLevel++;
          segInlineTag.setLength(0);
          if ("bpt".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startBPT(
                getAttributeValue(eStart, "i"), getAttributeValue(eStart, "x"));
            inlineTagHandler.setTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
          } else if ("ept".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startEPT(getAttributeValue(eStart, "i"));
          } else if ("it".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startOTHER();
            inlineTagHandler.setOtherTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
            inlineTagHandler.setCurrentPos(getAttributeValue(eStart, "pos"));
          } else if ("ph".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startOTHER();
            inlineTagHandler.setOtherTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
          } else {
            inlineTagHandler.startOTHER();
          }
          break;
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = e.asEndElement();
          if ("hi".equals(eEnd.getName().getLocalPart())) {
            // tag should be skipped
            break;
          }
          inlineLevel--;
          if ("seg".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          boolean slashBefore = false;
          boolean slashAfter = false;
          char tagName = StringUtil.getFirstLetterLowercase(segInlineTag);
          Integer tagN;
          if ("bpt".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getTagShortcutLetter();
            }
            tagN = inlineTagHandler.endBPT();
          } else if ("ept".equals(eEnd.getName().getLocalPart())) {
            slashBefore = true;
            tagName = inlineTagHandler.getTagShortcutLetter();
            tagN = inlineTagHandler.endEPT();
          } else if ("it".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setOtherTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getOtherTagShortcutLetter();
            }
            tagN = inlineTagHandler.endOTHER();
            if ("end".equals(inlineTagHandler.getCurrentPos())) {
              slashBefore = true;
            }
          } else if ("ph".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setOtherTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getOtherTagShortcutLetter();
            }
            tagN = inlineTagHandler.endOTHER();
            if (useSlash) {
              slashAfter = true;
            }
          } else {
            tagN = inlineTagHandler.endOTHER();
            if (useSlash) {
              slashAfter = true;
            }
          }
          if (tagName == 0) {
            tagName = 'f';
          }
          if (tagN == null) {
            // check error of TMX reading
            Log.logErrorRB(
                "TMX_ERROR_READING_LEVEL2",
                e.getLocation().getLineNumber(),
                e.getLocation().getColumnNumber());
            errorsCount++;
            segContent.setLength(0);
            // wait for end seg
            while (true) {
              XMLEvent ev = xml.nextEvent();
              switch (ev.getEventType()) {
                case XMLEvent.END_ELEMENT:
                  EndElement evEnd = (EndElement) ev;
                  if ("seg".equals(evEnd.getName().getLocalPart())) {
                    return;
                  }
              }
            }
          }

          segContent.append('<');
          if (slashBefore) {
            segContent.append('/');
          }
          segContent.append(tagName);
          segContent.append(Integer.toString(tagN));
          if (slashAfter) {
            segContent.append('/');
          }
          segContent.append('>');
          break;
        case XMLEvent.CHARACTERS:
          Characters c = (Characters) e;
          if (inlineLevel == 0) {
            segContent.append(c.getData());
          } else {
            segInlineTag.append(c.getData());
          }
          break;
      }
    }
  }
Ejemplo n.º 23
0
 /** Fills in the list of declared prefixes. */
 private void fillDeclaredPrefixes(EndElement event) {
   fillDeclaredPrefixes(event.getNamespaces());
 }
  private static void read(XMLEventReader reader, DumpData data, ReadMode readMode)
      throws XMLStreamException {
    // basic page info
    int pageId = -1;
    String title = null;
    String pageText = null;

    boolean withinRevisionTag = false;

    int pagesProcessed = 0;

    while (reader.hasNext()) {
      XMLEvent event = reader.nextEvent();
      if (event.isStartElement()) {
        StartElement startElement = event.asStartElement();
        String strStartElement = startElement.getName().getLocalPart();

        if (strStartElement.equals(PAGE_REVISION_TAG)) {
          withinRevisionTag = true;
        }

        if (!withinRevisionTag) {
          switch (strStartElement) {
            case PAGE_ID_TAG:
              pageId = Integer.parseInt(reader.nextEvent().asCharacters().getData());
              break;
            case PAGE_TITLE_TAG:
              if (readMode == ReadMode.TITLE) {
                title = reader.nextEvent().asCharacters().getData();
              }
              break;
            default:
              break;
          }
        } else if (readMode == ReadMode.CONTENT && strStartElement.equals(PAGE_REVISION_TEXT_TAG)) {
          pageText = reader.getElementText();
        }
      }

      if (event.isEndElement()) {
        EndElement endElement = event.asEndElement();
        String strEndElement = endElement.getName().getLocalPart();
        switch (strEndElement) {
          case PAGE_REVISION_TAG:
            withinRevisionTag = false;
            break;
          case PAGE_TAG:
            // process retrieved page related information depending on the dump.
            if (pageId == -1) {
              logger_.warn("Invalid Page Entry");
            } else if (readMode == ReadMode.TITLE) {
              logger_.debug("Extracted page : " + title + "(id : " + pageId + ")");
              data.addPageEntry(pageId, title);
            } else if (readMode == ReadMode.CONTENT) {
              logger_.debug("Extracted page content (id : " + pageId + ")");
              data.addContentInfo(pageId, pageText);
            }

            // reset
            pageId = -1;
            title = null;
            pageText = null;
            if (++pagesProcessed % 1_000_000 == 0)
              logger_.info("Processed: {} k", pagesProcessed / 1000);
            break;
          default:
            break;
        }
      }
    }
  }
Ejemplo n.º 25
0
  /**
   * Handles a Stream event.
   *
   * @param event
   * @throws SAXException
   */
  private void handleEvent(XMLEvent event) throws SAXException {
    if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
      Characters c = event.asCharacters();
      lastContents += c.getData();
    } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
      StartElement startElement = event.asStartElement();
      String tagLocalName = startElement.getName().getLocalPart();

      if ("row".equals(tagLocalName)) {
        Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
        if (firstRowIndex == -1) {
          firstRowIndex = Integer.parseInt(rowIndex.getValue());
        }
        currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
      } else if ("cols".equals(tagLocalName)) {
        parsingCols = true;
      } else if ("col".equals(tagLocalName) && parsingCols) {
        colNumber = colNumber + 1;
      } else if ("c".equals(tagLocalName)) {
        Attribute ref = startElement.getAttributeByName(new QName("r"));

        String[] coord = ref.getValue().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
        currentCell =
            new StreamingCell(
                CellReference.convertColStringToIndex(coord[0]), Integer.parseInt(coord[1]) - 1);
        setFormatString(startElement, currentCell);

        Attribute type = startElement.getAttributeByName(new QName("t"));
        if (type != null) {
          currentCell.setType(type.getValue());
        } else {
          currentCell.setType("n");
        }

        Attribute style = startElement.getAttributeByName(new QName("s"));
        if (style != null) {
          String indexStr = style.getValue();
          try {
            int index = Integer.parseInt(indexStr);
            currentCell.setCellStyle(stylesTable.getStyleAt(index));
          } catch (NumberFormatException nfe) {
            LOGGER.warn("Ignoring invalid style index {}", indexStr);
          }
        }
        // we store the dimension as well to revert with this method when cols not found
        // can happen see xlsx attached here https://jira.talendforge.org/browse/TDP-1957
        // <dimension ref="A1:B60"/>
      } else if ("dimension".equals(tagLocalName)) {
        Attribute attribute = startElement.getAttributeByName(new QName("ref"));
        if (attribute != null) {
          this.dimension = attribute.getValue();
        }
      }

      // Clear contents cache
      lastContents = "";
    } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
      EndElement endElement = event.asEndElement();
      String tagLocalName = endElement.getName().getLocalPart();

      if ("v".equals(tagLocalName) || "t".equals(tagLocalName)) {
        currentCell.setRawContents(unformattedContents());
        currentCell.setContents(formattedContents());
      } else if ("row".equals(tagLocalName) && currentRow != null) {
        rowCache.add(currentRow);
      } else if ("c".equals(tagLocalName)) {
        currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
      } else if ("cols".equals(tagLocalName)) {
        parsingCols = false;
      }
    }
  }