Package org.eclipse.emf.ecore.xmi

Examples of org.eclipse.emf.ecore.xmi.XMLParserPool


    }
   
    this.resource = resource;
    is = inputStream;
    this.options = options;
    XMLParserPool pool = (XMLParserPool)options.get(XMLResource.OPTION_USE_PARSER_POOL);
    @SuppressWarnings("unchecked") Map<String, Boolean> parserFeatures = (Map<String, Boolean>)options.get(XMLResource.OPTION_PARSER_FEATURES);
    @SuppressWarnings("unchecked") Map<String, ?> parserProperties = (Map<String, ?>)options.get(XMLResource.OPTION_PARSER_PROPERTIES);
    parserFeatures = (parserFeatures == null) ? Collections.<String, Boolean>emptyMap() : parserFeatures;
    parserProperties = (parserProperties == null) ? Collections.<String, Object>emptyMap() : parserProperties;

    // HACK: reading encoding
    String encoding = null;
    if (!Boolean.FALSE.equals(options.get(XMLResource.OPTION_USE_DEPRECATED_METHODS)))
    {
      encoding = getEncoding();
      resource.setEncoding(encoding);
    }
    try
    {
      SAXParser parser; 
      DefaultHandler handler;

      if (pool != null)
      {
        // use the pool to retrieve the parser
        parser = pool.get(parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
        handler = (DefaultHandler)pool.getDefaultHandler(resource, this, helper, options);
      }
      else
      {
        parser = makeParser();
        handler = makeDefaultHandler();
        // set features and properties
        if (parserFeatures != null)
        {
          for (Map.Entry<String, Boolean> entry : parserFeatures.entrySet())
          {
            parser.getXMLReader().setFeature(entry.getKey(), entry.getValue());
          }
        }
        if (parserProperties !=null)
        {
          for (Map.Entry<String, ?> entry : parserProperties.entrySet())
          {
            parser.getXMLReader().setProperty(entry.getKey(), entry.getValue());
          }
        }
      }

      InputSource inputSource = new InputSource(is);
      if (resource.getURI() != null)
      {
        String resourceURI = resource.getURI().toString();
        inputSource.setPublicId(resourceURI);
        inputSource.setSystemId(resourceURI);
        inputSource.setEncoding(encoding);
      }
   
      // set lexical handler
      if (Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)))
      {
        if (parserProperties == null || parserProperties.get(SAX_LEXICAL_PROPERTY) == null)
        {
          parser.setProperty(SAX_LEXICAL_PROPERTY, handler);
        }
      }
     
      parser.parse(inputSource, handler);
     
      // release parser back to the pool
      if (pool != null)
      {
        pool.release(parser, parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
        pool.releaseDefaultHandler((XMLDefaultHandler)handler, options);
      }
     
      helper = null;
      handleErrors();
    }
View Full Code Here


  public void load(XMLResource resource, InputSource inputSource, Map<?, ?> options) throws IOException
  {
    this.resource = resource;
  
    this.options = options;
    XMLParserPool pool = (XMLParserPool)options.get(XMLResource.OPTION_USE_PARSER_POOL);
    @SuppressWarnings("unchecked") Map<String, Boolean> parserFeatures = (Map<String, Boolean>)options.get(XMLResource.OPTION_PARSER_FEATURES);
    @SuppressWarnings("unchecked") Map<String, ?> parserProperties = (Map<String, ?>)options.get(XMLResource.OPTION_PARSER_PROPERTIES);
    parserFeatures = (parserFeatures == null) ? Collections.<String, Boolean>emptyMap() : parserFeatures;
    parserProperties = (parserProperties == null) ? Collections.<String, Object>emptyMap() : parserProperties;

    // Don't read encoding - rely on XML parser to provide one via Locator2

    try
    {
      SAXParser parser; 
      DefaultHandler handler;

      if (pool != null)
      {
        // use the pool to retrieve the parser
        parser = pool.get(parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
        handler = (DefaultHandler)pool.getDefaultHandler(resource, this, helper, options);
      }
      else
      {
        parser = makeParser();
        handler = makeDefaultHandler();
        // set features and properties
        if (parserFeatures != null)
        {
          for (Map.Entry<String, Boolean> feature : parserFeatures.entrySet())
          {
            parser.getXMLReader().setFeature(feature.getKey(),  feature.getValue());
          }
        }
        if (parserProperties !=null)
        {
          for (Map.Entry<String, ?> property : parserProperties.entrySet())
          {
            parser.getXMLReader().setProperty(property.getKey(), property.getValue());
          }
        }
      }
 
      // set lexical handler
      if (Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)))
      {
        if (parserProperties == null || parserProperties.get(SAX_LEXICAL_PROPERTY) == null)
        {
          parser.setProperty(SAX_LEXICAL_PROPERTY, handler);
        }
      }
     
      parser.parse(inputSource, handler);

      // release parser back to the pool
      if (pool != null)
      {
        pool.release(parser, parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
        pool.releaseDefaultHandler((XMLDefaultHandler)handler, options);
      }

      helper = null;
      handleErrors();
    }
View Full Code Here

  {
    this.resource = resource;
    this.options = options;
    this.namespaceAware = Boolean.FALSE.equals(options.get(XMLResource.OPTION_USE_DEPRECATED_METHODS));
    DefaultHandler handler;
    XMLParserPool pool = (XMLParserPool)options.get(XMLResource.OPTION_USE_PARSER_POOL);
    if (pool != null)
    {
      handler = (DefaultHandler)pool.getDefaultHandler(resource, this, helper, options);
    }
    else
    {
      handler = makeDefaultHandler();
    }
    LexicalHandler lexicalHandler = null;

    if (Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)))
    {
      lexicalHandler = (LexicalHandler)handler;
    }

    AttributesProxy attributesProxy = new AttributesProxy();
    try
    {
      short type = node.getNodeType();
      if (type == Node.ELEMENT_NODE)
      {
        handler.startDocument();
        if (Boolean.TRUE.equals(options.get(XMLResource.OPTION_DOM_USE_NAMESPACES_IN_SCOPE)))
        {
          traverseElement((Element)node, attributesProxy, handler, lexicalHandler);
        }
        else
        {
          traverse(node, attributesProxy, handler, lexicalHandler);
        }
        handler.endDocument();     
      }
      else
      {
        traverse(node, attributesProxy, handler, lexicalHandler);
      }
    }
    catch (SAXException e)
    {
      // ignore
    }
   
    if (pool != null)
    {
      pool.releaseDefaultHandler((XMLDefaultHandler)handler, options);
    }
   
    attributesProxy = null;
    handler = null;
    lexicalHandler = null;
View Full Code Here

TOP

Related Classes of org.eclipse.emf.ecore.xmi.XMLParserPool

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.