HOWTO: Using the Oracle XMLDB Repository to Automatically Shred Windows Office Documents

People who have attended the UKOUG presentation this year where Mark Drake, Sr. Product Manager XML Technologies / XMLDB, Oracle HQ, and I demonstrated the first principles of the XDB Repository, might have been impressed with its (GEO/KML Spatial, Image EXIF info) capabilities combined with Google Earth. This post will zoom in on how to consume automatically content of Windows Office document (docx).

Most (APEX) people know the PL/SQL Gateway functionality of the XDB Protocol Listener, but this is only one very small part of the XDB Repository functionality. To be precise only one “servlet” part of it. Those “servlets” can be based on Java, C or PL/SQL. The PL/SQL Gateway, as it’s name suggests, is based on the PL/SQL part. Another “servlet”, the Native Database Web Service (NDWS), which enables you to create a database SOA endpoint service and more, is based on C code. Beside demonstrating the WebDAV ACL driven security features and database extensibility/interfacing facilities based on the database (no cost option) XMLDB functionality, it also explain one of the coolest features, IMHO, introduced in Oracle 11gR1 called: XDB Repository Events.

There is one big problem in all of this. It is very, very sparsely documented and although there is some, or was some, code out there on the worldwide web, it took me a while to get a feeling of all the specifics at hand. This post will extend on some of the posts I already created to give to a head start of what is possible. This post will demonstrate what you can do with Windows Office documents, nowadays embedded (zipped) XML content, with extentions called: docx, xlsx or pptx…

The code here is an alternative, rewritten in PL/SQL (because we can…) out there on the internet, which is based on Java called “Oracle OpenWorld 2010 Oracle XML DB Repository Demo: MS Office Integration” (Oct. 2010 – Thomas Baby, Principal Developer, Oracle HQ). It demonstrates the use of the XDB Repository, XDB Repository Events and methods that can be used to query, via XML, the content of some Microsoft Word example docx documents. To make this “simple” example work in PL/SQL we need, once again:

  1. An Event Listener
  2. An Event Handler
  3. A Resource Configuration file

An event listener, is a PL/SQL package or Java Class, that handles the XDB “events”. An Event handler is the, in PL/SQL, procedure that handles a single event (e.g. “pre create”, “post create”, “pre delete”, etc). A Resource configuration file is an XML file, that acts as the glue between the XDB Repository file and folders (called “resources” in WebDAV terminology), so for example it determines which actions happen based on which XDB Repository Events, on which pre conditions (if defined, e.g. only for “jpg” files or which specific folder), under which (security) conditions, etc. As stated during the presentation mentioned, your imagination is the limit on what you would like to do.

Besides needing the XDB Repository Event strategy setup, we need for this example also:

  1. A folder structure
  2. Anton‘s Zip/Unzip package: AS_ZIP (with some small alterations)
  3. The “Event Handler” PL/SQL package, called: HANDLE_XDB_EVENTS

Setting up the Environment

For ease, let’s create a simple user account called EVENTS and create via this account the used XDB Repository folder structure.

[code language=”sql”]
SQL> create user events identified by events account unlock;
SQL> grant connect, resource, xdbadmin, xdb_set_invoker to events;
[/code]

Granted roles, CONNECT and RESOURCE will be used, among others, to be able to connect to the database, to create the needed PL/SQL code and table(s). The XDBADMIN role is needed if we would like to alter the xdbconfig.xml XDB Protocol listener configuration file that controls its behavior. The XDB_SET_INVOKER role will not be explicitly be used in this example, but could be used to further tighten security regarding call of code, via the resource configurartion file, of the defined event listener.

So now we have created the EVENTS user account, lets connect to the database with it and create the XDB Repository Folder/Resource structures. The following will create a /xdb main root folder, with underneath defined folders “upload“, “download” and a “res” folder where we will create our resource configuration XML file (and secure it if needed against public access).

[code language=”sql”]SQL> connect events/events

DECLARE

TYPE v_ntt IS TABLE OF VARCHAR2(1024);
v_res v_ntt := v_ntt( ‘/xdb/’
, ‘/xdb/upload/’
, ‘/xdb/res/’
, ‘/xdb/download/’
);

v_boolean BOOLEAN;
v_exist EXCEPTION;

PRAGMA EXCEPTION_INIT(v_exist, -31003);

BEGIN

FOR i IN v_res.FIRST .. v_res.LAST
LOOP

BEGIN
v_boolean := DBMS_XDB.CREATEFOLDER(v_res(i));
EXCEPTION
WHEN v_exist THEN NULL;
END;

END LOOP;

COMMIT;

END;
/[/code]
Be aware that whatever you do now via the EVENTS database account, the default WebDAV ACL security will assign EVENTS as the owner. I mention this explicitly because sometimes you will be confused why, for example, a DBA role privileged user might not be able to see certain XDB Repository resources. This is because the security implementation of XMLDB functionality in the database will be enforced via Oracle’s Virtual Database functionality (DBMS_RLS), which might prevent you to see or access things, based on database USER, ROLE, or WebDAV access. It is possible to create some structures which can not be seen or dropped afterwards due to your own implemented restrictions (or enforced via the environment).

As you might know, its very easy to create files and folders in the XDB Repository via for example PL/SQL packages DBMS_XDB, DBMS_RESOURCE or via FTP or WebDAV access (enabled in all Microsoft Office documents or via Windows Explorer). Those “resources”, file and folder, can then be accessed also via HTTP(s) if needed. The WebDAV access to the database can also be opened up via using the opensource DAVFS code or via a piece of software in Windows that can be used to mount the database, via WebDAV, directly as a file share, if needed.

So…now we have those directory structures and we can query these structures from within the database.
[code language=”sql”]
SQL> SELECT path(1) as “Relative Path”
2 FROM RESOURCE_VIEW
3 WHERE under_path(RES, 1, ‘/xdb’, 1) = 1;

Relative Path
—————-
download
res
upload

3 rows selected.

SQL> SELECT any_path as “Absolute Path”
2 FROM RESOURCE_VIEW
3 WHERE under_path(RES, ‘/xdb’) = 1;

Absolute Path
—————-
/xdb/download
/xdb/res
/xdb/upload

3 rows selected.
[/code]
Beside methods like using XDBURITYPE, you are also access the XDB Repository via one of the by Oracle defined views, PATH_VIEW or RESOURCE_VIEW, directly via SQL or PL/SQL.

XDB Repository Events

Let’s say we want all files that are be created in the folder /xdb/upload or be handled via a Event Listener package. This Event Listener, for now called HANDLE_XDB_EVENTS, will do specific stuff for us with those resources created in /xdb/upload like picking up the content it and inserting the needed content for further use automatically in a table. We have to instruct the XDB Repository Event framework to act on those specific needs by defining them in a resource configuration file. This configuration file will be created in the /xdb/res directory folder.
[code language=”sql”]DECLARE
b BOOLEAN := FALSE;
BEGIN
b := DBMS_XDB.createResource(
‘/xdb/res/UploadHandling.xml’,

OOXML Application
EVENTSHANDLE_XDB_EVENTS
PL/SQL



/xdb/res/UploadHandling.xml


‘);
END;
/
[/code]

The package used here DBMS_XDB creates a resource (file) and needs as input the absolute path plus the content that will be in this file. So in this case our file will be created in /xdb/res and will have as filename: UploadHandling.xml (/xdb/res/UploadHandling.xml). The needed content of this resource configuration XML file defines in this example:

  • The event listener name in use: HANDLE_XDB_EVENTS PL/SQL Package
  • The package owner: EVENTS
  • The source flavor: PL/SQL
  • A description
  • The events that will trigger the package to be executed: Post-LinkedIn, Post-Update, Render
  • All resources, childs, will be associated with this resource file automatically via being defined in the “defaultChildConfig” element section

Be aware of the references, in the XML content of the resource configuration file, to the XDBResConfig.xsd XML Schema. The content MUST follow the outlines defined in this XML schema. Just like a lot of other XMLDB configuration and structures, this and other important XML Schema’s are already registered in the XMLDB environment. No extra effort is needed. You read about its contents in the appendix of the XMLDB Developers Guide for your database version or can access it directly via a standard method like using the XDBURIFILE function.

In my situation I already created a “dummy” package with the name HANDLE_XDB_EVENTS. I come back later on the specifics regarding content needed…

The next step is to associate this resource configuration to the file or folder were we need it. In this example we want the HANDLE_XDB_EVENT package also ONLY to be triggered/called via XDB Repository Events when files or folders are created in the /xdb/upload folder. No where else. This is achieved by associating the resource configuration file to the actual folder via DBMS_RESCONFIG.

[code language=”sql”]
BEGIN
DBMS_RESCONFIG.appendResConfig(
‘/xdb/upload’,
‘/xdb/res/UploadHandling.xml’, DBMS_RESCONFIG.APPEND_RESOURCE);
END;
/
[/code]

We “append” our event listener configuration needs, because there are already event listeners in place (in Oracle 11gR2 XE at least) that handle extended security methods.

[code language=”sql”]
SQL> SELECT any_path as “Absolute Path”
2 FROM RESOURCE_VIEW
3 WHERE under_path(RES, 1, ‘/xdb/res’, 1) = 1;

Absolute Path
—————-
/xdb/res/UploadHandling.xml

1 row selected.

SQL> select xdburitype(‘/xdb/res/UploadHandling.xml’).getCLOB() as “ResConfig” from dual;

ResConfig
—————————————————————–

OOXML Application
EVENTSHANDLE_XDB_EVENTS
PL/SQL






[/code]

To get some info on which Event Handlers are defined on a resource you can use the GETLISTENER function of package DBMS_RESCONFIG. This will output the result in XMLTYPE which is very convenient because that enables use to feed the result directly into functions like XMLTABLE so we can query and format it via XPATH to get the needed info.

[code language=”sql”]
SQL> select xt.schema
2 , xt.source
3 , xt.description
4 FROM XMLTABLE (xmlnamespaces(default ‘http://xmlns.oracle.com/xdb/XDBResConfig.xsd’)
5 , ‘/event-listeners/listener’
6 PASSING (DBMS_RESCONFIG.GETLISTENERS(‘/xdb/upload’))
7 COLUMNS
8 schema VARCHAR2(30) PATH ‘schema’
9 , source VARCHAR2(30) PATH ‘source’
10 , description VARCHAR2(100) PATH ‘description’
11 ) xt
12 WHERE xt.schema = user;

SCHEMA SOURCE DESCRIPTION
——- —————– —————–
EVENTS HANDLE_XDB_EVENTS OOXML Application

1 row selected.
[/code]

or even a bit more complex…
[code language=”sql”]
SQL> select xt.schema
2 , xt.eventlistener
3 , xt.language
4 , xe.roworder
5 , xe.event
6 FROM XMLTABLE (xmlnamespaces(default ‘http://xmlns.oracle.com/xdb/XDBResConfig.xsd’)
7 , ‘/event-listeners/listener’
8 PASSING (DBMS_RESCONFIG.GETLISTENERS(‘/xdb/upload’))
9 COLUMNS
10 schema VARCHAR2(30) PATH ‘schema’
11 , eventlistener VARCHAR2(30) PATH ‘source’
12 , language VARCHAR2(10) PATH ‘language’
13 , childevents XMLTYPE PATH ‘events’
14 ) xt
15 , XMLTABLE (xmlnamespaces(default ‘http://xmlns.oracle.com/xdb/XDBResConfig.xsd’)
16 , ‘/events/*’
17 PASSING xt.childevents
18 COLUMNS
19 roworder FOR ORDINALITY
20 , event VARCHAR2(25) PATH ‘fn:local-name(.)’
21 ) xe
22 WHERE xt.schema = user;

SCHEMA EVENTLISTENER LANGUAGE ROWNUMBER EVENT
——- —————– ——– ——— ———–
EVENTS HANDLE_XDB_EVENTS PL/SQL 1 Post-LinkIn
EVENTS HANDLE_XDB_EVENTS PL/SQL 2 Post-Update
EVENTS HANDLE_XDB_EVENTS PL/SQL 3 Render

3 rows selected.
[/code]

So now we created the folder structure, a dummy package that will act as the event listener with the name HANDLE_XDB_EVENTS, a user account that owns the structures, and associated a resource configuration XML file that enforces now the execution of the HANDLE_XDB_EVENTS when events “Post-LinkedIn, Post-Update, Render” are detected by the XDB Event framework.

Accessing Microsoft Word Content

To be able to do everything in PL/SQL, I needed a ZIP/UNZIP method. So I asked my colleague Anton Scheffer, if he knew how to do this. Anton was busy at the time with his own PDF PL/SQL generator package, and this small section (zip/unzip) was one of the things he needed himself, so guess who the “colleague” is mentioned in Anton’s post: “Parsing a Microsoft Word docx, and unzip zipfiles, with PL/SQL“. Anton’s example takes a Windows Word/docx document from the fileserver as input, but we (Mark Drake and I) do our database work, as long as possible, in the database, so in our case we need this package (AS_ZIP) to work with the XDB Repository. Mark overloaded the ZIP and UNZIP procedures with pipelined table functionality, with the needed extra TYPE creation.

[code language=”sql”]
create or replace type ZIP_CONTENTS_LIST
as TABLE of VARCHAR2(4000)
/
create or replace type ZIP_ENTRY force as OBJECT
(
FILENAME VARCHAR2(4000),
CONTENT BLOB
)
/
create or replace type ZIP_ENTRY_TABLE as table of ZIP_ENTRY
/
[/code]

[code language=”sql”]

CREATE OR REPLACE package as_zip
is
/*
** Written by Anton Scheffer
** 3 April 2011
*/

function get_file_list(
p_dir in varchar2
, p_zip_file in varchar2
, p_encoding in varchar2 := null
)
return zip_contents_list;

function get_file_list(
p_zipped_blob in blob
, p_encoding in varchar2 := null /* Use CP850 for zip files created with a German Winzip to see umlauts, etc */
)
return zip_contents_list;

function get_file(
p_dir in varchar2
, p_zip_file in varchar2
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob;

function get_file(
p_zipped_blob in blob
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob;

function unzip(
p_zipped_blob in blob
, p_encoding in varchar2 := null
)
return zip_entry_table pipelined;

function unzip(
p_dir in varchar2
, p_zip_file in varchar2
, p_encoding in varchar2 := null
)
return zip_entry_table pipelined;

procedure add1file(
p_zipped_blob in out blob
, p_name in varchar2
, p_content in blob
);

procedure finish_zip(
p_zipped_blob in out blob
);

procedure save_zip(
p_zipped_blob in blob
, p_dir in varchar2 := ‘MY_DIR’
, p_filename in varchar2 := ‘my.zip’
);
[/code]

So now we can use Anton’s unzip package and get the needed info returned via a pipelined table function, defined on the user type definition ZIP_ENTRY which will return the FILENAME and the CONTENT.

[code language=”sql”]
user type definition
——————————
type ZIP_ENTRY force as OBJECT
(
FILENAME VARCHAR2(4000),
CONTENT BLOB
)
[/code]

Knowing that an XMLTYPE datatype can handle a lot of different datatypes (BFILE, BLOB, AnyData, CLOB, Ref Cursor, etc) , the rest is fairly “easy”. The following will give us the filename (VARCHAR2(4000)) and the content (BLOB) of the file in the XDB Repository.

[code language=”sql”]
select filename, content
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())));
[/code]

To make “content”, currently a BLOB, an XMLTYPE datatype, we have to feed it also NLS_CHARACTERSET info. This can be done via, for example:
[code language=”sql”]
select filename
, xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as DOCX
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
where filename = ‘word/document.xml’;
[/code]

So now we have as a result, a filename, an XMLType based content with as the source the document.xml file which is located in the (zipped) Word document in the XDB Repository called BM123Manual.docx which resides in the directory folder /xdb/download. If you drag and drop, for example via WebDAV and Windows Explorer, this example Word document from the original OTN Java based source in this repository folder and execute the query via SQL*Plus or SQL*Developer, then the result is:

[code language=”sql”]
select filename
, xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as DOCX
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
where filename = ‘word/document.xml’;

FILENAME
—————-
word/document.xml

DOCX
————————————————————————————-
< ?xml version="1.0" encoding="UTF-8" standalone="yes"?>
http://schemas.openxmlformats.org/markup-compatibility/2006″ xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:r=”http://schemas.openxmlformats.org/officeDocument/200” xmlns:m=”http://schemas.openxmlformats.org/officeDocument/2006/math” xmlns:v=”urn:schemas-microsoft-com:vml” xmlns:wp=”http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing” xmlns:w10=”urn:schemas-microsoft-com:office:word” xmlns:w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main” xmlns:wne=”http://schemas.microsoft.com/office/word/2006/wordml”> w:val=”Title”/>Operating Manual for BM123By Bob, TomTechnical SpecOn the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document lookYou can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.
[/code]

Not that very easy to read, useful, yet…but it becomes a bit more interesting if you query it (the CONTENT) via XQuery / XPath. The following gives you already a bit more (although concatinated still) info:

[code language=”sql”]
select extract((xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)))
, ‘//w:t/text()’
, ‘xmlns:w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”‘
) as “docx”
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
where filename = ‘word/document.xml’;

DOCX
————————————————————————————-
Operating Manual for BM123By Bob, TomTechnical SpecOn the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document lookYou can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.
[/code]

This content could, for example, already be used for a Oracle Text Index search, combined with Oracle XMLIndex indexes to enable a quicker search on the XML element and attributes (in this case the “w:t” section).

The usefulness becomes even more evident, I think, when you do stuff like

[code language=”sql”]

— PRINT ALL AVAILABLE TAGS

SELECT DISTINCT(BOOK_INFO.TAG)
FROM (select xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as OBJECT_VALUE
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
— where filename = ‘word/document.xml’
) BOOKS
, XMLTABLE(XMLNAMESPACES(
‘http://schemas.openxmlformats.org/wordprocessingml/2006/main’ AS “w”), ‘/w:document/w:body//w:sdt’
PASSING BOOKS.OBJECT_VALUE
COLUMNS TAG VARCHAR2(100) PATH ‘/w:sdt/w:sdtPr/w:tag/@w:val’
) BOOK_INFO;

TAG
————–
Abstract
Title
Author
Category

4 rows selected.
[/code]

[code language=”sql”]

— FIND TITLE OF ALL BOOKS IN THE “Technical Spec” CATEGORY

VAR CATEGORY VARCHAR2(20);
EXECUTE :CATEGORY := ‘Technical Spec’;

SELECT XMLQUERY(‘declare namespace w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”; /w:document/w:body//w:sdt[w:sdtPr/w:tag/@w:val=”Title”]/w:sdtContent//w:r//w:t//text()’
PASSING BOOKS.OBJECT_VALUE RETURNING CONTENT) TITLE
FROM (select xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as OBJECT_VALUE
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
— where filename = ‘word/document.xml’
) BOOKS
WHERE XMLEXISTS(‘declare namespace w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”; /w:document/w:body//w:sdt[w:sdtPr/w:tag/@w:val=”Category” and w:sdtContent//w:r//w:t//text()=$CATEGORY]’
PASSING BOOKS.OBJECT_VALUE,
:CATEGORY AS “CATEGORY”);
/

TITLE
—————————
Operating Manual for BM123

1 row selected.
[/code]

[code language=”sql”]

— RETURN BOOK TITLE, AUTHORS AND CATEGORY IN XML FORMAT

SELECT XMLQUERY(‘declare namespace w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”;
{$BOOK//w:document/w:body//w:sdt[w:sdtPr/w:tag/@w:val=”Title”]/w:sdtContent//w:r//w:t//text()}
{$BOOK//w:document/w:body//w:sdt[w:sdtPr/w:tag/@w:val=”Author”]/w:sdtContent//w:r//w:t//text()}
{$BOOK//w:document/w:body//w:sdt[w:sdtPr/w:tag/@w:val=”Category”]/w:sdtContent//w:r//w:t//text()}

PASSING BOOKS.OBJECT_VALUE AS “BOOK” RETURNING CONTENT) RESULT
FROM (select xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as OBJECT_VALUE
from table(as_zip.unzip((xdburitype(‘/xdb/download/BM123Manual.docx’).getBLOB())))
where filename = ‘word/document.xml’
) BOOKS
;

RESULT
————————
Operating Manual for BM123Bob, TomTechnical Spec
[/code]

Missing Links

So where we? We have the XDB Repository set-up. We know now how to extract information from a Windows Office document. We enabled the XDB Repository Event framework for folder /xdb/upload. We now need to specify the actions needed in package (Event Listener) HANDLE_XDB_EVENTS.

The events specified in the Resource Configuration file in /xdb/res/UploadHandling.xml, Post-LinkIn, Post-Update, Render, have to be handled by Event Handlers, that is procedures defined in the package, handling the specified events. The procedure naming is very specific and Case Sensitive. It took me a while to figure out those procedure names, until I saw the following Java document describing the “XDBRepositoryEventListener“. Your procedure names have to match these names. When you do, in the most extended form, you will get something like the following for the events defined in 11gR2 XE environment (derived from the XDBResConfig.xml content): “Intro into XDB Repository Events (OTN XMLDB Forum)“.

After creating a DOCSTORE table like the following and index the XML content via…

[code language=”sql”]
CREATE TABLE DOCSTORE
( filename VARCHAR2(4000)
, zipfilename VARCHAR2(4000)
, xmlcontent XMLTYPE)
TABLESPACE USERS
XMLTYPE xmlcontent STORE AS SECUREFILE BINARY XML
/
CREATE INDEX DOCSTORE_XMLINDEX ON DOCSTORE(xmlcontent)
INDEXTYPE IS XDB.XMLINDEX
PARAMETERS (‘PATHS( INCLUDE( /w:document/w:body//w:sdt //w:tbl )
NAMESPACE MAPPING
(xmlns:w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”))’
)
/
[/code]

You can now create/define the appropriate procedures in the package that handle the defined events. So, while I kept for demonstration reasons all the procedures in tact in the package, the package body contains a procedure called “handlePostLinkIn” (one of the triggered events) that does nothing else to call the main procedure “handleMain”.
[code language=”sql”]
PROCEDURE handlePostLinkIn(
P_EVENT dbms_xevent.XDBRepositoryEvent)
AS
BEGIN

handleMain(P_EVENT);

END handlePostLinkIn;
[/code]

This “handleMain” procedure in the package body, executes the needed actions, when, via the DBMS_XEVENT package, Oracle recognizes that the “Post-LinkedIn” event has been triggered. The handleMain procedure has a case statement that does just that…
[code language=”sql”]


— ——————————————
— Associate fired event with action
— ——————————————
CASE V_EVENT_TYPE
WHEN dbms_xevent.RENDER_EVENT THEN
V_EVENT_NAME := ‘Render’;
WHEN dbms_xevent.PRE_CREATE_EVENT THEN
V_EVENT_NAME := ‘PreCreate’;
WHEN dbms_xevent.POST_CREATE_EVENT THEN
V_EVENT_NAME := ‘PostCreate’;
WHEN dbms_xevent.PRE_DELETE_EVENT THEN
V_EVENT_NAME := ‘PreDelete’;
WHEN dbms_xevent.POST_DELETE_EVENT THEN
V_EVENT_NAME := ‘PostDelete’;
WHEN dbms_xevent.PRE_UPDATE_EVENT THEN
V_EVENT_NAME := ‘PreUpdate’;
WHEN dbms_xevent.POST_UPDATE_EVENT THEN
V_EVENT_NAME := ‘PostUpdate’;
WHEN dbms_xevent.PRE_LOCK_EVENT THEN
V_EVENT_NAME := ‘PreLock’;
WHEN dbms_xevent.POST_LOCK_EVENT THEN
V_EVENT_NAME := ‘PostLock’;
WHEN dbms_xevent.PRE_UNLOCK_EVENT THEN
V_EVENT_NAME := ‘PreUnlock’;
WHEN dbms_xevent.POST_UNLOCK_EVENT THEN
V_EVENT_NAME := ‘PostUnlock’;
WHEN dbms_xevent.PRE_LINKIN_EVENT THEN
V_EVENT_NAME := ‘PreLinkIn’;
WHEN dbms_xevent.POST_LINKIN_EVENT THEN

V_EVENT_NAME := ‘PostLinkIn’;

insert into docstore
(filename, zipfilename, xmlcontent)
select v_resource_path
, filename
, xmltype(content,NLS_CHARSET_ID(‘AL32UTF8’)) as xmlcontent
from table(as_zip.unzip((xdburitype(v_resource_path).getBLOB())))
where filename = ‘word/document.xml’;

WHEN dbms_xevent.PRE_LINKTO_EVENT THEN
V_EVENT_NAME := ‘PreLinkTo’;
WHEN dbms_xevent.POST_LINKTO_EVENT THEN
V_EVENT_NAME := ‘PostLinkTo’;
WHEN dbms_xevent.PRE_UNLINKIN_EVENT THEN
V_EVENT_NAME := ‘PreUnlinkIn’;
WHEN dbms_xevent.POST_UNLINKIN_EVENT THEN
V_EVENT_NAME := ‘PostUnlinkIn’;
WHEN dbms_xevent.PRE_UNLINKFROM_EVENT THEN
V_EVENT_NAME := ‘PreUnlinkFrom’;
WHEN dbms_xevent.POST_UNLINKFROM_EVENT THEN
V_EVENT_NAME := ‘PostUnlinkFrom’;
WHEN dbms_xevent.PRE_CHECKIN_EVENT THEN
V_EVENT_NAME := ‘PreCheckIn’;
WHEN dbms_xevent.POST_CHECKIN_EVENT THEN
V_EVENT_NAME := ‘PostCheckIn’;
WHEN dbms_xevent.PRE_CHECKOUT_EVENT THEN
V_EVENT_NAME := ‘PreCheckOut’;
WHEN dbms_xevent.POST_CHECKOUT_EVENT THEN
V_EVENT_NAME := ‘PostCheckOut’;
WHEN dbms_xevent.PRE_UNCHECKOUT_EVENT THEN
V_EVENT_NAME := ‘PreUncheckOut’;
WHEN dbms_xevent.POST_UNCHECKOUT_EVENT THEN
V_EVENT_NAME := ‘PostUncheckOut’;
WHEN dbms_xevent.PRE_VERSIONCONTROL_EVENT THEN
V_EVENT_NAME := ‘PreVersionControl’;
WHEN dbms_xevent.POST_VERSIONCONTROL_EVENT THEN
V_EVENT_NAME := ‘PostVersionControl’;
WHEN dbms_xevent.PRE_OPEN_EVENT THEN
V_EVENT_NAME := ‘PreOpen’;
WHEN dbms_xevent.POST_OPEN_EVENT THEN
V_EVENT_NAME := ‘PostOpen’;
WHEN dbms_xevent.PRE_INCONSISTENTUPDATE_EVENT THEN
V_EVENT_NAME := ‘PreInconsistentUpdate’;
WHEN dbms_xevent.POST_INCONSISTENTUPDATE_EVENT THEN
V_EVENT_NAME := ‘PostInconsistentUpdate’;
ELSE
V_EVENT_NAME := ‘UnknownEvent’;
END CASE;


[/code]

Now, everything kicks into effect. If you drag and drop a docx word document into /xdb/upload, the content will automatically of the embedded word/document.xml file, via the Post-LinkedIn event, inserted into the DOCSTORE table. So if you drag and drop, for example, 2Word documents into /xdb/upload, the DOCSTORE table will contain/show the following:
[code language=”sql”]
describe docstore

Name Null Type
———– —- ————–
FILENAME VARCHAR2(4000)
ZIPFILENAME VARCHAR2(4000)
XMLCONTENT XMLTYPE()

SQL> select * from docstore

FILENAME ZIPFILENAME XMLCONTENT
—————————— —————– ————————————
/xdb/download/test.docx word/document.xml < ?xml version = '1.0' encoding = ... /xdb/download/BM123Manual.docx word/document.xml References

HTH

Marco

Marco Gralike Written by:

4 Comments

  1. December 21

    Funny, that’s exactly what I’m working on right now but on OOXML spreadsheets (xlsx), hence my post today on OTN 😉
    I didn’t know you were presenting it this year.
    I too opted for a PL/SQL unzip method as this was primarily meant to run on XE, and of course XDB events to automatically populate workbook, worksheet and shareStrings tables.

Comments are closed.