├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── LICENSE ├── README.md ├── documentation └── user │ └── docx-html-editor_UserManual.docx ├── legal └── agpl-3.0.txt ├── pom.xml └── src └── main ├── java ├── docx2xhtml_Bare.xslt ├── docx2xhtml_CKEditor2013.xslt └── org │ └── plutext │ └── htmleditor │ ├── CK3ImageBrowser.java │ ├── Editor.java │ ├── EditorCSS.java │ ├── EditorHTML.java │ ├── EditorImage.java │ ├── EditorTableWriter.java │ ├── JAXRS2Application.java │ ├── Load.java │ ├── LongTailHelper.java │ ├── PPrNone.java │ ├── SessionAwareAbstractTableWriter.java │ ├── SessionImageHandler.java │ ├── SessionPPrHandler.java │ ├── SessionStyleHandler.java │ └── toDocx │ ├── RoundtripXHTMLImporter.java │ ├── Saver.java │ ├── SessionAwareDocx4jUserAgent.java │ └── XHTMLtoDocxImageHandler.java └── webapp ├── WEB-INF └── web.xml ├── browser ├── browser.css ├── cmis.png ├── document.png ├── folder.png ├── policy.png ├── relationship.png ├── type.png └── unknown.png ├── index.html └── placeholders ├── bookmark_end.png ├── bookmark_start.png ├── chart.png ├── comment_range_end.png ├── comment_range_start.png ├── comment_ref.png ├── endnote.png ├── equation.png ├── footnote.png ├── page_break.png ├── sectPr.png ├── signature.png └── tab.png /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | docx-html-editor 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | 663 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | docx-html-editor 2 | ================ 3 | 4 | A proof of concept (JAX-RS 2.0) to show how a web-based HTML rich text editors (eg CkEditor) can be 5 | used to edit docx files (via XHTML round trip). 6 | 7 | We've published this as a "cheap and cheerful" option only; for a commercial grade solution (new in 2018), please see 8 | https://nativedocuments.com/ (which takes a completely different approach). 9 | 10 | That said, it should work OK for documents containing text, tables, and images. 11 | Exotic Word features are not supported. 12 | 13 | Interested docx4j community members are welcome to improve it and offer pull requests. 14 | 15 | docx-html-editor is licensed under the AGPLv3. This means you have to publish any changes you make. 16 | 17 | How it works 18 | ============ 19 | 20 | The idea is: 21 | 22 | - use docx4j to convert the docx to XHTML 23 | - use CKEditor to edit that XHTML in the web browser 24 | - on submit, convert the XHTML back to docx content 25 | 26 | The general problem with converting to/from XHTML is the “impendance mismatch”. That is, losing stuff during round trip. This will be a familiar problem to anyone who has ever edited a docx in Google Docs or LibreOffice. 27 | 28 | This demo addresses that problem by identifying docx content which CKEditor would mangle, and then on submit/save, using the original docx content for those bits. 29 | 30 | In this demo, the problematic content is replaced with visual placeholders, so you can see it is there. 31 | 32 | Getting started 33 | =============== 34 | 35 | Pre-reqs: Since we don't distribute a binary, you'll need maven to build from source. 36 | 37 | 38 | Get CKeditor (v3.6.6.1); unzip it to src/main/webapp/ckeditor 39 | (so you have src/main/webapp/ckeditor/ckeditor.js etc) 40 | 41 | Build the war: 42 | 43 | mvn install 44 | 45 | Load the resulting war file from dir target to your app server (eg Tomcat) 46 | 47 | Visit the upload page in your web browser 48 | 49 | /docx-html-editor/ 50 | 51 | There's a bit more doco at https://github.com/plutext/docx-html-editor/blob/master/documentation/user/docx-html-editor_UserManual.docx?raw=true 52 | 53 | Developing 54 | ========== 55 | 56 | Set this up as a Maven project (eg in Eclipse) to get the dependencies. 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /documentation/user/docx-html-editor_UserManual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/documentation/user/docx-html-editor_UserManual.docx -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | com.plutext 7 | docx-html-editor 8 | 0.0.1-SNAPSHOT 9 | 10 | war 11 | 12 | docx-html-editor 13 | 14 | demo of editing a docx in a web browser using CKEditor via XHTML round trip 15 | 16 | http://www.docx4java.org/ 17 | 18 | 2013 19 | 20 | 21 | jharrop 22 | Jason Harrop 23 | jason@plutext.org 24 | Plutext 25 | 26 | Developer 27 | 28 | +10 29 | 30 | 31 | 32 | src/main/java 33 | src/test/java 34 | bin 35 | bin 36 | 37 | 38 | org.apache.tomcat.maven 39 | tomcat7-maven-plugin 40 | 2.0-beta-1 41 | 42 | tomcat-localhost 43 | http://localhost/manager/text 44 | 45 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-war-plugin 55 | 2.2 56 | 57 | true 58 | src/main/webapp/WEB-INF/web.xml 59 | 60 | 61 | 62 | 63 | maven-compiler-plugin 64 | 65 | 1.6 66 | 1.6 67 | false 68 | 69 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-dependency-plugin 78 | 2.0 79 | 80 | 81 | 82 | 83 | 84 | src/main/java 85 | 86 | **/*.xslt 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | org.docx4j 96 | docx4j 97 | 3.2.2 98 | 99 | 100 | 101 | org.docx4j 102 | docx4j-ImportXHTML 103 | 3.0.0-SNAPSHOT 104 | 105 | 106 | 107 | org.slf4j 108 | slf4j-simple 109 | 1.6.1 110 | 111 | 112 | 113 | org.apache.chemistry.opencmis 114 | chemistry-opencmis-client-impl 115 | 0.9.0 116 | 117 | 118 | 119 | javax.servlet 120 | servlet-api 121 | 2.5 122 | provided 123 | 124 | 125 | 126 | org.glassfish.jersey.core 127 | jersey-server 128 | 2.1 129 | 130 | 131 | 132 | 133 | org.glassfish.jersey.containers 134 | jersey-container-servlet-core 135 | 2.1 136 | 137 | 138 | 139 | org.glassfish.jersey.media 140 | jersey-media-multipart 141 | 2.1 142 | 143 | 144 | 145 | org.capaxit.textimage 146 | TextImageGen 147 | 2.0-SNAPSHOT 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /src/main/java/docx2xhtml_Bare.xslt: -------------------------------------------------------------------------------- 1 |  19 | 20 | 21 | 22 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 64 | 66 | 67 | /services/css/docx.css 68 | 69 | 70 | /services/images/ 71 | /services/image/ 72 | 73 | /services/save 74 | 75 | 76 | 77 | 78 | 79 | 80 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" ></meta> 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 129 | 130 | 131 | 132 | 133 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 164 | 165 | 166 | 167 | 170 | 171 | 172 | 173 | 175 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 187 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 263 | 264 | 265 | 266 | 267 | 268 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | ­ 426 | 427 | 428 | 429 | &#8209; 430 | 431 | 432 | 438 | 439 | 440 |
441 |
442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 501 | 502 | 503 | 504 | 505 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 516 | 517 | 518 |
519 | -------------------------------------------------------------------------------- /src/main/java/docx2xhtml_CKEditor2013.xslt: -------------------------------------------------------------------------------- 1 |  19 | 20 | 21 | 22 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 64 | 66 | 67 | /services/css/docx.css 68 | 69 | 70 | /services/images/ 71 | /services/image/ 72 | 73 | /services/save 74 | 75 | 76 | 77 | 78 | 79 | 80 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" ></meta> 81 | 82 | 83 | 84 | 85 | 86 | 87 | 90 | 91 | 201 | 202 | 203 | 204 | 205 | 206 |
211 | 212 |
213 | 214 |
215 | 216 | 217 |
218 | 219 | 220 |
221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 254 | 255 | 256 | 257 | 258 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 289 | 290 | 291 | 292 | 295 | 296 | 297 | 298 | 300 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 312 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 388 | 389 | 390 | 391 | 392 | 393 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | ­ 551 | 552 | 553 | 554 | &#8209; 555 | 556 | 557 | 563 | 564 | 565 |
566 |
567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 626 | 627 | 628 | 629 | 630 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 641 | 642 | 643 |
644 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/CK3ImageBrowser.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | package org.plutext.htmleditor; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map.Entry; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletContext; 24 | import javax.servlet.http.HttpServletRequest; 25 | import javax.servlet.http.HttpServletResponse; 26 | import javax.servlet.http.HttpSession; 27 | import javax.ws.rs.GET; 28 | import javax.ws.rs.Path; 29 | import javax.ws.rs.PathParam; 30 | import javax.ws.rs.QueryParam; 31 | import javax.ws.rs.WebApplicationException; 32 | import javax.ws.rs.core.Context; 33 | import javax.ws.rs.core.Response; 34 | import javax.ws.rs.core.Response.Status; 35 | import javax.ws.rs.core.UriInfo; 36 | 37 | import org.docx4j.openpackaging.exceptions.Docx4JException; 38 | 39 | /** 40 | * CKEditor user can browse the images in their 41 | * session (either already present in the docx), 42 | * or uploaded, and select one for insertion 43 | * in the docx. 44 | * 45 | * @author jharrop 46 | * 47 | */ 48 | @Path("/images/") 49 | public class CK3ImageBrowser { 50 | 51 | private static final Logger jul = Logger.getLogger(CK3ImageBrowser.class.getName()); 52 | 53 | @GET 54 | @Path("{url : .*}") 55 | public Response browse( 56 | @Context HttpServletRequest request, 57 | @Context HttpServletResponse response, 58 | @Context ServletContext context, 59 | @Context UriInfo info, 60 | @PathParam("url") String url, 61 | @QueryParam("CKEditorFuncNum") String CKEditorFuncNum 62 | ) { 63 | 64 | try { 65 | 66 | HttpSession session = request.getSession(false); 67 | 68 | // jul.info("request.getRequestURI()"+request.getRequestURI()); 69 | // jul.info("info.getRequestUri().toASCIIString()"+info.getRequestUri().toASCIIString()); // pretty close 70 | // jul.info("request.getQueryString()"+request.getQueryString()); 71 | // jul.info("info.getPath(false)"+info.getPath(false)); 72 | // jul.info("info.getPath(true)"+info.getPath(true)); 73 | 74 | // String browseUrl = url + "?" + request.getQueryString(); 75 | // jul.info(browseUrl); 76 | 77 | if (url.equals("")) { 78 | // display all the images 79 | 80 | StringBuilder sb = new StringBuilder(); 81 | sb.append(""); 82 | HashMap imageMap = SessionImageHandler.getImageMap(session); 83 | if (imageMap.size()==0) { 84 | sb.append("No images found. Click the 'upload' tab?"); 85 | } else { 86 | for (Entry entry : imageMap.entrySet()) { 87 | String suffix = "?CKEditorFuncNum="+CKEditorFuncNum; 88 | String imgUrl = getUrl(response, entry.getKey() + suffix); 89 | sb.append("

" + entry.getKey() + "

"); 90 | } 91 | } 92 | sb.append(""); 93 | return Response.ok(sb.toString() ).build(); 94 | 95 | } else { 96 | // its a request for a specific image 97 | String imageUrl = getUrl(response, url); 98 | 99 | String html = ""; 102 | 103 | 104 | return Response.ok(html).build(); 105 | 106 | } 107 | 108 | 109 | } catch (Exception e) { 110 | e.printStackTrace(); 111 | // System.out.println(e.getMessage()); 112 | throw new WebApplicationException( 113 | new Docx4JException(e.getMessage(), e), 114 | Status.INTERNAL_SERVER_ERROR); 115 | } 116 | 117 | } 118 | 119 | protected String getUrl(HttpServletResponse response, String name) { 120 | return response.encodeURL(Editor.getContextPath() + "/services/image/" + name); 121 | } 122 | 123 | 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/Editor.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | import java.io.PrintWriter; 25 | import java.io.StringWriter; 26 | import java.io.Writer; 27 | import java.net.HttpURLConnection; 28 | import java.net.URL; 29 | import java.util.HashMap; 30 | import java.util.logging.Level; 31 | import java.util.logging.Logger; 32 | 33 | import javax.servlet.ServletContext; 34 | import javax.servlet.ServletException; 35 | import javax.servlet.http.HttpServletRequest; 36 | import javax.servlet.http.HttpServletResponse; 37 | import javax.servlet.http.HttpSession; 38 | import javax.ws.rs.GET; 39 | import javax.ws.rs.Path; 40 | import javax.ws.rs.PathParam; 41 | import javax.ws.rs.WebApplicationException; 42 | import javax.ws.rs.core.Context; 43 | import javax.ws.rs.core.Response; 44 | import javax.ws.rs.core.Response.ResponseBuilder; 45 | import javax.ws.rs.core.Response.Status; 46 | import javax.ws.rs.core.StreamingOutput; 47 | import javax.ws.rs.core.UriInfo; 48 | import javax.xml.transform.Source; 49 | import javax.xml.transform.Templates; 50 | import javax.xml.transform.TransformerConfigurationException; 51 | import javax.xml.transform.stream.StreamSource; 52 | 53 | import org.docx4j.Docx4J; 54 | import org.docx4j.XmlUtils; 55 | import org.docx4j.convert.out.ConversionFeatures; 56 | import org.docx4j.convert.out.HTMLSettings; 57 | import org.docx4j.convert.out.common.AbstractWriterRegistry; 58 | import org.docx4j.convert.out.common.Exporter; 59 | import org.docx4j.convert.out.html.BookmarkStartWriter; 60 | import org.docx4j.convert.out.html.BrWriter; 61 | import org.docx4j.convert.out.html.FldSimpleWriter; 62 | import org.docx4j.convert.out.html.HTMLExporterXslt; 63 | import org.docx4j.convert.out.html.HyperlinkWriter; 64 | import org.docx4j.convert.out.html.SymbolWriter; 65 | import org.docx4j.openpackaging.exceptions.Docx4JException; 66 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 67 | 68 | 69 | 70 | 71 | 72 | @Path("/edit/") 73 | public class Editor { 74 | 75 | private static final Logger jul = Logger.getLogger(Editor.class 76 | .getName()); 77 | 78 | protected static final String APP_KEY = "DOCX4J_EDITOR:"; 79 | 80 | // TODO rethink this. The issue is that context is null 81 | // until service method starts 82 | // (injection happens when you enter service method) 83 | // so just get it locally in the service methods 84 | private static String APP_CONTEXT = null; 85 | protected static String getContextPath() { 86 | // eg /docx4j-web-editor-1.0.0-SNAPSHOT 87 | return APP_CONTEXT; 88 | } 89 | protected static void setContextPath(String contextPath) { 90 | APP_CONTEXT = contextPath; 91 | } 92 | 93 | 94 | // Templates are thread safe 95 | private static Templates CKEditor3_XSLT; 96 | private static Templates BARE_XSLT; // BARE is still intended for CKEditor3 97 | 98 | static { 99 | jul.info("Servlet loaded"); 100 | 101 | Logger.getLogger("com.sun.jersey").setLevel(Level.WARNING); 102 | Logger.getLogger("org.glassfish.jersey").setLevel(Level.WARNING); 103 | 104 | try { 105 | CKEditor3_XSLT = XmlUtils.getTransformerTemplate( 106 | new StreamSource( 107 | org.docx4j.utils.ResourceUtils.getResource("docx2xhtml_CKEditor2013.xslt"))); 108 | } catch (Exception e) { 109 | e.printStackTrace(); 110 | } 111 | try { 112 | BARE_XSLT = XmlUtils.getTransformerTemplate( 113 | new StreamSource( 114 | org.docx4j.utils.ResourceUtils.getResource("docx2xhtml_Bare.xslt"))); 115 | } catch (Exception e) { 116 | e.printStackTrace(); 117 | } 118 | 119 | } 120 | 121 | 122 | 123 | 124 | @GET 125 | @Path("{url : .+}") 126 | //@Consumes(MediaType.APPLICATION_XML) 127 | //@Produces( {MediaType.APPLICATION_XHTML_XML}) 128 | public Response getConfigForm( 129 | @Context HttpServletRequest request, 130 | @Context HttpServletResponse response, 131 | @Context ServletContext context, 132 | @Context UriInfo info, 133 | @PathParam("url") String url 134 | ) { 135 | 136 | try { 137 | 138 | HttpSession session = request.getSession(true); 139 | 140 | // Need repo info in a session object? 141 | 142 | // Use CMIS to get the docx from that URL 143 | // eg http://localhost:8080/alfresco/cmisatom/706ca2b8-d196-4834-a2bf-e99122b88994/content/cmis.docx 144 | 145 | String browseUrl = url + "?" + request.getQueryString(); 146 | jul.info(browseUrl); 147 | 148 | InputStream is = getCMISResource(request, browseUrl); 149 | 150 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is); 151 | 152 | // Store the docx, so we can later inject XHTML into it... 153 | session.setAttribute("docx", wordMLPackage); 154 | 155 | 156 | // return Response.ok( 157 | // "It worked" + url, 158 | // MediaType.TEXT_PLAIN).build(); 159 | 160 | ResponseBuilder builder = streamDocxAsHtml(wordMLPackage, session, EditorHTML.CKEditor3, response ); 161 | return builder.build(); 162 | 163 | } catch (Exception e) { 164 | e.printStackTrace(); 165 | System.out.println(e.getMessage()); 166 | throw new WebApplicationException( 167 | new Docx4JException(e.getMessage(), e), 168 | Status.INTERNAL_SERVER_ERROR); 169 | } 170 | 171 | } 172 | 173 | 174 | protected static final AbstractWriterRegistry CUSTOM_HTML_WRITER_REGISTRY = 175 | new AbstractWriterRegistry() { 176 | @Override 177 | protected void registerDefaultWriterInstances() { 178 | registerWriter(new EditorTableWriter()); // customised 179 | registerWriter(new SymbolWriter()); 180 | registerWriter(new BrWriter()); 181 | registerWriter(new FldSimpleWriter()); 182 | registerWriter(new BookmarkStartWriter()); 183 | registerWriter(new HyperlinkWriter()); 184 | } 185 | }; 186 | 187 | 188 | 189 | protected ResponseBuilder streamDocxAsHtml(final WordprocessingMLPackage wordMLPackage, final HttpSession session, 190 | EditorHTML editorHTML, HttpServletResponse response) { 191 | 192 | 193 | // .. the HtmlSettings object 194 | final HTMLSettings htmlSettings = new HTMLSettings(); 195 | 196 | if (editorHTML.equals(EditorHTML.BARE)) { 197 | htmlSettings.setCustomXsltTemplates(BARE_XSLT); 198 | } else if (editorHTML.equals(EditorHTML.CKEditor3)) { 199 | htmlSettings.setCustomXsltTemplates(CKEditor3_XSLT); 200 | } 201 | 202 | 203 | htmlSettings.setImageHandler(new SessionImageHandler(session)); 204 | 205 | htmlSettings.setStyleElementHandler(new SessionStyleHandler(session)); 206 | 207 | // htmlSettings.setUserBodyTop("

TOP!

"); 208 | // htmlSettings.setUserBodyTail("

TAIL!

"); 209 | 210 | // Sample sdt tag handler (tag handlers insert specific 211 | // html depending on the contents of an sdt's tag). 212 | // This will only have an effect if the sdt tag contains 213 | // the string @class=XXX 214 | // SdtWriter.registerTagHandler("@class", new TagClass() ); 215 | 216 | // SdtWriter.registerTagHandler(Containerization.TAG_BORDERS, new TagSingleBox() ); 217 | // SdtWriter.registerTagHandler(Containerization.TAG_SHADING, new TagSingleBox() ); 218 | 219 | 220 | htmlSettings.setWmlPackage(wordMLPackage); 221 | wordMLPackage.setUserData("HttpSession", session); 222 | 223 | // Since we'll store various stuff on the pkg object (eg images, long tail) 224 | // avoid using a clone! 225 | htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_DEEP_COPY); 226 | 227 | // Don't convert w:r/w:br[w:@type="page"] to 228 | // since it seems that should really go on the following paragraph 229 | htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_MOVE_PAGEBREAK); 230 | 231 | // We'll store the images associated with this docx in a map: 232 | HashMap imageMap = new HashMap(); 233 | // and store that in the docx 234 | // (so each docx has its own collection of available images; these aren't shared across 235 | // the user's session, if they have multiple windows open, each with a different editor) 236 | wordMLPackage.setUserData(APP_KEY + "imageMap", imageMap); 237 | 238 | htmlSettings.getSettings().put("ContextPath", getContextPath()); 239 | 240 | // For extension function 241 | htmlSettings.getSettings().put("HttpServletResponse", response); 242 | 243 | 244 | ResponseBuilder builder = Response.ok( 245 | 246 | new StreamingOutput() { 247 | public void write(OutputStream output) throws IOException, WebApplicationException { 248 | try { 249 | javax.xml.transform.stream.StreamResult result 250 | = new javax.xml.transform.stream.StreamResult(output); 251 | 252 | // Docx4J.toHTML(htmlSettings, output, Docx4J.FLAG_NONE); 253 | Exporter exporter = new HTMLExporterXslt(CUSTOM_HTML_WRITER_REGISTRY); 254 | exporter.export(htmlSettings, output); 255 | 256 | } catch (Exception e) { 257 | throw new WebApplicationException(e); 258 | } 259 | } 260 | } 261 | ); 262 | // builder.header("Content-Disposition", "attachment; filename=output.pdf"); 263 | // builder.type("application/pdf"); 264 | 265 | return builder; 266 | } 267 | 268 | /** 269 | * XSLT extension function which encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. 270 | * The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. 271 | * For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary. 272 | * 273 | * For robust session tracking, all URLs emitted by a servlet should be run through this method. 274 | * Otherwise, URL rewriting cannot be used with browsers which do not support cookies. 275 | * 276 | * @param url 277 | * @return 278 | */ 279 | public static String encodeURL(HttpServletResponse response, String url) { 280 | 281 | return response.encodeURL(url); 282 | } 283 | 284 | protected InputStream getCMISResource(HttpServletRequest req, String browseUrl) throws ServletException, IOException { 285 | 286 | // get content 287 | URL url = new URL(browseUrl); 288 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 289 | conn.setDoInput(true); 290 | conn.setDoOutput(false); 291 | conn.setRequestMethod("GET"); 292 | String authHeader = req.getHeader("Authorization"); 293 | if (authHeader != null) { 294 | conn.setRequestProperty("Authorization", authHeader); 295 | } 296 | conn.connect(); 297 | 298 | // // ask for login 299 | // if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { 300 | // resp.setHeader("WWW-Authenticate", conn.getHeaderField("WWW-Authenticate")); 301 | // resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization Required"); 302 | // return; 303 | // } 304 | // 305 | // // debug messages 306 | // if (log.isDebugEnabled()) { 307 | // log.debug("'" + browseUrl + "' -> '" + conn.getContentType() + "'"); 308 | // } 309 | 310 | return conn.getInputStream(); 311 | } 312 | 313 | 314 | 315 | public static String getStackTrace(Throwable aThrowable) { 316 | final Writer result = new StringWriter(); 317 | final PrintWriter printWriter = new PrintWriter(result); 318 | aThrowable.printStackTrace(printWriter); 319 | return result.toString(); 320 | } 321 | 322 | 323 | 324 | /** 325 | * For testing purposes only 326 | */ 327 | public static void main(String[] args) throws Exception { 328 | 329 | // .. the HtmlSettings object 330 | final HTMLSettings htmlSettings = new HTMLSettings(); 331 | htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_DEEP_COPY); 332 | 333 | 334 | try { 335 | Source xsltSource = new StreamSource(org.docx4j.utils.ResourceUtils.getResource( 336 | "org/plutext/htmleditor/docx2xhtml_CKEditor2013.xslt")); 337 | htmlSettings.setCustomXsltTemplates(XmlUtils.getTransformerTemplate(xsltSource)); 338 | 339 | } catch (IOException e) { 340 | e.printStackTrace(); 341 | } catch (TransformerConfigurationException e) { 342 | e.printStackTrace(); 343 | } 344 | 345 | 346 | 347 | // htmlSettings.setImageHandler(new SessionImageHandler(session)); 348 | // 349 | // htmlSettings.setStyleElementHandler(new SessionStyleHandler(session)); 350 | 351 | htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_DEEP_COPY); 352 | htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_MOVE_PAGEBREAK); 353 | 354 | 355 | String inputfilepath = System.getProperty("user.dir") 356 | // + "/sample-docx.docx"; 357 | // + "/sample-docs/word/2003/word2003xml.xml"; 358 | + "/docx-samples/Demo-Hayden-Management-v2.docx"; 359 | 360 | 361 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage 362 | .load(new java.io.File(inputfilepath)); 363 | 364 | htmlSettings.setWmlPackage(wordMLPackage); 365 | 366 | ByteArrayOutputStream output = new ByteArrayOutputStream(); 367 | 368 | Docx4J.toHTML(htmlSettings, output, Docx4J.FLAG_NONE); 369 | 370 | System.out.println(output.toString("UTF-8")); 371 | 372 | } 373 | 374 | } -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/EditorCSS.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | //import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.http.HttpSession; 24 | import javax.ws.rs.GET; 25 | import javax.ws.rs.Path; 26 | import javax.ws.rs.core.Context; 27 | import javax.ws.rs.core.Response; 28 | 29 | 30 | @Path("/css/") 31 | public class EditorCSS { 32 | 33 | // private static final Logger jul = Logger.getLogger(EditorCSS.class 34 | // .getName()); 35 | 36 | @GET 37 | @Path("/docx.css") 38 | public Response getCSS(@Context HttpServletRequest request) { 39 | 40 | // SessionStyleHandler has put the CSS into the session 41 | 42 | HttpSession session = request.getSession(); 43 | 44 | return Response.ok( 45 | session.getAttribute("css"), 46 | "text/css").build(); 47 | 48 | } 49 | 50 | // org.docx4j.convert.out.html.XsltHTMLFunctions.appendStyleElement($conversionContext) 51 | } -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/EditorHTML.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | public enum EditorHTML { 21 | 22 | BARE, CKEditor3; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/EditorImage.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.util.logging.Logger; 23 | 24 | import javax.servlet.ServletContext; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | import javax.servlet.http.HttpSession; 28 | import javax.ws.rs.Consumes; 29 | import javax.ws.rs.GET; 30 | import javax.ws.rs.POST; 31 | import javax.ws.rs.Path; 32 | import javax.ws.rs.PathParam; 33 | import javax.ws.rs.Produces; 34 | import javax.ws.rs.QueryParam; 35 | import javax.ws.rs.core.Context; 36 | import javax.ws.rs.core.MediaType; 37 | import javax.ws.rs.core.Response; 38 | import javax.ws.rs.core.UriInfo; 39 | 40 | import org.apache.commons.io.IOUtils; 41 | import org.docx4j.openpackaging.exceptions.Docx4JException; 42 | import org.glassfish.jersey.media.multipart.FormDataContentDisposition; 43 | import org.glassfish.jersey.media.multipart.FormDataParam; 44 | 45 | 46 | /** 47 | * @author jharrop 48 | * 49 | */ 50 | @Path("/image/") 51 | public class EditorImage { 52 | 53 | private static final Logger jul = Logger.getLogger(EditorImage.class 54 | .getName()); 55 | 56 | /** 57 | * The editor (eg CKEditor) invokes this to display an image. 58 | * 59 | * @param request 60 | * @param context 61 | * @param info 62 | * @param name 63 | * @return 64 | */ 65 | @GET 66 | @Path("{name}") 67 | public Response getImage( 68 | @Context HttpServletRequest request, 69 | @Context ServletContext context, 70 | @Context UriInfo info, 71 | @PathParam("name") String name 72 | ) { 73 | 74 | HttpSession session = request.getSession(); 75 | 76 | // jul.info("request.getRequestURI()"+request.getRequestURI()); 77 | // jul.info("info.getRequestUri().toASCIIString()"+info.getRequestUri().toASCIIString()); // pretty close 78 | // jul.info("request.getQueryString()"+request.getQueryString()); 79 | // jul.info("info.getPath(false)"+info.getPath(false)); 80 | // jul.info("info.getPath(true)"+info.getPath(true)); 81 | 82 | 83 | return Response.ok( 84 | SessionImageHandler.getImageMap(session).get(name), 85 | MediaType.APPLICATION_OCTET_STREAM).build(); 86 | 87 | } 88 | 89 | /** 90 | * CK3 specific image upload 91 | * 92 | * @param request 93 | * @param CKEditorFuncNum 94 | * @param imageInputStream 95 | * @param imageFileDetail 96 | * @return 97 | * @throws Docx4JException 98 | * @throws IOException 99 | */ 100 | @POST 101 | @Consumes("multipart/form-data") 102 | @Produces("text/html") 103 | public Response ck3ImageUpload( 104 | // @Context ServletContext servletContext, 105 | @Context HttpServletRequest request, 106 | @Context HttpServletResponse response, 107 | @QueryParam("CKEditorFuncNum") String CKEditorFuncNum, 108 | @FormDataParam("upload") InputStream imageInputStream, 109 | @FormDataParam("upload") FormDataContentDisposition imageFileDetail 110 | ) throws Docx4JException, IOException { 111 | 112 | // 113 | 114 | try { 115 | 116 | HttpSession session = request.getSession(false); 117 | 118 | String name = "uploaded_" + this.getFileName(imageFileDetail.getFileName()); 119 | 120 | SessionImageHandler.getImageMap(session).put(name, IOUtils.toByteArray(imageInputStream) ); 121 | 122 | jul.info("stored " + name); 123 | 124 | String url = response.encodeURL(Editor.getContextPath() + "/services/image/" + name); 125 | 126 | String html = ""; 130 | 131 | 132 | return Response.ok(html).build(); 133 | 134 | } catch (Exception e) { 135 | e.printStackTrace(); 136 | System.out.println(e.getMessage()); 137 | 138 | String html = ""; 142 | 143 | 144 | return Response.ok(html).build(); // TODO not ok 145 | 146 | // throw new WebApplicationException( 147 | // new Docx4JException(e.getMessage(), e), 148 | // Status.INTERNAL_SERVER_ERROR); 149 | } 150 | 151 | } 152 | 153 | /** 154 | * header sample 155 | * { 156 | * Content-Type=[image/png], 157 | * Content-Disposition=[form-data; name="file"; filename="filename.extension"] 158 | * } 159 | **/ 160 | private String getFileName(String name) { 161 | 162 | String finalFileName = name.trim().replaceAll("\"", ""); 163 | 164 | if (finalFileName.lastIndexOf(".")==-1) { 165 | return finalFileName; 166 | } else { 167 | return finalFileName.substring(0, finalFileName.lastIndexOf(".") ); 168 | } 169 | } 170 | 171 | 172 | 173 | } -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/EditorTableWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | Licensed to Plutext Pty Ltd under one or more contributor license agreements. 20 | 21 | * This file is part of docx4j. 22 | 23 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); 24 | you may not use this file except in compliance with the License. 25 | 26 | You may obtain a copy of the License at 27 | 28 | http://www.apache.org/licenses/LICENSE-2.0 29 | 30 | Unless required by applicable law or agreed to in writing, software 31 | distributed under the License is distributed on an "AS IS" BASIS, 32 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 | See the License for the specific language governing permissions and 34 | limitations under the License. 35 | 36 | */ 37 | package org.plutext.htmleditor; 38 | 39 | import java.util.List; 40 | 41 | import org.docx4j.UnitsOfMeasurement; 42 | import org.docx4j.convert.out.common.AbstractWmlConversionContext; 43 | import org.docx4j.convert.out.common.writer.AbstractTableWriterModel; 44 | import org.docx4j.convert.out.common.writer.AbstractTableWriterModelCell; 45 | import org.docx4j.convert.out.html.HtmlCssHelper; 46 | import org.docx4j.model.properties.Property; 47 | import org.docx4j.model.styles.StyleTree; 48 | import org.docx4j.model.styles.StyleTree.AugmentedStyle; 49 | import org.docx4j.model.styles.Tree; 50 | import org.slf4j.Logger; 51 | import org.slf4j.LoggerFactory; 52 | import org.w3c.dom.Document; 53 | import org.w3c.dom.Element; 54 | 55 | /* 56 | * Write a w:tbl as an HTML . 57 | * @author Alberto Zerolo, Adam Schmideg, Jason Harrop 58 | * 59 | */ 60 | public class EditorTableWriter extends SessionAwareAbstractTableWriter { 61 | protected final static Logger logger = LoggerFactory.getLogger(EditorTableWriter.class); 62 | 63 | protected final static String TABLE_BORDER_MODEL = "border-collapse"; 64 | protected final static String TABLE_INDENT = "margin-left"; 65 | 66 | @Override 67 | protected Logger getLog() { 68 | return logger; 69 | } 70 | 71 | @Override 72 | protected Element createNode(Document doc, int nodeType) { 73 | Element ret = null; 74 | switch (nodeType) { 75 | case NODE_TABLE: 76 | ret = doc.createElement("table"); 77 | break; 78 | case NODE_TABLE_COLUMN_GROUP: 79 | ret = doc.createElement("colgroup"); 80 | break; 81 | case NODE_TABLE_COLUMN: 82 | ret = doc.createElement("col"); 83 | break; 84 | case NODE_TABLE_HEADER: 85 | ret = doc.createElement("thead"); 86 | break; 87 | case NODE_TABLE_HEADER_ROW: 88 | ret = doc.createElement("tr"); 89 | break; 90 | case NODE_TABLE_HEADER_CELL: 91 | ret = doc.createElement("th"); 92 | break; 93 | case NODE_TABLE_BODY: 94 | ret = doc.createElement("tbody"); 95 | break; 96 | case NODE_TABLE_BODY_ROW: 97 | ret = doc.createElement("tr"); 98 | break; 99 | case NODE_TABLE_BODY_CELL: 100 | ret = doc.createElement("td"); 101 | break; 102 | } 103 | return ret; 104 | } 105 | 106 | @Override 107 | protected void applyAttributes(AbstractWmlConversionContext context, List properties, Element element) { 108 | HtmlCssHelper.applyAttributes(properties, element); 109 | } 110 | 111 | @Override 112 | protected void applyTableCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element tableRoot) { 113 | int cellSpacing = ((table.getEffectiveTableStyle().getTblPr() != null) && 114 | (table.getEffectiveTableStyle().getTblPr().getTblCellSpacing() != null) && 115 | (table.getEffectiveTableStyle().getTblPr().getTblCellSpacing().getW() != null) ? 116 | table.getEffectiveTableStyle().getTblPr().getTblCellSpacing().getW().intValue() : 0); 117 | 118 | // Set @class 119 | if (table.getStyleId()==null) { 120 | getLog().debug("table has no w:tblStyle?"); 121 | } else { 122 | StyleTree styleTree = context.getWmlPackage().getMainDocumentPart().getStyleTree(); 123 | Tree tTree = styleTree.getTableStylesTree(); 124 | org.docx4j.model.styles.Node asn = tTree.get(table.getStyleId()); 125 | tableRoot.setAttribute("class", 126 | StyleTree.getHtmlClassAttributeValue(tTree, asn) 127 | ); 128 | } 129 | 130 | // border model 131 | // Handle cellSpacing as in xsl-fo to have a consistent look. 132 | if (cellSpacing > 0) { 133 | HtmlCssHelper.appendStyle(tableRoot, Property.composeCss(TABLE_BORDER_MODEL, "separate")); 134 | tableRoot.setAttribute("cellspacing", 135 | //WW seems only to store cellSpacing/2 but displays and applies cellSpacing * 2 136 | convertToPixels(cellSpacing * 2)); 137 | } 138 | else { 139 | HtmlCssHelper.appendStyle(tableRoot, Property.composeCss(TABLE_BORDER_MODEL, "collapse")); 140 | } 141 | 142 | // table width 143 | if (table.getTableWidth() > 0) { 144 | HtmlCssHelper.appendStyle(tableRoot, 145 | Property.composeCss("width", UnitsOfMeasurement.twipToBest(table.getTableWidth()))); 146 | } 147 | } 148 | 149 | /** 150 | * Conversion of twips to pixels at 96dpi 151 | * @param twips 152 | * @return pixel-attribute value 153 | */ 154 | protected String convertToPixels(int twips) { 155 | float pixels = (96f * twips / 1440f); 156 | if ((twips > 0) && (pixels < 1)) { 157 | pixels = 1; 158 | } 159 | return UnitsOfMeasurement.format2DP.format(pixels) + "px"; 160 | } 161 | 162 | @Override 163 | protected void applyColumnCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element column, int columnIndex, int columnWidth) { 164 | if ((table.getTableWidth() > 0) && (columnWidth > -1)) { 165 | HtmlCssHelper.appendStyle(column, Property.composeCss("width", 166 | UnitsOfMeasurement.format2DP.format((100f * columnWidth)/table.getTableWidth()) + "%")); 167 | } 168 | } 169 | 170 | @Override 171 | protected void applyTableCellCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, AbstractTableWriterModelCell tableCell, Element cellNode, boolean isHeader, boolean isDummyCell) { 172 | if (isDummyCell) { 173 | HtmlCssHelper.appendStyle(cellNode, Property.composeCss("border", "none")); 174 | HtmlCssHelper.appendStyle(cellNode, Property.composeCss("background-color:", "transparent")); 175 | } 176 | 177 | if (tableCell.getExtraCols() > 0) { 178 | cellNode.setAttribute("colspan", Integer.toString(tableCell.getExtraCols() + 1)); 179 | 180 | } 181 | if (tableCell.getExtraRows() > 0) { 182 | cellNode.setAttribute("rowspan", Integer.toString(tableCell.getExtraRows() + 1)); 183 | } 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/JAXRS2Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.util.HashSet; 21 | import java.util.Set; 22 | 23 | import javax.ws.rs.core.Application; 24 | 25 | import org.glassfish.jersey.media.multipart.MultiPartFeature; 26 | import org.plutext.htmleditor.toDocx.Saver; 27 | 28 | /** 29 | * Portable JAX-RS application. 30 | * 31 | * @author Arul Dhesiaseelan (aruld@acm.org) 32 | */ 33 | public class JAXRS2Application extends Application { 34 | @Override 35 | public Set> getClasses() { 36 | final Set> classes = new HashSet>(); 37 | // register resources and features 38 | classes.add(MultiPartFeature.class); 39 | classes.add(Load.class); 40 | 41 | // classes.add(UploadICM.class); 42 | 43 | classes.add(Editor.class); 44 | classes.add(EditorCSS.class); 45 | classes.add(EditorImage.class); 46 | classes.add(CK3ImageBrowser.class); 47 | 48 | classes.add(Saver.class); 49 | // classes.add(LoggingFilter.class); 50 | return classes; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/Load.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import java.util.logging.Logger; 25 | 26 | import javax.servlet.ServletContext; 27 | import javax.servlet.http.HttpServletRequest; 28 | import javax.servlet.http.HttpServletResponse; 29 | import javax.servlet.http.HttpSession; 30 | import javax.ws.rs.Consumes; 31 | import javax.ws.rs.POST; 32 | import javax.ws.rs.Path; 33 | import javax.ws.rs.Produces; 34 | import javax.ws.rs.WebApplicationException; 35 | import javax.ws.rs.core.Context; 36 | import javax.ws.rs.core.Response; 37 | import javax.ws.rs.core.Response.ResponseBuilder; 38 | import javax.ws.rs.core.Response.Status; 39 | 40 | import org.docx4j.openpackaging.exceptions.Docx4JException; 41 | import org.docx4j.openpackaging.io.LoadFromZipNG; 42 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 43 | import org.glassfish.jersey.media.multipart.FormDataContentDisposition; 44 | import org.glassfish.jersey.media.multipart.FormDataParam; 45 | 46 | 47 | 48 | @Path("/load") 49 | public class Load { 50 | 51 | private static final Logger jul = Logger.getLogger(Load.class 52 | .getName()); 53 | 54 | 55 | // static { 56 | // jul.info("Servlet loaded"); 57 | // 58 | // Logger.getLogger("com.sun.jersey").setLevel(Level.FINEST); 59 | // Logger.getLogger("org.glassfish.jersey").setLevel(Level.FINEST); 60 | // } 61 | 62 | 63 | @POST 64 | @Consumes("multipart/form-data") 65 | @Produces( {"application/vnd.openxmlformats-officedocument.wordprocessingml.document" , 66 | "text/html"}) 67 | public Response processForm( 68 | @Context HttpServletRequest request, 69 | @Context HttpServletResponse response, 70 | @Context ServletContext servletContext, 71 | @FormDataParam("docxfile") InputStream docxInputStream, 72 | @FormDataParam("docxfile") FormDataContentDisposition docxFileDetail, 73 | @FormDataParam("adv") String editorHtml 74 | ) throws Docx4JException, IOException { 75 | 76 | try { 77 | 78 | HttpSession session = request.getSession(true); 79 | 80 | 81 | final WordprocessingMLPackage wordMLPackage; 82 | WordprocessingMLPackage tmpPkg=null; 83 | String docxname = getFileName(docxFileDetail.getFileName()); 84 | 85 | LoadFromZipNG loader = new LoadFromZipNG(); 86 | try { 87 | tmpPkg = (WordprocessingMLPackage)loader.get(docxInputStream ); 88 | } catch (Exception e) { 89 | throw new WebApplicationException( 90 | new Docx4JException("Error reading docx file (is this a valid docx?)"), 91 | Status.BAD_REQUEST); 92 | } 93 | 94 | if (tmpPkg==null) { 95 | throw new WebApplicationException( 96 | new Docx4JException("No docx file provided"), 97 | Status.BAD_REQUEST); 98 | } 99 | wordMLPackage = tmpPkg; 100 | // Store the docx, so we can later inject XHTML into it... 101 | session.setAttribute("docx", wordMLPackage); 102 | 103 | Editor editor = new Editor(); 104 | Editor.setContextPath(servletContext.getContextPath()); 105 | 106 | ResponseBuilder builder; 107 | if (editorHtml!=null && editorHtml.equals("bare")) { 108 | builder = editor.streamDocxAsHtml(wordMLPackage, session, EditorHTML.BARE, response); 109 | } else { 110 | builder = editor.streamDocxAsHtml(wordMLPackage, session, EditorHTML.CKEditor3, response); 111 | } 112 | return builder.build(); 113 | 114 | } catch (Exception e) { 115 | e.printStackTrace(); 116 | System.out.println(e.getMessage()); 117 | throw new WebApplicationException( 118 | new Docx4JException(e.getMessage(), e), 119 | Status.INTERNAL_SERVER_ERROR); 120 | } 121 | 122 | } 123 | 124 | /** 125 | * header sample 126 | * { 127 | * Content-Type=[image/png], 128 | * Content-Disposition=[form-data; name="file"; filename="filename.extension"] 129 | * } 130 | **/ 131 | private String getFileName(String name) { 132 | 133 | String finalFileName = name.trim().replaceAll("\"", ""); 134 | 135 | if (finalFileName.lastIndexOf(".")==-1) { 136 | return finalFileName; 137 | } else { 138 | return finalFileName.substring(0, finalFileName.lastIndexOf(".") ); 139 | } 140 | } 141 | 142 | } -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/LongTailHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.awt.Color; 21 | import java.io.ByteArrayOutputStream; 22 | import java.io.StringWriter; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import java.util.concurrent.atomic.AtomicInteger; 26 | import java.util.logging.Logger; 27 | 28 | import javax.servlet.http.HttpServletResponse; 29 | import javax.servlet.http.HttpSession; 30 | import javax.xml.bind.Unmarshaller; 31 | import javax.xml.parsers.DocumentBuilderFactory; 32 | import javax.xml.parsers.ParserConfigurationException; 33 | 34 | import org.capaxit.imagegenerator.Margin; 35 | import org.capaxit.imagegenerator.TextImage; 36 | import org.capaxit.imagegenerator.imagecallbacks.BackgroundColorCallback; 37 | import org.capaxit.imagegenerator.imageexporter.ImageType; 38 | import org.capaxit.imagegenerator.imageexporter.ImageWriter; 39 | import org.capaxit.imagegenerator.imageexporter.ImageWriterFactory; 40 | import org.capaxit.imagegenerator.impl.TextImageImpl; 41 | import org.capaxit.imagegenerator.textalign.GreedyTextWrapper; 42 | import org.docx4j.TextUtils; 43 | import org.docx4j.TraversalUtil; 44 | import org.docx4j.TraversalUtil.CallbackImpl; 45 | import org.docx4j.XmlUtils; 46 | import org.docx4j.convert.out.html.HTMLConversionContext; 47 | import org.docx4j.dml.CTBlip; 48 | import org.docx4j.dml.wordprocessingDrawing.Anchor; 49 | import org.docx4j.dml.wordprocessingDrawing.Inline; 50 | import org.docx4j.jaxb.Context; 51 | import org.docx4j.model.images.WordXmlPictureE10; 52 | import org.docx4j.model.images.WordXmlPictureE20; 53 | import org.docx4j.vml.CTImageData; 54 | import org.docx4j.wml.R; 55 | import org.w3c.dom.Document; 56 | import org.w3c.dom.DocumentFragment; 57 | import org.w3c.dom.Element; 58 | import org.w3c.dom.Node; 59 | import org.w3c.dom.traversal.NodeIterator; 60 | 61 | /** 62 | * Paragraph content (eg sectPr or oMathPara) can be preserved, 63 | * as can run content (most stuff is preserved at this level). 64 | * 65 | * Preserved structures are stored in a hashmap, and allocated 66 | * an ID. 67 | * 68 | * Reinstatement is then just a matter of matching the ID 69 | * on the non-editable content item image, with an entry in the 70 | * hashmap. 71 | */ 72 | public class LongTailHelper { 73 | 74 | private static final Logger jul = Logger.getLogger(LongTailHelper.class.getName()); 75 | 76 | private final static AtomicInteger counter = new AtomicInteger(); 77 | 78 | private static String getId() { 79 | return Editor.APP_KEY+"fcb"+counter.getAndIncrement(); 80 | // since we store this in the pkg user data, its nice to use a 'namespace' 81 | } 82 | 83 | /** 84 | * Takes a paragraph, or some specific object (eg a footnote reference), 85 | * and returns an image which will represent it for round trip purposes. 86 | * 87 | * @param context 88 | * @param nodeIt 89 | * @param objectCount 90 | * @param frozenContentTypeHint 91 | * @return 92 | */ 93 | public static DocumentFragment createFrozenContentBlock( 94 | HTMLConversionContext context, 95 | NodeIterator nodeIt, 96 | int objectCount, 97 | String frozenContentTypeHint, HttpServletResponse response) { 98 | 99 | /* 100 | self::v:textbox 101 | or self::v:imagedata 102 | or self::w:control 103 | or self::a:blip 104 | or self::dgm:relIds 105 | or self::w:pict 106 | or self::w:object 107 | or self::c:chart 108 | or self::m:oMathPara 109 | 110 | or self::w:sectPr 111 | or self::o:signatureline 112 | 113 | */ 114 | 115 | try { 116 | 117 | // context.getLog().info("objectCount " + objectCount); 118 | // jul.info("objectCount " + objectCount); 119 | 120 | Object jaxbEl = null; 121 | if (nodeIt!=null) { //It is never null 122 | Node n = nodeIt.nextNode(); 123 | if (n!=null) { 124 | Unmarshaller u = Context.jc.createUnmarshaller(); 125 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 126 | jaxbEl = u.unmarshal(n); 127 | } 128 | } 129 | 130 | // Store the frozen object in the pkg 131 | // (this is easier than subclassing HTMLSettings, passing the storage map through the XSLT etc) 132 | String id = getId(); 133 | context.getWmlPackage().setUserData(id, jaxbEl); 134 | 135 | // System.out.println(id + " --> \n" + XmlUtils.marshaltoString(jaxbEl, true, true)); 136 | 137 | 138 | // Get the text content of wmlP 139 | StringWriter sw = new StringWriter(); 140 | TextUtils.extractText(jaxbEl, sw); 141 | String textContent = sw.toString().trim(); 142 | 143 | if (objectCount==1) { 144 | 145 | if (frozenContentTypeHint.equals("sectPr")) { 146 | return staticPlaceholder("sectPr.png", id, true); 147 | } 148 | 149 | if (frozenContentTypeHint.equals("br")) { 150 | // TODO - handle other break types 151 | return staticPlaceholder("page_break.png", id, true); 152 | } 153 | 154 | if (frozenContentTypeHint.equals("tab")) { 155 | return staticPlaceholder("tab.png", id, false); 156 | } 157 | 158 | if (frozenContentTypeHint.equals("signatureline")) { 159 | return staticPlaceholder("signature.png", id, true); 160 | } 161 | 162 | if (frozenContentTypeHint.equals("chart")) { 163 | return staticPlaceholder("chart.png", id, true); 164 | } 165 | 166 | if (frozenContentTypeHint.equals("oMathPara")) { 167 | return staticPlaceholder("equation.png", id, true); 168 | } 169 | 170 | if (frozenContentTypeHint.equals("bookmarkStart")) { 171 | return staticPlaceholder("bookmark_start.png", id, false); 172 | } 173 | 174 | if (frozenContentTypeHint.equals("bookmarkEnd")) { 175 | return staticPlaceholder("bookmark_end.png", id, false); 176 | } 177 | 178 | if (frozenContentTypeHint.equals("commentRangeStart")) { 179 | return staticPlaceholder("comment_range_start.png", id, false); 180 | } 181 | 182 | if (frozenContentTypeHint.equals("commentRangeEnd")) { 183 | return staticPlaceholder("comment_range_end.png", id, false); 184 | } 185 | 186 | if (frozenContentTypeHint.equals("commentReference")) { 187 | return staticPlaceholder("comment_ref.png", id, false); 188 | } 189 | 190 | if (frozenContentTypeHint.equals("footnoteReference")) { 191 | return staticPlaceholder("footnote.png", id, false); 192 | } 193 | 194 | if (frozenContentTypeHint.equals("endnoteReference")) { 195 | return staticPlaceholder("endnote.png", id, false); 196 | } 197 | 198 | if (jaxbEl instanceof R) { 199 | 200 | R p = (R)jaxbEl; 201 | 202 | // Handle w:drawing image 203 | BlipFinder bf = new BlipFinder(); 204 | new TraversalUtil(p.getContent(), bf); 205 | 206 | SessionImageHandler sih = (SessionImageHandler)context.getImageHandler(); 207 | sih.setResponse(response); 208 | 209 | if (bf.blips.size()==1) { 210 | // handle an ordinary image - WordXmlPictureE20 211 | DocumentFragment createHtmlImgE20 = WordXmlPictureE20.createHtmlImgE20(context, bf.anchorOrInline, id); 212 | return createHtmlImgE20; 213 | 214 | } else { 215 | 216 | // Handle w:pict//v:imagedata 217 | ShapeFinder sf = new ShapeFinder(); 218 | new TraversalUtil(p.getContent(), sf); 219 | 220 | if (sf.shapeRefs.size()==1) { 221 | // handle VML image 222 | DocumentFragment createHtmlImgE10 = WordXmlPictureE10.createHtmlImgE10(context, sf.pict, id); 223 | return createHtmlImgE10; 224 | } 225 | } 226 | } 227 | } 228 | 229 | // Otherwise, its not just a simple image 230 | if (context.getLog().isDebugEnabled() && jaxbEl!=null) { 231 | context.getLog().debug(XmlUtils.marshaltoString(jaxbEl, true, true)); 232 | } 233 | 234 | 235 | // this is where we generate a placeholder image 236 | HttpSession session = (HttpSession)context.getWmlPackage().getUserData("HttpSession"); 237 | String url = createPlaceholder(session, id, frozenContentTypeHint, textContent); 238 | 239 | // Create a DOM document to take the results 240 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 241 | Document document = factory.newDocumentBuilder().newDocument(); 242 | // Element xhtmlBlock = document.createElement("p"); 243 | // document.appendChild(xhtmlBlock); 244 | 245 | Element xhtmlBlock = document.createElement("img"); 246 | document.appendChild(xhtmlBlock); 247 | 248 | xhtmlBlock.setAttribute("src", response.encodeURL(url)); 249 | xhtmlBlock.setAttribute("id", id); 250 | 251 | DocumentFragment docfrag = document.createDocumentFragment(); 252 | docfrag.appendChild(document.getDocumentElement()); 253 | 254 | return docfrag; 255 | 256 | } catch (Exception e) { 257 | context.getLog().error(e.getMessage(), e); 258 | } 259 | 260 | return null; 261 | } 262 | 263 | private static DocumentFragment staticPlaceholder(String imageName, String id, boolean block) { 264 | 265 | String url = Editor.getContextPath()+"/placeholders/" + imageName; 266 | 267 | // Create a DOM document to take the results 268 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 269 | Document document=null; 270 | try { 271 | document = factory.newDocumentBuilder().newDocument(); 272 | } catch (ParserConfigurationException e) { 273 | // TODO Auto-generated catch block 274 | e.printStackTrace(); 275 | } 276 | // Element xhtmlBlock = document.createElement("p"); 277 | // document.appendChild(xhtmlBlock); 278 | 279 | Element xhtmlBlock = document.createElement("img"); 280 | document.appendChild(xhtmlBlock); 281 | 282 | xhtmlBlock.setAttribute("src", url); 283 | xhtmlBlock.setAttribute("id", id); 284 | if(block) { 285 | xhtmlBlock.setAttribute("style", "display:block"); 286 | } 287 | 288 | DocumentFragment docfrag = document.createDocumentFragment(); 289 | docfrag.appendChild(document.getDocumentElement()); 290 | 291 | return docfrag; 292 | 293 | } 294 | 295 | private static String createPlaceholder(HttpSession session, String id, String frozenContentTypeHint, String textContent) throws Exception { 296 | 297 | // There's another library https://code.google.com/p/litetext/ 298 | // which I haven't tried, developed for Google App Engine use where AWT and BufferedImage et al do not exist. 299 | 300 | TextImage textImage; 301 | if (frozenContentTypeHint.equals("textbox") ) { 302 | 303 | if (textContent.length()==0) { 304 | textContent="[empty text box]"; 305 | } 306 | 307 | int rows = (int) Math.ceil((double)textContent.length()/40); // 40 chars per row 308 | int height = rows * 16; 309 | 310 | textImage = new TextImageImpl(300, height, new Margin(0, 0)); 311 | textImage.useTextWrapper(new GreedyTextWrapper() ); 312 | textImage.wrap(true); 313 | 314 | textImage.performAction(new BackgroundColorCallback(Color.LIGHT_GRAY, Color.BLACK, textImage)); 315 | 316 | textImage.write(textContent); 317 | 318 | } else { 319 | 320 | // 1. create a new TextImageImpl with a size of 300x300 pixels 321 | // and a left and top margin of 5 pixels. The default font is SansSerif, 322 | // PLAIN,12 323 | textImage = new TextImageImpl(100, 20, new Margin(0, 0)); 324 | 325 | // String tag = id.substring(id.indexOf(":")+1); 326 | 327 | // 2. These methods add text and a newline 328 | // textImage.writeLine("[" + tag + "]"); 329 | textImage.writeLine("[" + frozenContentTypeHint + "]"); 330 | 331 | } 332 | 333 | // 3. Add explicit newlines. All methods can be chained for convenience. 334 | // textImage.newLine().newLine(); 335 | // textImage.withFontStyle(Style.UNDERLINED).write("Hello world!"); 336 | 337 | ImageWriter imageWriter = ImageWriterFactory.getImageWriter(ImageType.PNG); 338 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 339 | // imageWriter.writeImageToFile(textImage, new File("simple.png")); 340 | imageWriter.writeImageToOutputStream(textImage, baos); 341 | 342 | // session.setAttribute(id, baos.toByteArray()); 343 | SessionImageHandler.getImageMap(session).put(id, baos.toByteArray()); 344 | 345 | 346 | return Editor.getContextPath() + "/services/image/" + id; 347 | 348 | 349 | } 350 | 351 | static class BlipFinder extends CallbackImpl { 352 | 353 | Object anchorOrInline; // BlipFinder is only useful to us when there is a single element in the List, so this is OK 354 | 355 | List blips = new ArrayList(); 356 | 357 | @Override 358 | public List apply(Object o) { 359 | 360 | if (o instanceof Inline) { 361 | anchorOrInline = o; 362 | } else if (o instanceof Anchor) { 363 | anchorOrInline = o; 364 | } else if (o instanceof CTBlip) { 365 | blips.add((CTBlip)o); 366 | } 367 | 368 | return null; 369 | } 370 | } 371 | 372 | static class ShapeFinder extends CallbackImpl { 373 | 374 | org.docx4j.wml.Pict pict; // ShapeFinder is only useful to us when there is a single element in the List, so this is OK 375 | List shapeRefs = new ArrayList(); 376 | 377 | @Override 378 | public List apply(Object o) { 379 | 380 | if (o instanceof org.docx4j.wml.Pict) { 381 | this.pict = (org.docx4j.wml.Pict)o; 382 | } else if (o instanceof org.docx4j.vml.CTImageData) { 383 | CTImageData imageData = (org.docx4j.vml.CTImageData)o; 384 | shapeRefs.add(imageData); 385 | } 386 | return null; 387 | } 388 | } 389 | 390 | // public static void main(String[] args) throws Exception { 391 | // String textContent = "Product Launch Revenue Plan"; 392 | // System.out.println(Math.ceil((double)textContent.length()/40)); 393 | // } 394 | } 395 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/PPrNone.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | /** 19 | * 20 | */ 21 | package org.plutext.htmleditor; 22 | 23 | /** 24 | * Used to indicate that this P had no PPr. 25 | * 26 | * @author jharrop 27 | * 28 | */ 29 | public class PPrNone { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/SessionAwareAbstractTableWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | Licensed to Plutext Pty Ltd under one or more contributor license agreements. 20 | 21 | * This file is part of docx4j. 22 | 23 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); 24 | you may not use this file except in compliance with the License. 25 | 26 | You may obtain a copy of the License at 27 | 28 | http://www.apache.org/licenses/LICENSE-2.0 29 | 30 | Unless required by applicable law or agreed to in writing, software 31 | distributed under the License is distributed on an "AS IS" BASIS, 32 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 | See the License for the specific language governing permissions and 34 | limitations under the License. 35 | 36 | */ 37 | package org.plutext.htmleditor; 38 | 39 | import java.util.ArrayList; 40 | import java.util.List; 41 | import java.util.Map; 42 | import java.util.TreeMap; 43 | 44 | import javax.xml.bind.JAXBElement; 45 | import javax.xml.transform.TransformerException; 46 | 47 | import org.docx4j.UnitsOfMeasurement; 48 | import org.docx4j.XmlUtils; 49 | import org.docx4j.convert.out.common.AbstractWmlConversionContext; 50 | import org.docx4j.convert.out.common.writer.AbstractSimpleWriter; 51 | import org.docx4j.convert.out.common.writer.AbstractTableWriterModel; 52 | import org.docx4j.convert.out.common.writer.AbstractTableWriterModelCell; 53 | import org.docx4j.convert.out.common.writer.AbstractTableWriterModelRow; 54 | import org.docx4j.jaxb.Context; 55 | import org.docx4j.model.properties.Property; 56 | import org.docx4j.model.properties.PropertyFactory; 57 | import org.docx4j.model.properties.table.AbstractBorder; 58 | import org.docx4j.model.properties.table.BorderBottom; 59 | import org.docx4j.model.properties.table.BorderLeft; 60 | import org.docx4j.model.properties.table.BorderRight; 61 | import org.docx4j.model.properties.table.BorderTop; 62 | import org.docx4j.model.properties.table.CellMarginBottom; 63 | import org.docx4j.model.properties.table.CellMarginLeft; 64 | import org.docx4j.model.properties.table.CellMarginRight; 65 | import org.docx4j.model.properties.table.CellMarginTop; 66 | import org.docx4j.model.properties.table.tc.Shading; 67 | import org.docx4j.model.properties.table.tc.TextAlignmentVertical; 68 | import org.docx4j.model.properties.table.tr.TrHeight; 69 | import org.docx4j.wml.CTBorder; 70 | import org.docx4j.wml.CTHeight; 71 | import org.docx4j.wml.CTShd; 72 | import org.docx4j.wml.CTTblCellMar; 73 | import org.docx4j.wml.CTTblPrBase; 74 | import org.docx4j.wml.CTTblPrEx; 75 | import org.docx4j.wml.STBorder; 76 | import org.docx4j.wml.STShd; 77 | import org.docx4j.wml.TblBorders; 78 | import org.docx4j.wml.TblGridCol; 79 | import org.docx4j.wml.TcPr; 80 | import org.docx4j.wml.TrPr; 81 | import org.slf4j.Logger; 82 | import org.w3c.dom.DOMException; 83 | import org.w3c.dom.Document; 84 | import org.w3c.dom.DocumentFragment; 85 | import org.w3c.dom.Element; 86 | import org.w3c.dom.Node; 87 | 88 | /* 89 | * @author Alberto Zerolo, Adam Schmideg, Jason Harrop 90 | * @since 3.0.0 91 | * 92 | */ 93 | public abstract class SessionAwareAbstractTableWriter extends AbstractSimpleWriter { 94 | public static final String WRITER_ID = "w:tbl"; 95 | 96 | 97 | protected static final int NODE_TABLE = 0; 98 | protected static final int NODE_TABLE_COLUMN_GROUP = 1; 99 | protected static final int NODE_TABLE_COLUMN = 2; 100 | protected static final int NODE_TABLE_HEADER = 3; 101 | protected static final int NODE_TABLE_HEADER_ROW = 4; 102 | protected static final int NODE_TABLE_HEADER_CELL = 5; 103 | protected static final int NODE_TABLE_BODY = 6; 104 | protected static final int NODE_TABLE_BODY_ROW = 7; 105 | protected static final int NODE_TABLE_BODY_CELL = 8; 106 | 107 | protected static final Map PATTERN_PERCENTAGES = new TreeMap(); 108 | 109 | static { 110 | /* 111 | * These patterns cause a reset 112 | PATTERN_PERCENTAGES.put("clear", -1); 113 | PATTERN_PERCENTAGES.put("nil", -1); 114 | 115 | * and these can't be approximated by a background color 116 | * so they are ignored 117 | PATTERN_PERCENTAGES.put("diagStripe", -1); 118 | PATTERN_PERCENTAGES.put("horzStripe", -1); 119 | PATTERN_PERCENTAGES.put("thinDiagStripe", -1); 120 | PATTERN_PERCENTAGES.put("thinHorzStripe", -1); 121 | PATTERN_PERCENTAGES.put("thinReverseDiagStripe", -1); 122 | PATTERN_PERCENTAGES.put("thinVertStripe", -1); 123 | PATTERN_PERCENTAGES.put("vertStripe", -1); 124 | */ 125 | 126 | 127 | // These Patterns are aproximated by a background color 128 | PATTERN_PERCENTAGES.put("diagCross", 50); 129 | PATTERN_PERCENTAGES.put("horzCross", 50); 130 | 131 | PATTERN_PERCENTAGES.put("thinDiagCross", 25); 132 | PATTERN_PERCENTAGES.put("thinHorzCross", 25); 133 | 134 | PATTERN_PERCENTAGES.put("pct5", 5); 135 | PATTERN_PERCENTAGES.put("pct10", 10); 136 | PATTERN_PERCENTAGES.put("pct12", 12); 137 | PATTERN_PERCENTAGES.put("pct15", 15); 138 | PATTERN_PERCENTAGES.put("pct20", 20); 139 | PATTERN_PERCENTAGES.put("pct25", 25); 140 | PATTERN_PERCENTAGES.put("pct30", 30); 141 | PATTERN_PERCENTAGES.put("pct35", 35); 142 | PATTERN_PERCENTAGES.put("pct37", 37); 143 | PATTERN_PERCENTAGES.put("pct40", 40); 144 | PATTERN_PERCENTAGES.put("pct45", 45); 145 | PATTERN_PERCENTAGES.put("pct50", 50); 146 | PATTERN_PERCENTAGES.put("pct55", 55); 147 | PATTERN_PERCENTAGES.put("pct60", 60); 148 | PATTERN_PERCENTAGES.put("pct62", 62); 149 | PATTERN_PERCENTAGES.put("pct65", 65); 150 | PATTERN_PERCENTAGES.put("pct70", 70); 151 | PATTERN_PERCENTAGES.put("pct75", 75); 152 | PATTERN_PERCENTAGES.put("pct80", 80); 153 | PATTERN_PERCENTAGES.put("pct85", 85); 154 | PATTERN_PERCENTAGES.put("pct87", 87); 155 | PATTERN_PERCENTAGES.put("pct90", 90); 156 | PATTERN_PERCENTAGES.put("pct95", 95); 157 | PATTERN_PERCENTAGES.put("solid", 100); 158 | } 159 | 160 | 161 | protected static class TableModelTransformState implements TransformState { 162 | 163 | // The last table number, in document order, 164 | // which we have processed. 165 | // The idea is to be able to write an id (unique within the document) to each 166 | // table. 167 | int idx = 0; 168 | 169 | /** 170 | * @return the idx 171 | */ 172 | public int getIdx() { 173 | return idx; 174 | } 175 | 176 | /** 177 | * @param idx the idx to set 178 | */ 179 | public void incrementIdx() { 180 | idx++; 181 | } 182 | } 183 | 184 | public static String getId(int idx) { 185 | return "docx4j_tbl_" + idx; 186 | } 187 | 188 | 189 | protected SessionAwareAbstractTableWriter() { 190 | super(WRITER_ID); 191 | } 192 | 193 | @Override 194 | public TransformState createTransformState() { 195 | return new TableModelTransformState(); 196 | } 197 | 198 | @Override 199 | public Node toNode(AbstractWmlConversionContext context, Object unmarshalledNode, Node content, TransformState transformState, Document doc) throws TransformerException { 200 | AbstractTableWriterModel table = new AbstractTableWriterModel(); 201 | 202 | table.build(context, unmarshalledNode, content); 203 | if (getLog().isDebugEnabled()) { 204 | getLog().debug("Table asXML:\n" + table.debugStr()); 205 | } 206 | 207 | DocumentFragment docfrag = doc.createDocumentFragment(); 208 | Element tableRoot = createNode(doc, null, NODE_TABLE); 209 | List rowProperties = new ArrayList(); 210 | int rowPropertiesTableSize = -1; 211 | 212 | 213 | // The tbl level customisation: store w:tblPr and w:tblGrid 214 | String id = getId(((TableModelTransformState)transformState).getIdx()); 215 | tableRoot.setAttribute("id", id ); 216 | context.getWmlPackage().setUserData(id+"#Pr", table.getTblPr()); 217 | context.getWmlPackage().setUserData(id+"#Grid", table.getTblGrid()); 218 | 219 | ((TableModelTransformState)transformState).incrementIdx(); 220 | 221 | 222 | List cellProperties = new ArrayList(); 223 | int cellPropertiesTableSize = -1; 224 | int cellPropertiesRowSize = -1; 225 | boolean inHeader = (table.getHeaderMaxRow() > -1); 226 | 227 | AbstractTableWriterModelRow rowModel = null; 228 | Element rowContainer = null; 229 | Element row = null; 230 | Element cellNode = null; 231 | 232 | createRowProperties(rowProperties, table.getEffectiveTableStyle().getTrPr(), true); 233 | rowPropertiesTableSize = rowProperties.size(); 234 | createCellProperties(cellProperties, table.getEffectiveTableStyle().getTrPr()); 235 | createCellProperties(cellProperties, table.getEffectiveTableStyle().getTcPr()); 236 | // will apply these as a default on each td, and then override 237 | createCellProperties(cellProperties, table.getEffectiveTableStyle().getTblPr()); 238 | cellPropertiesTableSize = cellProperties.size(); 239 | 240 | docfrag.appendChild(tableRoot); 241 | applyTableStyles(context, table, transformState, tableRoot); 242 | 243 | // setup column widths 244 | createColumns(context, table, transformState, doc, tableRoot); 245 | 246 | rowContainer = createNode(doc, tableRoot, (inHeader ? NODE_TABLE_HEADER : NODE_TABLE_BODY)); 247 | tableRoot.appendChild(rowContainer); 248 | 249 | applyTableRowContainerCustomAttributes(context, table, transformState, rowContainer, inHeader); 250 | 251 | for (int rowIndex = 0; rowIndex < table.getCells().size(); rowIndex++) { 252 | rowModel = table.getCells().get(rowIndex); 253 | 254 | if ((inHeader) && (rowIndex > table.getHeaderMaxRow())) { 255 | rowContainer = createNode(doc, tableRoot, NODE_TABLE_BODY); 256 | tableRoot.appendChild(rowContainer); 257 | inHeader = false; 258 | applyTableRowContainerCustomAttributes(context, table, transformState, rowContainer, inHeader); 259 | } 260 | row = createNode(doc, rowContainer, (inHeader ? NODE_TABLE_HEADER_ROW : NODE_TABLE_BODY_ROW)); 261 | TrPr trPr = rowModel.getRowProperties(); 262 | CTTblPrEx tblPrEx = rowModel.getRowPropertiesExceptions(); 263 | 264 | // The row level customisation: store w:trPr and w:tblPrEx 265 | String rowId = id+"_"+rowIndex; 266 | row.setAttribute("id", rowId ); 267 | context.getWmlPackage().setUserData(rowId+"#Pr", trPr ); 268 | context.getWmlPackage().setUserData(rowId+"#PrEx", tblPrEx ); 269 | 270 | 271 | createRowProperties(rowProperties, trPr, false); 272 | processAttributes(context, rowProperties, row); 273 | applyTableRowCustomAttributes(context, table, transformState, row, rowIndex, inHeader); 274 | 275 | createCellProperties(cellProperties, trPr); 276 | createCellProperties(cellProperties, tblPrEx); 277 | cellPropertiesRowSize = cellProperties.size(); 278 | 279 | int cellCounter=0; 280 | for (AbstractTableWriterModelCell cell : rowModel.getRowContents()) { 281 | // process cell 282 | 283 | if (cell.isDummy()) { 284 | if (!cell.isVMerged()) { 285 | //Dummy-Cells resulting from vertical merged cells shouldn't be included 286 | cellNode = createNode(doc, row, (inHeader ? NODE_TABLE_HEADER_CELL : NODE_TABLE_BODY_CELL)); 287 | row.appendChild(cellNode); 288 | applyTableCellCustomAttributes(context, table, transformState, cell, cellNode, inHeader, true); 289 | } 290 | } 291 | else { 292 | 293 | cellNode = createNode(doc, row, (inHeader ? NODE_TABLE_HEADER_CELL : NODE_TABLE_BODY_CELL)); 294 | row.appendChild(cellNode); 295 | 296 | // The cell level customisation: store w:tcPr 297 | // - only for real cells 298 | String cellId = id+"_"+rowIndex+"_"+cellCounter; 299 | cellNode.setAttribute("id", cellId ); 300 | context.getWmlPackage().setUserData(cellId+"#Pr", cell.getTcPr() ); 301 | cellCounter++; 302 | 303 | //Apply cell style 304 | createCellProperties(cellProperties, cell.getTcPr()); 305 | processAttributes(context, cellProperties, cellNode); 306 | applyTableCellCustomAttributes(context, table, transformState, cell, cellNode, inHeader, false); 307 | //remove properties defined on cell level 308 | resetProperties(cellProperties, cellPropertiesRowSize); 309 | 310 | // insert content into cell 311 | // skipping w:tc node itself, insert only its children 312 | if (cell.getContent() == null) { 313 | getLog().warn("model cell had no contents!"); 314 | } else { 315 | getLog().debug("copying cell contents.."); 316 | XmlUtils.treeCopy(cell.getContent().getChildNodes(), 317 | cellNode); 318 | } 319 | } 320 | } 321 | //remove properties defined on row level 322 | resetProperties(cellProperties, cellPropertiesTableSize); 323 | resetProperties(rowProperties, rowPropertiesTableSize); 324 | } 325 | return docfrag; 326 | } 327 | 328 | protected Element createNode(Document doc, Element parent, int nodeType) { 329 | Element ret = createNode(doc, nodeType); 330 | if ((ret != null) && (parent != null)) { 331 | parent.appendChild(ret); 332 | } 333 | return (ret != null ? ret : parent); 334 | } 335 | 336 | protected void createColumns(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Document doc, Element tableRoot) throws DOMException { 337 | List gridCols = (table.getTblGrid() != null ? table.getTblGrid().getGridCol() : null); 338 | Element columnGroup = createNode(doc, tableRoot, NODE_TABLE_COLUMN_GROUP); 339 | Element column = null; 340 | 341 | applyColumnGroupCustomAttributes(context, table, transformState, columnGroup); 342 | if ((gridCols != null) && (!gridCols.isEmpty())) { 343 | for(int i=0; i tableProperties = null; 358 | int tblCellSpacing = -1; 359 | 360 | // This handles: 361 | // - position (tblPr/tblInd) 362 | // - table-layout 363 | 364 | tableProperties = PropertyFactory.createProperties(table.getEffectiveTableStyle().getTblPr()); 365 | 366 | // Borders, shading 367 | if (table.getEffectiveTableStyle().getTcPr()!=null) { 368 | PropertyFactory.createPropertiesTable(tableProperties, table.getEffectiveTableStyle().getTcPr()); 369 | } 370 | 371 | // vAlign fix: match Word's default of top 372 | if (table.getEffectiveTableStyle().getTcPr()==null 373 | || table.getEffectiveTableStyle().getTcPr().getVAlign()==null) { 374 | tableProperties.add(new TextAlignmentVertical()); 375 | } 376 | 377 | if (!table.isDrawTableBorders()) { 378 | //isn't nice, but better than passing a lot of flags to the PropertyFactory 379 | //1. remove any borders and shading 380 | for (int i=tableProperties.size()-1; i>=0; i--) { 381 | if ((tableProperties.get(i) instanceof Shading) || 382 | (tableProperties.get(i) instanceof AbstractBorder)) { 383 | tableProperties.remove(i); 384 | } 385 | } 386 | //2. apply explicit none-borders and transparent shading 387 | // (in html there might be borders and shading inherited from the class) 388 | appendNoneBordersAndShading(tableProperties); 389 | } 390 | 391 | processAttributes(context, tableProperties, tableRoot); 392 | 393 | // this sets the ID 394 | applyTableCustomAttributes(context, table, transformState, tableRoot); 395 | } 396 | 397 | protected void appendNoneBordersAndShading(List tableProperties) { 398 | CTBorder ctBrdr = null; 399 | CTShd shd = Context.getWmlObjectFactory().createCTShd(); 400 | ctBrdr = Context.getWmlObjectFactory().createCTBorder(); 401 | ctBrdr.setVal(STBorder.NONE); 402 | tableProperties.add(new BorderLeft(ctBrdr)); 403 | ctBrdr = Context.getWmlObjectFactory().createCTBorder(); 404 | ctBrdr.setVal(STBorder.NONE); 405 | tableProperties.add(new BorderRight(ctBrdr)); 406 | ctBrdr = Context.getWmlObjectFactory().createCTBorder(); 407 | ctBrdr.setVal(STBorder.NONE); 408 | tableProperties.add(new BorderTop(ctBrdr)); 409 | ctBrdr = Context.getWmlObjectFactory().createCTBorder(); 410 | ctBrdr.setVal(STBorder.NONE); 411 | tableProperties.add(new BorderBottom(ctBrdr)); 412 | shd.setColor("auto"); 413 | shd.setFill("auto"); 414 | shd.setVal(STShd.CLEAR); 415 | tableProperties.add(new Shading(shd)); 416 | } 417 | 418 | protected void createRowProperties(List properties, TrPr trPr, boolean includeDefaultHeight) { 419 | JAXBElement trHeight = (trPr != null ? 420 | (JAXBElement)getElement(trPr.getCnfStyleOrDivIdOrGridBefore(), "trHeight") : 421 | null); 422 | if (trHeight != null) { 423 | properties.add(new TrHeight(trHeight.getValue())); 424 | } 425 | } 426 | 427 | protected void createCellProperties(List properties, TrPr trPr) { 428 | 429 | } 430 | 431 | 432 | protected void createCellProperties(List properties, CTTblPrBase tblPr) { 433 | TblBorders tblBorders = tblPr.getTblBorders(); 434 | CTTblCellMar tblCellMargin = tblPr.getTblCellMar(); 435 | if (tblBorders!=null) { 436 | if (tblBorders.getInsideH()!=null) { 437 | properties.add(new BorderTop(tblBorders.getTop())); 438 | properties.add(new BorderBottom(tblBorders.getBottom())); 439 | } 440 | if (tblBorders.getInsideV()!=null) { 441 | properties.add(new BorderRight(tblBorders.getRight())); 442 | properties.add(new BorderLeft(tblBorders.getLeft())); 443 | } 444 | } 445 | 446 | if (tblCellMargin != null) { 447 | if (tblCellMargin.getTop() != null) 448 | properties.add(new CellMarginTop(tblCellMargin.getTop())); 449 | if (tblCellMargin.getBottom() != null) 450 | properties.add(new CellMarginBottom(tblCellMargin.getBottom())); 451 | if (tblCellMargin.getLeft() != null) 452 | properties.add(new CellMarginLeft(tblCellMargin.getLeft())); 453 | if (tblCellMargin.getRight() != null) 454 | properties.add(new CellMarginRight(tblCellMargin.getRight())); 455 | } 456 | 457 | } 458 | 459 | protected void createCellProperties(List properties, TcPr tcPr) { 460 | if (tcPr != null) { 461 | PropertyFactory.createProperties(properties, tcPr); 462 | } 463 | } 464 | 465 | protected void createCellProperties(List properties, CTTblPrEx tblPrEx) { 466 | } 467 | 468 | protected JAXBElement getElement(List> cnfStyleOrDivIdOrGridBefore, String localName) { 469 | JAXBElement element = null; 470 | if ((cnfStyleOrDivIdOrGridBefore != null) && (!cnfStyleOrDivIdOrGridBefore.isEmpty())) { 471 | for (int i=0; i properties, Element element) { 482 | CTShd shd = null; 483 | Shading shading = null; 484 | int bgColor = 0xffffff; //the background color of the page is assumed as white 485 | int fgColor = 0; //the default color of the font is assumed as black 486 | int pctPattern = -1; 487 | for (int i=0; i properties, int size) { 554 | while (properties.size() > size) properties.remove(properties.size() - 1); 555 | } 556 | 557 | /* 558 | * These are the main methods the subclasses have to or should override 559 | */ 560 | protected abstract Logger getLog(); 561 | 562 | protected abstract Element createNode(Document doc, int nodeType); 563 | 564 | protected abstract void applyAttributes(AbstractWmlConversionContext context, List properties, Element element); 565 | 566 | 567 | protected void applyTableCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element tableRoot) { 568 | } 569 | 570 | protected void applyColumnGroupCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element columnGroup) { 571 | } 572 | 573 | protected void applyColumnCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element column, int columnIndex, int columnWidth) { 574 | } 575 | 576 | protected void applyTableRowContainerCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element rowContainer, boolean isHeader) { 577 | } 578 | 579 | protected void applyTableRowCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, Element row, int rowIndex, boolean isHeader) { 580 | } 581 | 582 | protected void applyTableCellCustomAttributes(AbstractWmlConversionContext context, AbstractTableWriterModel table, TransformState transformState, AbstractTableWriterModelCell tableCell, Element cellNode, boolean isHeader, boolean isDummyCell) { 583 | } 584 | 585 | } 586 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/SessionImageHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.util.HashMap; 21 | 22 | import javax.servlet.http.HttpServletResponse; 23 | import javax.servlet.http.HttpSession; 24 | 25 | import org.docx4j.model.images.AbstractConversionImageHandler; 26 | import org.docx4j.openpackaging.exceptions.Docx4JException; 27 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 28 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 29 | 30 | public class SessionImageHandler extends AbstractConversionImageHandler { 31 | 32 | HttpSession session; 33 | public SessionImageHandler(HttpSession session) { 34 | 35 | 36 | super("dummyImageDirPath", false); 37 | this.session = session; 38 | } 39 | 40 | private HttpServletResponse response; 41 | protected void setResponse(HttpServletResponse response) { 42 | this.response = response; 43 | } 44 | 45 | @Override 46 | protected String createStoredImage(BinaryPart binaryPart, byte[] bytes) 47 | throws Docx4JException { 48 | 49 | String name = this.getImageName(binaryPart); 50 | 51 | // The images associated with this docx are stored in a map in the docx 52 | getImageMap(session).put(name, bytes); 53 | 54 | return getUrl(name); 55 | 56 | } 57 | 58 | protected String getUrl(String name) { 59 | return response.encodeURL(Editor.getContextPath() + "/services/image/" + name); 60 | } 61 | 62 | @SuppressWarnings("unchecked") 63 | public 64 | static HashMap getImageMap(HttpSession session) { 65 | 66 | WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)session.getAttribute("docx"); 67 | return (HashMap)wordMLPackage.getUserData(Editor.APP_KEY + "imageMap"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/SessionPPrHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | import javax.xml.bind.JAXBException; 23 | import javax.xml.bind.Unmarshaller; 24 | 25 | import org.docx4j.convert.out.html.HTMLConversionContext; 26 | import org.docx4j.convert.out.html.XsltHTMLFunctions; 27 | import org.docx4j.jaxb.Context; 28 | import org.docx4j.wml.PPr; 29 | import org.w3c.dom.DocumentFragment; 30 | import org.w3c.dom.Element; 31 | import org.w3c.dom.Node; 32 | import org.w3c.dom.traversal.NodeIterator; 33 | 34 | public class SessionPPrHandler { 35 | 36 | private final static AtomicInteger counter = new AtomicInteger(); 37 | 38 | private static String getId() { 39 | return Editor.APP_KEY+"p"+counter.getAndIncrement(); 40 | // since we store this in the pkg user data, its nice to use a 'namespace' 41 | } 42 | 43 | 44 | public static DocumentFragment createBlockForPPr( 45 | HTMLConversionContext context, 46 | NodeIterator pPrNodeIt, 47 | String pStyleVal, NodeIterator childResults ) { 48 | 49 | DocumentFragment df = XsltHTMLFunctions.createBlockForPPr(context, pPrNodeIt, pStyleVal, childResults); 50 | 51 | PPr pPr = null; 52 | if (pPrNodeIt!=null) { 53 | pPrNodeIt.previousNode(); // since it was read already 54 | Node n = pPrNodeIt.nextNode(); 55 | if (n!=null) { 56 | try { 57 | Unmarshaller u = Context.jc.createUnmarshaller(); 58 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 59 | Object jaxb = u.unmarshal(n); 60 | pPr = (PPr)jaxb; 61 | } catch (ClassCastException e) { 62 | context.getLog().error("Couldn't cast to PPr!"); 63 | } catch (JAXBException e) { 64 | // TODO Auto-generated catch block 65 | e.printStackTrace(); 66 | } 67 | } 68 | } 69 | 70 | // Store the ppr object in the pkg 71 | // (this is easier than subclassing HTMLSettings, passing the storage map through the XSLT etc) 72 | String id = getId(); 73 | 74 | if (pPr==null) { 75 | context.getWmlPackage().setUserData(id, new PPrNone()); 76 | } else { 77 | context.getWmlPackage().setUserData(id, pPr); 78 | } 79 | Element e = (Element)df.getFirstChild(); 80 | e.setAttribute("id", id); 81 | 82 | 83 | return df; 84 | 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/SessionStyleHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor; 19 | 20 | import javax.servlet.http.HttpSession; 21 | 22 | import org.docx4j.convert.out.ConversionHTMLStyleElementHandler; 23 | import org.docx4j.openpackaging.packages.OpcPackage; 24 | import org.w3c.dom.Document; 25 | import org.w3c.dom.Element; 26 | 27 | /** 28 | * Put the CSS into the session, so it is available when the GET request comes. 29 | * 30 | * Done this way, since EditorCSS doesn't have ready access to conversionContext, and so can't itself 31 | * invoke org.docx4j.convert.out.html.XsltHTMLFunctions.appendStyleElement($conversionContext) 32 | * 33 | */ 34 | public class SessionStyleHandler implements ConversionHTMLStyleElementHandler { 35 | 36 | HttpSession session; 37 | public SessionStyleHandler(HttpSession session) { 38 | 39 | this.session = session; 40 | } 41 | 42 | 43 | @Override 44 | public Element createStyleElement(OpcPackage opcPackage, Document document, 45 | String styleDefinition) { 46 | 47 | 48 | if ((styleDefinition != null) && (styleDefinition.length() > 0)) { 49 | session.setAttribute("css", styleDefinition); 50 | // System.out.println(styleDefinition); 51 | 52 | // 53 | 54 | 55 | // Element ret = document.createElement("link"); 56 | // ret.setAttribute("rel", "stylesheet"); 57 | // ret.setAttribute("type", "text/css"); 58 | // ret.setAttribute("href", Editor.getContextPath() + "/services/css/docx.css"); 59 | // return ret; 60 | 61 | } else { 62 | System.out.println("styleDefinition was null or empty!"); 63 | } 64 | return null; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/toDocx/RoundtripXHTMLImporter.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor.toDocx; 19 | 20 | import java.util.Map; 21 | 22 | import org.docx4j.convert.in.xhtml.XHTMLImporterImpl; 23 | import org.docx4j.jaxb.Context; 24 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 25 | import org.docx4j.org.xhtmlrenderer.newtable.TableBox; 26 | import org.docx4j.org.xhtmlrenderer.newtable.TableCellBox; 27 | import org.docx4j.org.xhtmlrenderer.render.BlockBox; 28 | import org.docx4j.wml.CTTblPrEx; 29 | import org.docx4j.wml.PPr; 30 | import org.docx4j.wml.Tbl; 31 | import org.docx4j.wml.TblGrid; 32 | import org.docx4j.wml.TblPr; 33 | import org.docx4j.wml.Tc; 34 | import org.docx4j.wml.TcPr; 35 | import org.docx4j.wml.Tr; 36 | import org.docx4j.wml.TrPr; 37 | import org.plutext.htmleditor.PPrNone; 38 | import org.w3c.dom.css.CSSValue; 39 | 40 | public class RoundtripXHTMLImporter extends XHTMLImporterImpl { 41 | 42 | public RoundtripXHTMLImporter(WordprocessingMLPackage wordMLPackage) { 43 | super(wordMLPackage); 44 | } 45 | 46 | @Override 47 | protected void setupTblPr(TableBox cssTable, Tbl tbl, TableProperties tableProperties) { 48 | 49 | /* 50 | * Reinstate 51 | * 52 | context.getWmlPackage().setUserData(id+"#Pr", table.getTblPr()); 53 | 54 | as set by SessionAwareAbstractTableWriter 55 | 56 | */ 57 | String id = cssTable.getElement().getAttribute("id"); 58 | 59 | if (id==null ) { 60 | log.debug("no id on table " ); 61 | 62 | } else { 63 | log.debug("processing table with id " + id); 64 | 65 | Object o = wordMLPackage.getUserData(id+"#Pr"); 66 | if (o==null) { 67 | 68 | log.debug("no #Pr UserData on table with id " + id); 69 | 70 | } else { 71 | tbl.setTblPr((TblPr)o); 72 | return; 73 | } 74 | } 75 | 76 | // we need to fall back for a newly created table 77 | super.setupTblPr( cssTable, tbl, tableProperties); 78 | } 79 | 80 | @Override 81 | protected void setupTblGrid(TableBox cssTable, Tbl tbl, TableProperties tableProperties) { 82 | 83 | /* 84 | * Reinstate 85 | * 86 | context.getWmlPackage().setUserData(id+"#Grid", table.getTblGrid()); 87 | 88 | as set by SessionAwareAbstractTableWriter 89 | 90 | */ 91 | String id = cssTable.getElement().getAttribute("id"); 92 | 93 | if (id==null ) { 94 | log.debug("no id on table " ); 95 | 96 | } else { 97 | log.debug("processing table with id " + id); 98 | 99 | Object o = wordMLPackage.getUserData(id+"#Grid"); 100 | if (o==null) { 101 | 102 | log.debug("no #Grid UserData on table with id " + id); 103 | 104 | } else { 105 | tbl.setTblGrid((TblGrid)o); 106 | return; 107 | } 108 | } 109 | 110 | super.setupTblGrid( cssTable, tbl, tableProperties); 111 | 112 | } 113 | 114 | @Override 115 | protected void setupTrPr(org.docx4j.org.xhtmlrenderer.newtable.TableRowBox trBox, Tr tr) { 116 | 117 | /* 118 | * Reinstate 119 | * 120 | * context.getWmlPackage().setUserData(rowId+"#Pr", trPr ); 121 | context.getWmlPackage().setUserData(rowId+"#PrEx", tblPrEx ); 122 | 123 | as set by SessionAwareAbstractTableWriter 124 | 125 | */ 126 | 127 | String id = trBox.getElement().getAttribute("id"); 128 | 129 | if (id==null ) { 130 | log.debug("no id on tr " ); 131 | 132 | } else { 133 | log.debug("processing tr with id " + id); 134 | 135 | // TrPr 136 | Object o = wordMLPackage.getUserData(id+"#Pr"); 137 | if (o==null) { 138 | 139 | log.debug("no #Pr UserData on tr with id " + id); 140 | 141 | } else { 142 | tr.setTrPr((TrPr)o); 143 | } 144 | 145 | Object o2 = wordMLPackage.getUserData(id+"#PrEx"); 146 | if (o2==null) { 147 | 148 | log.debug("no #PrEx UserData on tr with id " + id); 149 | 150 | } else { 151 | tr.setTblPrEx((CTTblPrEx)o2); 152 | } 153 | 154 | if (o!=null || o2 !=null) return; 155 | 156 | } 157 | 158 | super.setupTrPr( trBox, tr); 159 | } 160 | 161 | @Override 162 | protected void setupTcPr(TableCellBox tcb, Tc tc, TableProperties tableProperties) { 163 | 164 | /* 165 | * Reinstate 166 | * 167 | context.getWmlPackage().setUserData(cellId+"#Pr", cell.getTcPr() ); 168 | 169 | as set by SessionAwareAbstractTableWriter 170 | 171 | */ 172 | 173 | String id = tcb.getElement().getAttribute("id"); 174 | 175 | if (id==null ) { 176 | log.debug("no id on tc " ); 177 | 178 | } else { 179 | log.debug("processing tc with id " + id); 180 | 181 | Object o = wordMLPackage.getUserData(id+"#Pr"); 182 | if (o==null) { 183 | 184 | log.debug("no #Pr UserData on tc with id " + id); 185 | 186 | } else { 187 | tc.setTcPr((TcPr)o); 188 | return; 189 | } 190 | } 191 | 192 | super.setupTcPr( tcb, tc, tableProperties); 193 | } 194 | 195 | @Override 196 | protected PPr getPPr(BlockBox blockBox, Map cssMap) { 197 | 198 | // if the paragraph has an ID, use the preserved existing pPr 199 | String id = blockBox.getElement().getAttribute("id"); 200 | 201 | if (id==null ) { 202 | log.debug("no id on p " ); 203 | 204 | } else { 205 | log.debug("processing p with id " + id); 206 | 207 | Object o = wordMLPackage.getUserData(id); 208 | if (o==null) { 209 | 210 | log.debug("no #Pr UserData on p with id " + id); 211 | 212 | } else if (o instanceof PPrNone) { 213 | return null; 214 | } else { 215 | 216 | return((PPr)o); 217 | // (TODO unless the user has changed the style) 218 | } 219 | } 220 | 221 | // A new p the user has created 222 | PPr pPr = Context.getWmlObjectFactory().createPPr(); 223 | populatePPr(pPr, blockBox, cssMap); 224 | return pPr; 225 | } 226 | 227 | } 228 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/toDocx/Saver.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor.toDocx; 19 | 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | import java.util.logging.Level; 23 | import java.util.logging.Logger; 24 | 25 | import javax.servlet.ServletContext; 26 | import javax.servlet.http.HttpServletRequest; 27 | import javax.servlet.http.HttpSession; 28 | import javax.ws.rs.FormParam; 29 | import javax.ws.rs.POST; 30 | import javax.ws.rs.Path; 31 | import javax.ws.rs.WebApplicationException; 32 | import javax.ws.rs.core.Context; 33 | import javax.ws.rs.core.MediaType; 34 | import javax.ws.rs.core.Response; 35 | import javax.ws.rs.core.Response.ResponseBuilder; 36 | import javax.ws.rs.core.Response.Status; 37 | import javax.ws.rs.core.StreamingOutput; 38 | import javax.ws.rs.core.UriInfo; 39 | 40 | import org.docx4j.convert.in.xhtml.XHTMLImporterImpl; 41 | import org.docx4j.openpackaging.exceptions.Docx4JException; 42 | import org.docx4j.openpackaging.io.SaveToZipFile; 43 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 44 | import org.docx4j.org.xhtmlrenderer.docx.Docx4jUserAgent; 45 | import org.docx4j.org.xhtmlrenderer.docx.DocxRenderer; 46 | import org.docx4j.wml.Style; 47 | 48 | 49 | 50 | 51 | 52 | 53 | @Path("/save") 54 | public class Saver { 55 | 56 | private static final Logger jul = Logger.getLogger(Saver.class 57 | .getName()); 58 | 59 | 60 | @POST 61 | //@Path("{url : .+}") 62 | //@Consumes(MediaType.APPLICATION_XML) 63 | //@Produces( {MediaType.APPLICATION_XHTML_XML}) 64 | public Response xhtmlToDocx( 65 | @Context HttpServletRequest request, 66 | // @Context ServletContext context, 67 | // @Context UriInfo info, 68 | @FormParam("editorOutput") String xhtml 69 | ) { 70 | 71 | try { 72 | 73 | HttpSession session = request.getSession(false); 74 | if (session==null) { 75 | return Response.ok( 76 | "Your session has expired.", 77 | MediaType.TEXT_PLAIN).build(); 78 | } 79 | WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)session.getAttribute("docx"); 80 | 81 | // contains   82 | xhtml = xhtml.replace(" ", " ") ; 83 | 84 | // we need to feed our css to Flying Saucer, or it'll use inappropriate defaults 85 | xhtml = "" 86 | + "" + xhtml + ""; 87 | 88 | 89 | jul.log(Level.INFO, xhtml); 90 | 91 | XHTMLImporterImpl xHTMLImporter = new RoundtripXHTMLImporter(wordMLPackage); 92 | xHTMLImporter.setXHTMLImageHandler(new XHTMLtoDocxImageHandler()); 93 | 94 | // SessionAwareDocx4jUserAgent knows how to find images in the session 95 | Docx4jUserAgent ua = new SessionAwareDocx4jUserAgent(session); 96 | DocxRenderer renderer = new DocxRenderer(ua); 97 | xHTMLImporter.setRenderer(renderer); 98 | 99 | wordMLPackage.getMainDocumentPart().getContent().clear(); 100 | wordMLPackage.getMainDocumentPart().getContent().addAll( 101 | xHTMLImporter.convert( xhtml, null) ); 102 | 103 | // Remove DocDefault virtual style, since it upsets table formatting 104 | // (applies space after for example, when that wouldn't otherwise be used) 105 | Style normal = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getDefaultParagraphStyle(); 106 | if (normal!=null) { 107 | normal.setBasedOn(null); 108 | } 109 | 110 | // Now stream the docx 111 | final SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 112 | ResponseBuilder builder = Response.ok( 113 | 114 | new StreamingOutput() { 115 | public void write(OutputStream output) throws IOException, WebApplicationException { 116 | try { 117 | saver.save(output); 118 | } catch (Docx4JException e) { 119 | throw new WebApplicationException(e); 120 | } 121 | } 122 | } 123 | ); 124 | builder.header("Content-Disposition", "attachment; filename=result.docx"); 125 | builder.type("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 126 | 127 | return builder.build(); 128 | 129 | 130 | } catch (Exception e) { 131 | e.printStackTrace(); 132 | System.out.println(e.getMessage()); 133 | throw new WebApplicationException( 134 | new Docx4JException(e.getMessage(), e), 135 | Status.INTERNAL_SERVER_ERROR); 136 | } 137 | 138 | } 139 | 140 | 141 | } -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/toDocx/SessionAwareDocx4jUserAgent.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * {{{ header & license 20 | * Copyright (c) 2004, 2005 Torbj�rn Gannholm 21 | * Copyright (c) 2006 Wisconsin Court System 22 | * 23 | * This program is free software; you can redistribute it and/or 24 | * modify it under the terms of the GNU Lesser General Public License 25 | * as published by the Free Software Foundation; either version 2.1 26 | * of the License, or (at your option) any later version. 27 | * 28 | * This program is distributed in the hope that it will be useful, 29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | * GNU Lesser General Public License for more details. 32 | * 33 | * You should have received a copy of the GNU Lesser General Public License 34 | * along with this program; if not, write to the Free Software 35 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 36 | * }}} 37 | */ 38 | package org.plutext.htmleditor.toDocx; 39 | 40 | import javax.servlet.http.HttpSession; 41 | 42 | import org.docx4j.org.xhtmlrenderer.docx.Docx4JFSImage; 43 | import org.docx4j.org.xhtmlrenderer.docx.Docx4jUserAgent; 44 | import org.plutext.htmleditor.SessionImageHandler; 45 | 46 | public class SessionAwareDocx4jUserAgent extends Docx4jUserAgent { 47 | 48 | HttpSession session; 49 | protected SessionAwareDocx4jUserAgent(HttpSession session) { 50 | this.session = session; 51 | } 52 | 53 | 54 | // private byte[] readStream(InputStream is) throws IOException { 55 | // ByteArrayOutputStream out = new ByteArrayOutputStream(is.available()); 56 | // byte[] buf = new byte[10240]; 57 | // int i; 58 | // while ( (i = is.read(buf)) != -1) { 59 | // out.write(buf, 0, i); 60 | // } 61 | // out.close(); 62 | // return out.toByteArray(); 63 | // } 64 | 65 | @Override 66 | public Docx4JFSImage getDocx4JImageResource(String uri) { 67 | 68 | // System.out.println("Looking at " + uri); 69 | String name = uri.substring(uri.lastIndexOf("/")+1); 70 | // System.out.println("Looking for " + name); 71 | 72 | byte[] bytes = SessionImageHandler.getImageMap(session).get(name); 73 | if (bytes!=null) { 74 | // System.out.println(".. found in session "); 75 | return new Docx4JFSImage(bytes); 76 | } 77 | // System.out.println(".. not found in session "); 78 | return super.getDocx4JImageResource(uri); 79 | 80 | } 81 | 82 | // public static void main(String[] args) throws Exception { 83 | // 84 | // String uri = "http://localhost:8086/docx4j-web-editor-1.0.0-SNAPSHOT/services/image/hi.png"; 85 | // 86 | // System.out.println("Looking at " + uri); 87 | // String name = uri.substring(uri.lastIndexOf("/")+1); 88 | // System.out.println("Looking for " + name); 89 | // 90 | // } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/plutext/htmleditor/toDocx/XHTMLtoDocxImageHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of docx-html-editor. 3 | 4 | docx-html-editor is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Affero General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Affero General Public License for more details. 13 | 14 | You should have received a copy of the GNU Affero General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | package org.plutext.htmleditor.toDocx; 19 | 20 | import org.docx4j.convert.in.xhtml.XHTMLImageHandlerDefault; 21 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 22 | import org.docx4j.org.xhtmlrenderer.docx.Docx4jUserAgent; 23 | import org.docx4j.wml.P; 24 | import org.w3c.dom.Element; 25 | 26 | public class XHTMLtoDocxImageHandler extends XHTMLImageHandlerDefault { 27 | 28 | @Override 29 | public void addImage(Docx4jUserAgent docx4jUserAgent, WordprocessingMLPackage wordMLPackage, P targetP, Element e, Long cx, Long cy) { 30 | 31 | // This design doesn't allow the user to resize the image in the editor, 32 | // since we don't trust it to allow that to be done accurately 33 | 34 | 35 | String id = e.getAttribute("id"); 36 | 37 | 38 | if (id==null ) { 39 | log.debug("no id on image with src " + e.getAttribute("src") ); 40 | // System.out.println("no id on image with src " + e.getAttribute("src") ); 41 | 42 | } else { 43 | log.debug("processing image with id " + id); 44 | // System.out.println("processing image with id " + id); 45 | 46 | Object o = wordMLPackage.getUserData(id); 47 | if (o==null) { 48 | 49 | log.debug("no UserData on image with id " + id); 50 | // System.out.println("no UserData on image with id " + id); 51 | 52 | } else { 53 | if (o instanceof P) { 54 | P pStored = (P)o; 55 | // replace p contents 56 | targetP.setPPr(pStored.getPPr()); 57 | targetP.getContent().clear(); 58 | targetP.getContent().addAll(pStored.getContent()); 59 | return; 60 | } else { 61 | log.debug("adding " + o.getClass().getName() ); 62 | 63 | // Just add the contents 64 | targetP.getContent().add(o); 65 | return; 66 | } 67 | 68 | } 69 | } 70 | 71 | super.addImage(docx4jUserAgent, wordMLPackage, targetP, e, cx, cy); 72 | 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | docx4j-web-editor 5 | 6 | 14 | 15 | 18 | 19 | services 20 | org.glassfish.jersey.servlet.ServletContainer 21 | 24 | 25 | javax.ws.rs.Application 26 | org.plutext.htmleditor.JAXRS2Application 27 | 29 | 30 | 1 31 | 32 | 33 | 89 | 90 | 91 | 92 | services 93 | /services/* 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /src/main/webapp/browser/browser.css: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one 4 | * or more contributor license agreements. See the NOTICE file 5 | * distributed with this work for additional information 6 | * regarding copyright ownership. The ASF licenses this file 7 | * to you under the Apache License, Version 2.0 (the 8 | * "License"); you may not use this file except in compliance 9 | * with the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, 14 | * software distributed under the License is distributed on an 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | * KIND, either express or implied. See the License for the 17 | * specific language governing permissions and limitations 18 | * under the License. 19 | * 20 | */ 21 | 22 | @CHARSET "UTF-8"; 23 | 24 | body { 25 | font-family: arial,sans-serif; 26 | font-size: 10pt; 27 | } 28 | 29 | h1 { 30 | font-family: Georgia; 31 | } 32 | 33 | h2 { 34 | font-family: Georgia; 35 | } 36 | 37 | h3 { 38 | font-family: Georgia; 39 | } 40 | 41 | a:link { 42 | color: #204a87; 43 | text-decoration: none; 44 | } 45 | 46 | a:visited { 47 | color: #204a87; 48 | text-decoration: none; 49 | } 50 | 51 | a:hover { 52 | color: #204a87; 53 | text-decoration: underline; 54 | } 55 | 56 | a:active { 57 | color: a40000; 58 | text-decoration: underline; 59 | } 60 | 61 | div.navigationbox { 62 | background-color: #f6f1de; 63 | margin-top: 10px; 64 | margin-bottom: 10px; 65 | margin-left: 0px; 66 | margin-right: 0px; 67 | padding: 5px; 68 | border-style: solid; 69 | border-width: 1px; 70 | border-color: #888a85; 71 | } 72 | 73 | table.servicetable { 74 | border-collapse: collapse; 75 | } 76 | 77 | table.servicetable th{ 78 | font-size: 10pt; 79 | font-weight: bold; 80 | padding: 5px; 81 | text-align: left; 82 | } 83 | 84 | table.servicetable td { 85 | font-size: 10pt; 86 | padding: 5px; 87 | border-bottom-style: solid; 88 | border-color: #dddddd; 89 | border-width: 1px; 90 | } 91 | 92 | .servicebox { 93 | background-color: #f6f1de; 94 | font-size: 10pt; 95 | padding: 10px; 96 | margin: 10px; 97 | border-color: #888a85; 98 | border-style: solid; 99 | border-width: 2px; 100 | } 101 | 102 | table.feedtable { 103 | border-collapse: collapse; 104 | } 105 | 106 | table.feedtable th { 107 | font-size: 10pt; 108 | font-weight: bold; 109 | text-align: left; 110 | } 111 | 112 | .tdinfo { 113 | font-size: 10pt; 114 | padding-left: 3px; 115 | padding-top: 3px; 116 | padding-bottom: 0px; 117 | padding-right: 10px; 118 | } 119 | 120 | .tdinfo2 { 121 | font-size: 10pt; 122 | padding: 3px; 123 | border-bottom-style: solid; 124 | border-color: #d3d7cf; 125 | border-width: 1px; 126 | } 127 | 128 | .tdlinks { 129 | font-size: 8pt; 130 | padding: 3px; 131 | border-bottom-style: solid; 132 | border-color: #d3d7cf; 133 | border-width: 1px; 134 | } 135 | 136 | .tdmore { 137 | background-color: #eeeeec; 138 | font-size: 10pt; 139 | padding-left: 3px; 140 | padding-top: 3px; 141 | padding-bottom: 0px; 142 | padding-right: 10px; 143 | } 144 | 145 | .tdinfo-odd { 146 | background-color: #fcf9df; 147 | font-size: 10pt; 148 | padding-left: 3px; 149 | padding-top: 3px; 150 | padding-bottom: 0px; 151 | padding-right: 10px; 152 | } 153 | 154 | .tdinfo2-odd { 155 | background-color: #fcf9df; 156 | font-size: 10pt; 157 | padding: 3px; 158 | border-bottom-style: solid; 159 | border-color: #d3d7cf; 160 | border-width: 1px; 161 | } 162 | 163 | .tdlinks-odd { 164 | background-color: #fcf9df; 165 | font-size: 8pt; 166 | padding: 3px; 167 | border-bottom-style: solid; 168 | border-color: #d3d7cf; 169 | border-width: 1px; 170 | } 171 | 172 | .tdmore-odd { 173 | background-color: #fce94f; 174 | font-size: 10pt; 175 | padding: 3px; 176 | border-bottom-style: solid; 177 | border-color: #d3d7cf; 178 | border-width: 1px; 179 | } 180 | 181 | a.actionlink:link { 182 | color: #888a85; 183 | text-decoration: none; 184 | } 185 | 186 | a.actionlink:visited { 187 | color: #888a85; 188 | text-decoration: none; 189 | } 190 | 191 | a.actionlink:hover { 192 | color: #2e3436; 193 | text-decoration: underline; 194 | } 195 | 196 | a.actionlink:active { 197 | color: red; 198 | text-decoration: underline; 199 | } 200 | 201 | table.entrytable { 202 | border-collapse: collapse; 203 | } 204 | 205 | table.entrytable th { 206 | font-size: 10pt; 207 | font-weight: bold; 208 | text-align: left; 209 | } 210 | 211 | table.entrytable td { 212 | font-size: 10pt; 213 | padding: 5px; 214 | border-bottom-style: solid; 215 | border-color: #d3d7cf; 216 | border-width: 1px; 217 | } 218 | 219 | div.entrysummary { 220 | margin-top: 10px; 221 | margin-bottom: 10px; 222 | margin-left: 0px; 223 | margin-right: 0px; 224 | padding: 5px; 225 | border-style: solid; 226 | border-width: 1px; 227 | border-color: #888a85; 228 | height: 150px; 229 | overflow: auto; 230 | } 231 | -------------------------------------------------------------------------------- /src/main/webapp/browser/cmis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/cmis.png -------------------------------------------------------------------------------- /src/main/webapp/browser/document.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/document.png -------------------------------------------------------------------------------- /src/main/webapp/browser/folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/folder.png -------------------------------------------------------------------------------- /src/main/webapp/browser/policy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/policy.png -------------------------------------------------------------------------------- /src/main/webapp/browser/relationship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/relationship.png -------------------------------------------------------------------------------- /src/main/webapp/browser/type.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/type.png -------------------------------------------------------------------------------- /src/main/webapp/browser/unknown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/browser/unknown.png -------------------------------------------------------------------------------- /src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | docx html editor - file upload form 8 | 9 | 122 | 123 | 124 | 125 | 126 | 127 |
128 | 129 |

Test your document

130 | 131 | 132 |
134 |

135 | Choose file to upload: 158 |

159 | 160 |

161 | Return bare 162 | HTML only (ie no editor) 163 |

164 | 165 |

166 | 167 | 168 |
169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /src/main/webapp/placeholders/bookmark_end.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/bookmark_end.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/bookmark_start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/bookmark_start.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/chart.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/comment_range_end.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/comment_range_end.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/comment_range_start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/comment_range_start.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/comment_ref.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/comment_ref.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/endnote.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/endnote.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/equation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/equation.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/footnote.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/footnote.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/page_break.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/page_break.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/sectPr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/sectPr.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/signature.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/signature.png -------------------------------------------------------------------------------- /src/main/webapp/placeholders/tab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutext/docx-html-editor/ebbd13f84db483a3d17c93f8fe88d7f5b30c4758/src/main/webapp/placeholders/tab.png --------------------------------------------------------------------------------