org.citygml4j.xml.io.reader.XMLChunk Java Examples

The following examples show how to use org.citygml4j.xml.io.reader.XMLChunk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FeatureReaderWorker.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	if (firstWork != null) {
		doWork(firstWork);
		firstWork = null;
	}

	while (shouldRun) {
		try {
			XMLChunk work = workQueue.take();				
			doWork(work);
		} catch (InterruptedException ie) {
			// re-check state
		}
	}
}
 
Example #2
Source File: JAXBSimpleReader.java    From citygml4j with Apache License 2.0 6 votes vote down vote up
public synchronized XMLChunk nextChunk() throws CityGMLReadException {
	XMLChunkImpl next = null;

	if (iterator || hasNext()) {
		try {
			next = new XMLChunkImpl(this, null);
			while (reader.hasNext()) {
				next.addEvent(reader);
				if (next.isComplete())
					break;
				
				reader.next();
			}
		} catch (XMLStreamException e) {
			throw new CityGMLReadException("Caused by: ", e);
		}
	}

	if (next == null)
		throw new NoSuchElementException();
	else {
		iterator = false;
		return next;
	}
}
 
Example #3
Source File: AdditionalObjectsHandler.java    From web-feature-service with Apache License 2.0 4 votes vote down vote up
protected void writeObjects() {
    if (!shouldRun)
        return;

    try {
        // close writers to flush buffers
        for (CityModelWriter tempWriter : tempWriters.values())
            tempWriter.close();

        CityGMLInputFactory in = cityGMLBuilder.createCityGMLInputFactory();
        in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_COLLECTION_MEMBER);

        startAdditionalObjects();

        Attributes dummyAttributes = new AttributesImpl();
        String propertyName = "member";
        String propertyQName = saxWriter.getPrefix(Constants.WFS_NAMESPACE_URI) + ":" + propertyName;

        // iterate over temp files and send additional objects to response stream
        for (Path tempFile : tempFiles.values()) {
            try (CityGMLReader tempReader = in.createCityGMLReader(tempFile.toFile())) {
                while (tempReader.hasNext()) {
                    XMLChunk chunk = tempReader.nextChunk();
                    if ("CityModel".equals(chunk.getTypeName().getLocalPart())
                            && Modules.isCityGMLModuleNamespace(chunk.getTypeName().getNamespaceURI()))
                        continue;

                    ContentHandler handler;
                    if (transformerChainFactory == null)
                        handler = saxWriter;
                    else {
                        TransformerChain chain = transformerChainFactory.buildChain();
                        chain.tail().setResult(new SAXResult(saxWriter));
                        handler = chain.head();
                        handler.startDocument();
                    }

                    handler.startElement(Constants.WFS_NAMESPACE_URI, propertyName, propertyQName, dummyAttributes);
                    chunk.send(handler, true);
                    handler.endElement(Constants.WFS_NAMESPACE_URI, propertyName, propertyQName);

                    if (transformerChainFactory != null)
                        handler.endDocument();
                }
            }
        }

    } catch (CityGMLWriteException | CityGMLBuilderException | CityGMLReadException | SAXException | TransformerConfigurationException e) {
        eventDispatcher.triggerSyncEvent(new InterruptEvent("Failed to write additional objects.", LogLevel.ERROR, e, eventChannel, this));
    }
}
 
Example #4
Source File: FeatureReaderWorkerFactory.java    From importer-exporter with Apache License 2.0 4 votes vote down vote up
@Override
public Worker<XMLChunk> createWorker() {
	return new FeatureReaderWorker(dbWorkerPool, config, eventDispatcher);
}
 
Example #5
Source File: JAXBChunkReader.java    From citygml4j with Apache License 2.0 4 votes vote down vote up
public XMLChunk nextChunk() throws CityGMLReadException {
	if (iterator == null) {
		try {
			current = null;

			while (reader.hasNext()) {
				int event = reader.next();	

				// keep track of schema documents
				if (event == XMLStreamConstants.START_ELEMENT && parseSchema) {
					for (int i = 0; i < reader.getAttributeCount(); i++) {
						if (reader.getAttributeLocalName(i).equals("schemaLocation"))
							parseSchema(reader.getAttributeValue(i));					
						else if (reader.getAttributeLocalName(i).equals("noNamespaceSchemaLocation"))
							schemaHandler.parseSchema("", reader.getAttributeValue(i));
					}
				}

				// move to the next CityGML feature in the document
				// and create a new chunk to initiate parsing
				if (!isInited) {
					if (event != XMLStreamConstants.START_ELEMENT)
						continue;

					else {
						elementInfo = elementChecker.getElementInfo(reader.getName());

						if (elementInfo != null && elementInfo.isFeature()) {
							isInited = true;
							chunks.clear();

							chunk = new XMLChunkImpl(this, null);
							chunk.setFirstElement(reader.getName());

							if (isFilteredReader())
								chunk.setIsFiltered(!filter.accept(reader.getName()));
						} else
							continue;
					}						
				}

				// check whether start element is a feature
				if (event == XMLStreamConstants.START_ELEMENT) {
					ElementInfo lastElementInfo = elementInfos.push(elementInfo);
					elementInfo = elementChecker.getElementInfo(reader.getName(), lastElementInfo);

					if (elementInfo != null && elementInfo.isFeature()) {
						chunk.removeTrailingCharacters();
						chunks.add(chunk);

						chunk = new XMLChunkImpl(this, chunks.peek());
						chunk.setFirstElement(reader.getName());

						if (isFilteredReader())
							chunk.setIsFiltered(!filter.accept(reader.getName()));								

						if (lastElementInfo != null)
							setXLink = lastElementInfo.hasXLink();
					}
				}

				// pop last element info
				else if (event == XMLStreamConstants.END_ELEMENT)
					elementInfo = elementInfos.pop();

				// add streaming event to the current chunk 
				chunk.addEvent(reader);
				if (setXLink)
					setXLink(reader);

				// if the chunk is complete, return it
				if (chunk.isComplete()) {
					current = chunk;

					if (!chunks.isEmpty())
						chunk = chunks.pop();
					else {
						chunk = null;
						isInited = false;
					}

					if (!isFilteredReader() || !current.isFiltered())
						break;
					else
						current = null;
				}
			}
		} catch (XMLStreamException | SAXException | MissingADESchemaException e) {
			throw new CityGMLReadException("Caused by: ", e);
		}

		if (current == null)
			throw new NoSuchElementException();
	} else
		current = iterator;

	iterator = null;
	return current;
}
 
Example #6
Source File: MultithreadedReader.java    From citygml4j with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	SimpleDateFormat df = new SimpleDateFormat("[HH:mm:ss] "); 

	System.out.println(df.format(new Date()) + "setting up citygml4j context and CityGML builder");
	CityGMLContext ctx = CityGMLContext.getInstance();
	CityGMLBuilder builder = ctx.createCityGMLBuilder();
	
	// create a fixed thread pool
	int nThreads = Runtime.getRuntime().availableProcessors() * 2;
	System.out.println(df.format(new Date()) + "setting up thread pool with " + nThreads + " threads");
	ExecutorService service = Executors.newFixedThreadPool(nThreads);
	
	System.out.println(df.format(new Date()) + "reading LOD3_Railway_v200.gml in a multithreaded fashion");
	
	// create a validating reader that chunks the input file on a per feature level
	CityGMLInputFactory in = builder.createCityGMLInputFactory();
	in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_FEATURE);
	in.setProperty(CityGMLInputFactory.USE_VALIDATION, true);
	
	CityGMLReader reader = in.createCityGMLReader(new File("datasets/LOD3_Railway_v200.gml"));
	
	while (reader.hasNext()) {
		// whereas the nextFeature() method of a CityGML reader completely unmarshals the 
		// XML chunk to an instance of the citygml4j object model and optionally validates
		// it before returning, the nextChunk() method returns faster but only provides a
		// set of SAX events.		
		final XMLChunk chunk = reader.nextChunk();
		
		// we forward every XML chunk to a separate thread of the thread pool
		// for unmarshalling and validation
		service.execute(() -> {
               try {
                   chunk.unmarshal();
               } catch (UnmarshalException | MissingADESchemaException e) {
                   //
               }

               // invoking the hasPassedXMLValidation() method prior to unmarshal()
               // or when using a non-validating CityGML reader will always yield false.
               boolean isValid = chunk.hasPassedXMLValidation();

               System.out.println("Thread '" + Thread.currentThread().getName() + "' unmarshalled " + chunk.getCityGMLClass() + "; valid: " + isValid);
           });
	}
	
	System.out.println(df.format(new Date()) + "shutting down threadpool");
	service.shutdown();
	service.awaitTermination(120, TimeUnit.SECONDS);
	
	reader.close();
	System.out.println(df.format(new Date()) + "sample citygml4j application successfully finished");
}