Skip to content

Commit

Permalink
schema index html cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cwbhhjl committed May 19, 2020
1 parent 402e70d commit 2746ff8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
{
"id": "step",
"extensions": [
".ifc",
".stp",
".p21",
".step"
".ifc"
],
"aliases": [
"STEP",
Expand Down
107 changes: 95 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,41 @@ import htmlparser = require('htmlparser2');
import HtmlTableToJson = require('html-table-to-json');
import xpath = require('xpath');
import xmldom = require('xmldom');
import crypto = require('crypto');
import fs = require('fs');
import path = require('path');

const STEP_MODE: vscode.DocumentFilter = { language: 'step', scheme: 'file' };
const NOT_DIG_REGEXP = new RegExp(/\D/);

const IFC_SCHEMA_URL = {
'IFC2X3': [
new URL('http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/alphabeticalorder_selecttype.htm'),
new URL('http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/alphabeticalorder_enumtype.htm'),
new URL('http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/alphabeticalorder_definedtype.htm'),
new URL('http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/alphabeticalorder_entities.htm')
new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_selecttype.htm'),
new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_enumtype.htm'),
new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_definedtype.htm'),
new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_entities.htm')
],
'IFC4': [new URL('http://www.buildingsmart-tech.org/ifc/IFC4/final/html/toc.htm')],
'IFC4X1': [new URL('http://www.buildingsmart-tech.org/ifc/IFC4x1/final/html/toc.htm')]
'IFC4': [new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC4/FINAL/HTML/toc.htm')],
'IFC4X1': [new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/toc.htm')]
};

const IFC_SCHEMA_URL_HASH = {
'IFC2X3': [
{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_selecttype.htm'), hash: 'cac41d9aa3e74ab5dd62c1b6f4ea6366' },
{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_enumtype.htm'), hash: '49d9b8d46b2ced2a73aad9b19890435f' },
{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_definedtype.htm'), hash: 'b449322901b62b4eba7768fb15b9ef90' },
{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/alphabeticalorder_entities.htm'), hash: '10b4daef8ea0ff80dc5860eb438b89da' }
],
'IFC4': [{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC4/FINAL/HTML/toc.htm'), hash: 'f36a4afd3b1c52628d85c2f84d512825' }],
'IFC4X1': [{ url: new URL('https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/toc.htm'), hash: 'b2b3debdd13a7918d89700b0b83e83f0' }]
}

var CURRENT_SCHEMA = '';

const EXTENSION_PATH = vscode.extensions.getExtension('cwbhhjl.step-ref').extensionPath;
const WEBCACHE_PATH_RELATIVE_EX = './tmp/webcache';
const WEBCACHE_PATH = path.join(EXTENSION_PATH, WEBCACHE_PATH_RELATIVE_EX);

const REQUEST_HEADER = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
Expand All @@ -35,6 +53,35 @@ const REQUEST_HEADER = {
'Cache-Control': 'no-cache'
};

function makeSchemaCache(cachePath: string): void {
for (let key in IFC_SCHEMA_URL_HASH) {
let urls = IFC_SCHEMA_URL_HASH[key];
for (let index = 0; index < urls.length; index++) {
let u = urls[index];
let cacheFile = path.join(cachePath, u.hash);
fs.exists(cacheFile, (exists) => {
if (!exists) {
request(u.url.href).pipe(fs.createWriteStream(cacheFile, { autoClose: true }));
}
});
}
}
}

function mkdirs(dirname: string, callback: (error: NodeJS.ErrnoException) => void): void {
fs.exists(dirname, exists => {
if (exists) {
callback(undefined);
} else {
mkdirs(path.dirname(dirname), () => {
fs.mkdir(dirname, callback);
});
}
});
}

//var test = crypto.createHash('md5').update(exPath, 'utf8').digest('hex');

function getStepReferenceIdFromDocument(document: TextDocument, position: Position): string {
let wordAtPosition = document.getWordRangeAtPosition(position, /#[0-9]+/);
if (wordAtPosition) {
Expand Down Expand Up @@ -110,16 +157,38 @@ async function getIfcTypeHrefFromHtml(content: string, typeName: string): Promis
});
}

async function getIfcSchemaHtml(url: URL): Promise<string> {
let cacheName = crypto.createHash('md5').update(url.href, 'utf8').digest('hex');
let fileCachePath = path.join(WEBCACHE_PATH, cacheName);
return new Promise<string>((rev, rej) => {
fs.exists(fileCachePath, exists => {
if (!exists) {
request(url.href).pipe(fs.createWriteStream(fileCachePath, { autoClose: true }));
request(url.href, async (error, response, body) => {
if (error) {
rej(`error: ${error}; response: ${response}`);
} else {
rev(body);
}
});
} else {
fs.readFile(fileCachePath, "utf8", (err, data) => {
if (err) rej(err);
rev(data);
})
}
});
});
}

async function getIfcTypeHyperLink(typeName: string, schema?: string): Promise<string> {
let schemaUrls = getIfcSchemaUrls(schema);
return new Promise<string>((rev, rej) => {
for (let index = 0; index < schemaUrls.length; index++) {
request(schemaUrls[index].href, async (error, response, body) => {
if (error) {
rej(`error: ${error}; response: ${response}`);
} else {
rev(new URL(await getIfcTypeHrefFromHtml(body, typeName), schemaUrls[index]).href);
}
getIfcSchemaHtml(schemaUrls[index]).then(async (value) => {
rev(new URL(await getIfcTypeHrefFromHtml(value, typeName), schemaUrls[index]).href);
}).catch(reason => {
rej(reason);
});
}
});
Expand Down Expand Up @@ -263,6 +332,18 @@ class SchemaInfoController {
}
}

function preTask() {
let webcachePath = WEBCACHE_PATH;

mkdirs(webcachePath, (error) => {
if (error) {
console.log(error);
} else {
makeSchemaCache(webcachePath);
}
});
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
Expand All @@ -273,6 +354,8 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerHoverProvider(STEP_MODE, new StepHoverProvider()));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(STEP_MODE, new StepDefinitionProvider()));
context.subscriptions.push(new SchemaInfoController());

preTask();
}

// this method is called when your extension is deactivated
Expand Down

0 comments on commit 2746ff8

Please sign in to comment.