# rdf-parser-abstract

> Abstract base class for RDF Interface parser implementations

Latest version **0.3.2** (published 2017-03-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install rdf-parser-abstract
pnpm add rdf-parser-abstract
yarn add rdf-parser-abstract
bun add rdf-parser-abstract
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2017-03-04 |
| First published | 2015-11-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf-parser, rdf, linked, data |

## Links

- npm: https://www.npmjs.com/package/rdf-parser-abstract
- Repository: https://github.com/rdf-ext/rdf-parser-abstract
- Issues: https://github.com/rdf-ext/rdf-parser-abstract/issues
- npm.io page: https://npm.io/package/rdf-parser-abstract

## Dependencies (3)

- [inherits](https://npm.io/package/inherits.md) ^2.0.1
- [concat-stream](https://npm.io/package/concat-stream.md) ^1.5.0
- [readable-stream](https://npm.io/package/readable-stream.md) ^2.2.3

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.3.2 (latest) — 2017-03-04
- 0.3.0-rc1 (unstable) — 2015-11-05
- 0.3.1 — 2016-09-07
- 0.3.0 — 2016-01-14

## README

# rdf-parser-abstract

[![Build Status](https://travis-ci.org/rdf-ext/rdf-parser-abstract.svg?branch=master)](https://travis-ci.org/rdf-ext/rdf-parser-abstract)
[![NPM Version](https://img.shields.io/npm/v/rdf-parser-abstract.svg?style=flat)](https://npm.im/rdf-parser-abstract)

Abstract base class for RDF-Interfaces parser implementations.

## Usage

The `AbstractParser` class adds default implementations for `.parse(data, callback, base, filter, graph)` and `.stream(inputStream, base, filter)`.
Only the `.process(data, callback, base, filter, done)` method must be implemented.

Your own parser hast to inherit from the abstract parser.
First let's install the package.
Start a terminal and run:

	npm install rdf-abstract-parser --save

Now let's start coding.
To inherit from the abstract parser, we have to load the inherits and the `AbstractParser` itself first:

	var inherits = require('inherits')
	var AbstractParser = require('rdf-abstract-parser')

Your parser must accept the optional parameter `rdf` to use the given RDF environment.
To initialize the `AbstractParser`, you have to call the `AbstractParser` constructor in your own constructor.

	function YourParser (options) {
	  // use the given RDF environment or RDF-Ext if none was given
	  this.rdf = options.rdf || require('rdf-ext')

    // call the AbstractParser constructor
	  AbstractParser.call(this, rdf)
	}

After the constructor code you have to inherit with the `inherits` function.

	inherits(YourParser, AbstractParser)

Only the `.process` method is required to be implemented.
The `base` parameter is optional, let's check if it is given and createt a `NamedNode` from the string, so we can use it for the `graph` property for the quads.
The `filter` parameter is optional, so let's assign a accept all triples filter, if none was given.
Finally you can implement your parser code.
The example code accepts strings with one triple per line and spo space separated and only named node without additional escaping.
To parse the string we only have to split the string per line and than split every line into spo.
For every spo we create a quad with the RDF environment and check if the filter accepts the quad.
If the filter accepts the quad we can forward it.
That's it, your parser is complete!

	YourParser.prototype.process = function (data, callback, base, filter, done) {
	  var self = this

    // create a NamedNode if the base parameter is given
    base = !!base ? null : self.rdf.createNamedNode(base.toString())

	  // use a accept all triples filter, if none was given
	  filter = filter || function () {
	    return true
	  }

	  // split string into triples
	  data.split('\n').forEach(function (line) {
	    // split triple into spo
	    var spo = line.split(' ')

	    // create the Quad using the RDF environment
	    var quad = self.rdf.createQuad(
	      self.rdf.createNamedNode(spo[0]),
	      self.rdf.createNamedNode(spo[1]),
	      self.rdf.createNamedNode(spo[2]),
	      base)

      // only forward the quad if the filter accepts it
	    if (filter(quad)) {
	      // forward the quad
      	callback(quad)
      }
	  })
	}

---
_Source: https://npm.io/package/rdf-parser-abstract · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
